Latest Updates. News. Insights. Ideas.

tutorials Archives - Sahi Pro

Fetching multiple elements? _collect APIs to your rescue.

Posted by | Sahi, technical, tutorials, web automation | No Comments

There are multiple cases in automation where you would like to fetch multiple elements. In such cases, you can use any of the three APIs – _collect, _collectAttributes and _collectElementsInfo.
Let us see each API in detail.

This API returns an array of element stubs of all elements of type apiType matching the identifier within relations.

_collect($apiType, $identifier[, $relations ...])

$apiType string type of elements to collect. Eg. "_link", "_button", "/text/" etc.
$identifier string|int Sahi Identifier. Can also be just an index.
$relations … relations optional Relations like _in, _near etc.

The next API to check out is _collectAttributes
This returns an array of element attributes of all elements of type apiType matching the identifier within relations.

_collectAttributes($apiType, $identifier, $attribute[, $relations ...])

$apiType string type of elements to collect. Eg. "_link", "_button", "/text/" etc.
$identifier string|int Sahi Identifier. Can also be just an index.
$attribute attribute|function attribute or function.
$relations … relations optional Relations like _in, _near etc.This can fetch

This API can fetch

  • a simple attribute from multiple elements
  • one of the few simple attributes from multiple elements
  • some synthesized/processed values from multiple elements by executing
    • an inline function for each element
    • a defined function for each element
    • nested functions

The third API to note is _collectElementsInfo
This API returns array of Accessor Info object of all elements of type apiType within relations.

_collectElementsInfo($apiType[, $relations ...])

$apiType string Type of elements to collect. Can be regular expression. Eg. "_link", "_button", "/text/" etc.
$relations … relations optional Relations like _in, _near etc.

To know more about Fetch APIs, please refer this link:

Feel free to write your questions to support [at] sahipro [dot] com

Common mistakes while using Sahi Excel Framework – Data Driven Testing

Posted by | Sahi, troubleshooting, tutorials, Uncategorized | No Comments

Sahi Excel Framework is a powerful feature which bridges the gap between domain experts and automation engineers. We had highlighted Sahi Excel Framework in our Five Questions Series here:

In this post, we intend to highlight the common mistakes people make while using Sahi for Data Driven Testing

Syntax and Data File Errors:

  • Missing = after the variable name.
    Some testers seem to read the external file and store the data (array) in a variable. They miss out the = after the variable name.
    Incorrect Usage:
    variableName _readCSVFile(“fileName”)

    Expected Usage:
    variableName= _readCSVFile(“fileName”)Error

  • A common mistake observed was the usage of _readCSV, _readExcel instead of _readCSVFile and _readExcelFile.
  • Using the incorrect API to read the external files
    There are three APIs you need to be aware of: _readCSVFile, _readExcelFile and _getDB. You need to use these APIs based on where you are saving your test data which could be CSV, Excel or a database.
  • First column is not empty. There should not be any data under the first column header.
    Expected: BlankColumn
  • Mismatch in the header names. The data from the column will not be considered if the header name doesn’t match in the scenario file and data file.
  • Keyword and Data are not enclosed in square brackets
  • Instead of [variable:header:columnName], many testers have used [variable:header.columnName] They have used . instead of : and you would notice that the values would not be picked up for the fields.

Which other issues have you encountered while using Sahi Excel Framework for Data Driven Testing? Do let us know through your comments.

Five Questions Series: Sahi Controller

Posted by | features, Sahi, tutorials | No Comments

Each week, we will pick a feature of Sahi Pro and hope to highlight its capabilities, benefits and tips on how to use it effectively by asking not more than five questions. We begin our Five Questions Series on Sahi Pro by focusing on Controller.

What is Sahi Controller?
Sahi Controller allows you to interact with Sahi and easily experiment and debug on the browser. Using Sahi Controller, one can easily identify elements, record and playback the scripts. One can launch Controller by pressing ALT and double clicking anywhere on the browser. Kick-start your web automation by recording using Sahi Controller and using the scripts as it is or creating functions out of the recorded snippets of code.

What are the capabilities of Sahi Controller?
Sahi Controller has many powerful capabilities spread across four tabs?—?Record, Playback, Clipboard and Info.

Sahi Controller

Sahi Controller

Record: This tab has controls to record a script. Recording is as easy as entering a name for the script and clicking on Record button. All the actions performed on the browser will be recorded and displayed in ‘Recorded steps’ tab.

One can identify elements using CTRL + mouseover and the Sahi Accessor for the element is displayed. Other actions like Click, Highlight, Set, Hover can also be performed on this element.

Action Buttons

Action Buttons

As seen in the image, each button invokes a        specific API.
  Click: _click API
  Hilight: _highlight API
  Set: _setValue API
  Hover: _mouseOver API



Assert: One can add assertions to verify if the end result is as expected. To add assertions, CTRL+mouseover an element. Click on ‘Assert’ button to view the possible assertions. It will be displayed in the ‘Evaluate Expression’ tab. Once tested, the assert statements can be appended to the main script.

Evaluate Expression: This can be used to evaluate snippets of Sahi code. Clicking on Test will evaluate the content and display the result (true/false/undefined) in the box below.

Smart dropdown for Evaluate Expression: If one wants to use Sahi API on only a part of the expression in the Evaluate Expression box, select the text and choose an action from the dropdown. This would in turn trigger an API and the results will be displayed in the box below. In this case, the ‘Is Visible’ option to trigger the API ‘_isVisible’ on the element ‘_textbox(“total”)’ is highlighted.

API on an element

API on an element

Playback: Using this tab, one can playback the scripts which are already recorded. Providing a Start URL and checking the checkbox will ensure that the script runs by first navigating to the URL mentioned. If the checkbox is not checked, the script will be executed on the current page without refreshing it.

Sahi Record Tab

Sahi Record Tab

Clicking on each of the links will open the respective window. For ex: Clicking on Logs will display the logs for all the scripts run so far. Clicking on a specific script on that page would display the specific logs.

Clipboard: This tab can be used to store small pieces of information.

Info: This tab shows the basic information of Sahi Controller, mode, browser, operating system and Java.

How are the elements on a web page identified?
Any element on the webpage can be identified by pressing CTRL and mouseover the element. The best Sahi accessor for the element is displayed by default in the Accessor field. One has the option to choose an alternative from the ‘Alternatives’ dropdown. Value shows the value of the current element. Prefix populates the prefix as needed when working with popup windows and multiple domains.

Element Identification

Element Identification

Tell us more about the Relational API and anchoring an element.
Sometimes, an element changes its position/name due to code changes and the whole script fails as the tool is not able to identify the element. To get rid of this issue, a very powerful option is Relational API and anchoring an element.

Let us take the following example where we have to identify the quantity field against “Ruby for Rails”.

Relational API

Relational API

If we identify the field in isolation, the Sahi Accessor reads:
What if three more book titles are added before Ruby for Rails. The Accessor for the field against Ruby for Rails also changes. To avoid this situation, let us anchor the label ‘Ruby for Rails’ first and then identify the field against it.

Identify Ruby for Rails field

Identify Ruby for Rails field

Anchor the field

Anchor the field

Identify the quantity field

Identify the quantity field

How does recording with ‘Enable Accessor Repository’ on help?
If ‘Enable Accessor Repository’ checkbox is checked, Accessor Repository file text field appears. Once the user clicks on ‘Record’ button, Sahi will record using Accessor Repository. In the ‘Accessor Repository File’ text field, we mention the relative or absolute path to the Accessor Repository file.

Accessor Repository

Accessor Repository

Once a Accessor Repository file is chosen, an element will always be identified first by its AR entry, even when not in recording mode. This prevents the same element from being identified in multiple ways in different scripts.

When elements have been smartly identified using Relational APIs like _near or _under once, newer recordings on those pages will simply use the AR entry, so other users do not need to worry about doing it again or wrongfully using a different identification mechanism.

One important thing to note with Accessor Repository keys is that they must always be in the format $_ALL_IN_CAPS. If this is not followed, the scripts will still work fine, but the values will not come up properly in Script Editor’s context panel.

Any other questions in your mind?

Feel free to comment or ask your question(s) and we will get back to you.

Sahi Pro – Cheat Sheet for Download

Posted by | Sahi, tutorials | No Comments

When we start using powerful products like Sahi Pro, we look forward to using it to the fullest. Sahi Pro has many APIs that can help us with our testing. Some of the categories of APIs include
– File APIs
– Action APIs
– Fetch APIs
– Script Execution Control APIs
– Masking APIs

We have come up with a cheat sheet to help you refer to the different APIs at a glance. Download the cheat sheet from the below links:


Let us know what you think about the cheat sheet. Is it useful? Have we missed anything? Feel free to talk to us at support[at]sahipro[dot]com

Parsing XML in Sahi scripts

Posted by | features, Sahi, tutorials, XML | 2 Comments
Sahi uses Rhino as its javascript engine and Rhino has excellent support for handling XML.
Below is a script which reads and asserts XML nodes and attributes. The example has been picked from so that it is easy to experiment with the ibm examples in this script.

var xmlStr = '' +
'' +
' ' +
' Ant' +
' Shaggy' +
' Blue' +
' 176' +
' +
' ' +
' Paul' +
' Spiky' +
' Grey' +
' 178' +
' +

var $x = new XML(xmlStr);
_assertEqual("Ant", $x.person[0].name.toString());
_assertEqual("Grey", $x.person[1].eyes.toString());

for each (var $p in $x.person){
var $measure = $p.height.@measure.toString();
_assert($measure == "metric");
_assert($p.height > 170);
Two points to note:
1) All nodes that you access are of type xml. You will need to use toString() on them before you assert them.
2) Using @ from inside a Browser Action Function (like _click, _assert etc.) causes the script to fail because of a parsing error in Sahi’s code. So first assign it to a variable and then use it, like it has been used for $measure. This bug will be fixed in the coming release.
There is a lot more that can be done with the XML object. Have a look at these links:

Configuring Eclipse for Sahi

Posted by | eclipse, IDE, Sahi, tutorials | No Comments
Eclipse’s JSDT plugin provides syntax highlighting and verification for Javascript. It can be configured to work well with Sahi scripts too. This video goes through the different steps to configure Eclipse to work with Sahi scripts.

JSDT looks at all functions in the given source folder and can list them in content-assist. Taking advantage of this, we use a dummy definitions file called apis.sah which has all the Sahi APIs. This file, apis.sah, can be downloaded from here and needs to copied into the “scripts” folder.
JSDT is a part of the Web Platforms Toolkit.
It can be installed as a plugin or can be downloaded as a single bundle in the “Eclipse IDE for Java EE Developers (163 MB)” (For windows: This is downloadable from

Toggling Proxy on Browsers

Posted by | tutorials | One Comment

One of the pain points of using Sahi is to remember to set the proxy on the browser while testing and removing it later.

There are a few tools which alleviate this well.


Use Proxy Button to toggle the proxy in one click from the Firefox toolbar.

Internet Explorer:

Use ProxyPal to toggle proxy from the IE toolbar.

Use /tools/toggle_IE_proxy.exe to toggle proxy.

Command Line:
toggle_IE_proxy.exe enable
toggle_IE_proxy.exe disable


Full ant file

Use fully-loaded Sahi Pro FREE for a month. Download Now Request a Demo