Skip to content
Get Free Trial

Miscellaneous

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 https://sahitest.com/demo/training/login.htm. You should be able to record and run correctly.

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

Section titled “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?

Section titled “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?

Section titled “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 in the Controller. 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 Run Tests via Controller.

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)
$data.push($firstRow);
var $secondRow = [];
$secondRow[0] = _getText(_div(2)); // assuming _div(2)
$secondRow[1] = _getText(_div(3)); // assuming _div(3)
$data.push($secondRow);
_writeCSVFile($data, "C:/a.csv", true);

You can run it against https://sahitest.com/demo/spandiv.htm

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.

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.

    _typeKeyCodeNative(java.awt.event.KeyEvent.VK_TAB);
    // 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.

    _typeKeyCodeNative(java.awt.event.KeyEvent.VK_SPACE);
    // 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.
    _focusWindow();
    _click(_link("Sign out"));
    _wait(2000);
    _typeKeyCodeNative(java.awt.event.KeyEvent.VK_SPACE);

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).

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.

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

We have ourselves not tried an integration with FitNesse.

But someone else has done it here.

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");

    or

    _rteWrite(_iframe("rtedemo"),"some text");

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 192.168.1.21

    Configure browser proxy setting to address:192.168.1.21 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 192.168.1.21:9999 to open the launcher url from where you can choose the scripts or suite and run the tests.

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();
_navigateTo("http://localhost/demo/getUsers?"+$uniqueID);

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.

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.

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.

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.

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.

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?

Section titled “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

_call(_textbox('dob').value="14/08/1990");

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

Integrate Sahi with existing test repository tools

Section titled “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.

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;

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"))) {
_click(_button("Confirm"));
}
else {
_assert(false, '_button("Confirm") does not exist');
}

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?

Section titled “Does Sahi Excel framework support .xlsx files?”

One can write .xlsx files as part of the Excel framework.

Question: How can I find the parent of an element using the Controller?

Answer: Do the following.

  1. Identify the element by pressing the Ctrl (⌘ on Mac) 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.

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.

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.

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.

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

Section titled “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

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.

Question: Does Sahi support Silverlight applications?

>

Answer: No, Sahi does not support Silverlight applications.

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.

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

Question: How do I use a regular expression in any Sahi function?

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/").

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.

_click(_link("/search/"));

This regular expression will match strings like,

_click(_link("search1234"));
_click(_link("search-132"));
_click(_link("sdf-search-sdf"));

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.

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.

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.

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.

Question: Does 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

Section titled “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.

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).

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.