Verifying Sahi installation

question How can we verify that Sahi is installed and working properly?
answer You can check the following.
  • If you are on Windows, launching Sahi Pro from the Desktop shortcut should launch the Sahi Dashboard.

    If you are on Linux or Mac, navigate to userdata/bin. Launch start_dashboard.sh to start Sahi server with the Dashboard.
  • Open a browser from the Sahi Dashboard. This should open the http://sahi.example.com/_s_/dyn/Driver_initialized page correctly.
  • Access http://sahitest.com/demo/training/login.htm. You should be able to record and playback correctly.

How can I run the same script multiple times with different parameters simultaneously?

The data driven suite allows the user to specify a script multiple times in a suite with different parameters.

To execute the scripts in the data driven suite in parallel, you just need to use testrunner.bat with SINGLE_SESSION as false and THREADS=5 (any value greater than 1).

How can I launch/execute a url that initiates some long running activity?

question My application has a component that performs some long running activity. This component functionality is exposed through a url. How can I launch/execute this url from a Sahi script?
answer Use _readURL(url) and give the component url. You don't need to process the returned value. This will essentially execute the url call.

Is it possible to add watch variables and debug a Sahi script?

To add watch variables, use _debug, and _log statements and run the script, you can step through various steps from the controller.
  1. Add a _wait(10000) in the script so the script waits at that point.
  2. When this wait step is being evaluated, press Pause on the Playback tab. Once paused, you can use the controller buttons like Click, Highlight, Set, etc.
  3. You can then press Step to get out of the wait immediately and process one step at a time. Or you can press Play. Play will remain in the wait till it is over.
To debug the Sahi script, refer Debugging Sahi Scripts and Debug during Playback.

Copy row text and output to csv file

question I'm trying to copy text (using getText) and output it to a csv file. How can I do this?
answer A simple example would be:
var $data = [];
var $firstRow = [];
$firstRow[0] = _getText(_div(0)); // assuming _div(0)
$firstRow[1] = _getText(_div(1)); // assuming _div(1)

var $secondRow = [];
$secondRow[0] = _getText(_div(2)); // assuming _div(2)
$secondRow[1] = _getText(_div(3)); // assuming _div(3)

_writeCSVFile($data, "C:/a.csv", true);

You can run it against http://sahi.co.in/demo/spandiv.htm

Java assertEquals

When using the Java driver, it is better to use JUnit's assertEquals method directly, instead of trying to use the Sahi equivalent as follows - b.executeSahi("_assertEqual(...)").

Likewise, use JUnit's other assert methods instead of invoking equivalent Sahi asserts through b.executeSahi.

How to handle onbeforeunload popup

question When I click on a link in current page, it should take me to a new page.

But it throws a windows popup dialog that says "This page is asking you to confirm that you want to leave - data you have entered may not be saved." with two buttons "Leave this page" and "Stay on this page".

How do I automate this dialog?
answer The popup dialog that you see is thrown from the window.onbeforeunload function that the page has implemented. This is not a web dialog and cannot be identified by Sahi.

  • You can avoid this popup while running through Sahi by adding this line
    _call(window.onbeforeunload = null);

    before the link click that navigates out of the page.

    This will cause the new page to be shown without the alert being shown to you.
  • If this does not work, you can use Native keypress events to click on the "Leave Page" button.

    Typically, the focus would be on the "Leave Page" button, so pressing Space or Enter key will cause the button to be clicked.

    Use below native key events.

    To move focus between controls, use VK_TAB or VK_SHIFT_TAB (to give focus to previous element). eg.
    // Presses TAB to change the current focus to give focus to next element.
    To click on the button, use VK_SPACE or VK_ENTER. eg.
    // Fires Space key press event on the focused element. Or you can use VK_ENTER.

    So you can use a combination of the above methods to navigate between the controls and press Space or Enter on the focused buttons.

    Example code:
    // Do _focusWindow before clicking on the link that throws up the dialog.
    _click(_link("Sign out"));

Using pound and euro symbols

question How can we refer to the pound or euro symbol from inside a script?
answer Since SahiScript is really Javascript, you could either use
  • The Javascript notation - the unicode character \u00A3 for pound and \u20AC for euro.
  • Or use the literal pound or euro sign itself in the script. You will need to ensure that the Sahi script is UTF 8 encoded (If you are using Notepad++, use the option Encode in UTF-8 without BOM).

Reading an XML file

question How can I read an XML file using Sahi script?
answer We don't have out of the box functionality yet to process xml files from Sahi scripts like we have for Excel files.

But from Sahi scripts you can invoke any java class that is available in the class path. So you can create java class(es) for reading xml file and you can call the same from sahi.

Refer to Sahi Scripting - Calling Java below for how to call java classes from sahi.

Integration with XStudio

The following urls contain details about the integration.

http://www.xqual-forums.com/viewtopic.php?f=4&t=1545 http://www.xqual.com/documentation/launchers/sahi.html

Integration with FitNesse

We have ourselves not tried an integration with FitNesse.

But someone else has done it here.

Using _rteWrite()

question How do I use _rteWrite() to write to a Rich Text Editor? The _rte accessor is not visible from the controller.
answer From Controller you will not be able to find the rte accessor.

To write to the rich text editor field, follow below steps.
  1. Open the RTE page in the browser.
  2. Right click on the RTE and select Inspect Element.
  3. You will find that RTE is in some container like IFrame.
  4. Copy the id of that Iframe. Let us say, it is "rtedemo".
  5. Now you can use this in the _rteWrite() statement like
    _rteWrite(_rte("rtedemo"),"some text");
    _rteWrite(_iframe("rtedemo"),"some text");

Accessing Sahi from a different browser

question I have a test lab machine that has Sahi installed. I wish to test Sahi from remote machines so that I can cover different client configurations (OS and Browser). How do I do this?
answer What you want to do is:
  1. Run Sahi server on the remote machine.
  2. Run tests from your browser (without running Sahi on your machine)
For this, essentially your browser needs to be configured to use the remote machine's proxy. There is no way to do so automatically.

NOTE that when you launch browsers from the Sahi Dashboard on your local machine, the proxy settings are automatically configured. But since you want to use a remote proxy, the configuration has to be done manually.

  1. Start Sahi on remote machine.
  2. Open browser on your machine. Note that Sahi is not running on your machine, so you will be opening the browser outside of Sahi.
  3. Configure the proxy setting in your browser to point to your remote machine.

    Example: If Remote machine IP is

    Configure browser proxy setting to address: and port:9999.
There are 2 ways in which you can run the scripts on your machine.
  1. Open the controller on the page and run scripts. You cannot run a suite from the controller.
  2. Access to open the launcher url from where you can choose the scripts or suite and run the tests.

How can I prevent caching of page?

question Web pages are being cached in my application. How can I ensure that a new page is returned everytime?
answer We can append a unique identifier to the url such that the url is unique every time.

We can use new Date().getTime() as the unique id to append to the url.

For example:
var $uniqueID=new Date().getTime();

Text of alert shown through Ext.msg.show

question We use ext js. How can I get the text of an alert shown using Ext.msg.show?
answer When you use Ext.msg.show to show the message(alert), it shows the alert as an HTML inline element on the page and it is not suppressed by Sahi. Hence it is visible on the page.

Since it is a HTML element, we have access to that element and we can deal with it from the controller.

Mostly you will see a _div() container containing the alert on the page.

You can use _getText() function to get the text of alert.

But since the alert is not suppressed, you will have to click on OK or Cancel from the script.

_wait does not work inside an Excel file

To work around this, add a function called doWait() in the script that you are loading inside the excel file. function doWait($timetowait){ _wait($timetowait); } Call doWait from inside the excel file, instead of calling _wait.

When is _condition needed?

question When is it mandatory to use _condition in an if statement in Sahi?
answer If you are checking for any attribute through a Sahi function, like _getText, _isVisible, _exists etc, you do not need to use _condition.

If you are checking for an attribute directly, like say _div("some-div").innerHTML, then you should use _condition.
info However, using _condition is not harmful, so when you are in doubt, use _condition.

_assertExists parameters

question I am using _assertExists(condition, _alert('condition exists')) to alert if condition exists. The alert seems to be called always.
answer NOTE that the second parameter in _assertExists should be a string message. You cannot have a function call or a Sahi step in the second parameter.

The message will be logged if the condition does NOT exist. So the correct usage is _assertExists(condition, 'condition does not exist')).

In the given usage, the _alert step gets evaluated like a normal Sahi step and hence you always get the alert.

Java driver tests in parallel

question We are using Sahi with Java driver. We have a customized runner to run the java tests (junit based). Is it possible to run the java tests in parallel?
answer Multiple browser instances can be launched using new Browser() instances.

So if your junit based test runner can execute testcases in parallel, you will be able to run java tests in parallel.

Accessing a random row from a grid

question How can I select a random row from a grid?
answer You can do the following.
  1. Use _count() to get the count of rows in the grid.
  2. Use _random(rowcount-1) to get a random number between 0 and rowcount-1.
  3. Identify the row at this random index

How do I set Date field which has date separator?

question I have a Date of Birth field with date separators (/). How can I enter the date through Sahi?
answer Based on the format (dd/mm/yyyy or mm/dd/yyyy or something else), do an appropriate _setValue which includes the / separator. Example: Assuming that the format is dd/mm/yyyy
_setValue(_textbox('dob'), '14/08/1990')

If this results in an invalid date being set, ignore the / separator and just do
_setValue(_textbox('dob'), '14081990')

If this does not work, try the following

Same goes for a Phone Number field or a SSN field that has - as a separator.

Integrate Sahi with existing test repository tools

question How easy is it to integrate Sahi with an existing test repository tool?
answer Sahi can be almost entirely controlled using http calls. You can trigger testcases, view reports, launch browsers etc. using just URL calls. It should be easy to integrate with your test repository tool.

Read data from Excel using Sahi APIs

question I am trying to read data from an excel. How can I get the active row count?
answer Have a look at this section Working with Excel Sheets in Sahi Pro.

You would do something like:

var $excel = _getExcel("D:\\poiTest.xls","Sheet1"); var $data=$excel.getData(); var $activeRowCount = $data.length;

_assertExists in if condition

question Can I use _assertExists in if condition?
answer _assertExists does not return true or false. So you cannot use it inside if condition. Instead use _exists in the if condition, like below.
if (_exists(_button("Confirm"))) {
else {
	_assert(false, '_button("Confirm") does not exist');

Assert equality of arrays

question How can I assert that two arrays are equal?
answer You can use _assertEqual and pass the two arrays to directly compare the two arrays. You don't need to loop the arrays and compare the elements individually.

Does Sahi Excel framework support .xlsx files?

One can write .xlsx files as part of the Excel framework.
danger However note that you should not rename .xls files as .xlsx.

Identifying parent of element

question How can I find the parent of an element using the Controller?
answer Do the following.
  1. Identify the element by pressing Ctrl key and hovering mouse over the element.
  2. Once the element identifier is visible in the Accessor textbox in the Record tab, click the Up arrow near the Accessor: label.
  3. This will show the parent of the element.
  4. To come back to the previous child element, press the Down arrow near the Accessor: label.
  5. You can walk up the parent heirarchy by continously clicking on the Up arrow.
info Finding the parent is especially useful when you want to locate the element inside a container element using the _in Relation API.

Identify element class using Java Driver

question How can I get the class of an element using the Java driver?
answer You can use the following line of code.
String className = b.button("Login").fetch("className"); // Replace button("Login") with the specific element type and name.

Scripts in suite do not run in order

question I have SET SINGLE_SESSION=true and SET THREADS=1 in testrunner.bat. When I run a suite, it seems that scripts are not being executed in the order mentioned in the suite but in alphabetical order. Why is this so?
answer The scripts are executed in the same order as listed in the suite. However the script links in the Suite Report are shown alphabetically.

401 authentication dialog

question My website puts up a 401 Authentication windows dialog. How can I automate this?
answer You will see the windows dialog from a normal browser outside of Sahi.

But when you go through Sahi, Sahi will intercept the original response and present its own embedded dialog in the html page. Since the dialog is embedded in the html page itself, this can be automated like any other html page.
info Always test the behavior through a Sahi browser because that is what the test script will interact with.

Browser in testrunner command

question How do I specify the browser(s) in the testrunner command?
answer When running a suite/script with testrunner.bat, one needs to specify the browser to run the test(s) in.

Do the following to find out the browser label
  1. Only those browsers that are available in the Dashboard can be tested against. If your preferred browser is not part of the Dashboard, refer to this link.
  2. Hover the mouse over your preferred browser button on the Dashboard. A tooltip will appear. The tooltip text is the browser label that should be passed to the testrunner command.

Run tests in multiple browsers simultaneously

question Is it possible to run the same script or suite in 3 different browsers (IE, Firefox and Chrome) simultaneously?
answer Yes, it is possible to run the same script or suite in 3 different browsers simultaneously through testrunner.

Start Sahi Pro, click "Bin" link on the Dashboard.

In the command prompt, you can run myscript.sah on IE, Firefox and Chrome simultaneously as follows.
testrunner myscript.sah http://sahi.co.in/demo/ ie+firefox+chrome

Similarly you can run a suite mysuite.suite on the 3 browsers as follows.
testrunner mysuite.suite http://sahi.co.in/demo/ ie+firefox+chrome

Selective login

question My application has a login page. On IE, in the first run of the script, if I close the browser without logging out, the user's session is not terminated. When I run the script the second time, the application shows the user as logged in, and so the login step fails. How can I handle login selectively?
answer Ideally you should logout from the application at the end of the test.

If you don't want that, you can do the following.
  • If there is no Logout button, do a Login.
  • If there is a Logout button,
    • Either logout and login
    • Or do NOT login.

Silverlight support

question Does Sahi support Silverlight applications?
answer No, Sahi does not support Silverlight applications.

Dropdown with Java driver

question How can I choose a value from a dropdown using Java driver?
answer You can use the following code.
b.select("colors").choose("red"); // Here "colors" identifies the dropdown and "red" identifies a value in the dropdown.

Getting absolute file path

question We refer to a file from inside a script using relative path. How can we get the absolute path of the file.
answer Use _resolvePath

Regular expressions in Sahi

question How do I use a regular expression in any Sahi API?
answer In Sahi, we treat the regular expression as a String, so specify it within double quotes. A regular expression starts and ends with /. Example: If you had a textbox whose name shows up as Test email 123, you can specify this as _textbox("/Test email/").

Regular expression usecase

question A link on my page changes dynamically every time but has some words that do not change. How can I test this link from a Sahi script?
answer Use a regular expression that matches the unchanged text in the link identifier.

Example: Assume that the text "search" stays static in the link identifier.
This regular expression will match strings like,

Check to see that page is functional

question What is the best way to perform a simple check that a page is functional and displaying correct content? Should I use _exists(), _isVisible() etc? Can I use _assertEqual to check for content?
answer There is no standard answer to this question.

_exists() checks if an element is available in the Javascript DOM.

_isVisible() checks if an element is available in the JavaScript DOM and also whether it is visible. By visibility, we mean DOM visibility and not whether it is visible on the screen.

So if you have an element whose display is set to none, _exists() will return true while _isVisible() will return false.

From a functional point of view, _isVisible() can be more useful because an end user only interacts with visible elements.

In normal use cases, _assertEqual and _assert for _isVisible would be the most used. _assertEqual is used mostly to
  1. Check for messages shown on the screen.
  2. Check default values
  3. Validate output values of calculations.
_assert(_isVisible()) would be used to check if the correct elements appear based on workflow/permissions etc.

Excel files as part of Suite

question Can excel files be run as part of a suite?
answer Yes, you can add excel files as part of a suite and run the suite through testrunner.bat.
warning Note that if testrunner.bat is run with multiple threads, the excel files will be run in parallel. So make sure that there are no dependencies between the excel files.

Reports database

question What database is used by Sahi internally for test reports generated by Sahi?
answer We use h2 database.

We also support MYSQL, ORACLE, POSTGRESQL and MSSQL, but you will have to manually run the db script and make configuration changes.

create_report_tables_mysql.sql, create_report_tables_oracle.sql, create_report_tables_postgresql.sql and create_report_tables_mssql.sql are available inside Sahi/config/db folder.

Scheduling tests

question How can I schedule tests to run at a specific time?
answer To trigger the tests at a particular time, you can do either of the following
  • You can configure Jenkins to build periodically, and trigger the batch file or ANT task at the end of a build.
  • Or use the Windows Task Scheduler.

Drag and drop

questionDoes Sahi support drag and drop?
answer Yes, Sahi Pro supports the drag and drop feature through the following APIs.

_dragDrop _dragDropXY

Single smoke script to test multiple sites

question I would like to use a single smoke script to test multiple websites (staging, prod, etc) and show the results using the suite report. How can I do this?
answer It is possible for you to have a suite that calls the same script multiple times, only differing in the start url.

Example: You could have a demo.suite with contents like

test.sah http://url1.com test.sah http://url2.com test.sah http://url3.com

If you need to pass parameters, use a data driven suite instead of a suite.

_debug() output

question _debug does not seem to print on console of the browser.
answer _debug() will not write to the console of the browser. It will write to the Sahi server console (the black command window).

Sahi with file: URLs

question Can Sahi work with file: URLs?
answer Sahi does not work on file: URLs. It will work only with http or https urls.

So you cannot use file: URLs
  • As start urls with testrunner.bat.
  • In the _navigateTo statement.


question What does _runUnitTests do?
answer It picks all the functions in the script which start with test and executes them one after the other (in unit testing style).

  • Use _runUnitTests() to run all tests.
  • Pass an array of testcases in _runUnitTests() to run the specific set of testcases. Example:_runUnitTests(["testUserNameMax50Characters", "testPasswordAlphaNumeric"]) will run testUserNameMax50Characters and testPasswordAlphaNumeric.
For more details, refer to this link.