Sahi Documentation

Fetch APIs

abstract While working on scripts, we often need to fetch content from the web page.

Examples may be:
  1. Fetching and storing a generated userId for later use in script
  2. Getting the value of form field for verification
  3. Getting the text from a UI element for assertion
  4. Getting some particular attribute of a UI element, like href of a link
info Sahi's script is executed on in the proxy JVM and not in the browser.
Only steps needed to be executed on the browser are sent to the browser.
Fetch APIs work by first sending a command to evaluate the given expression on the browser and then another call is made internally to read the value.

_getValue

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.17.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_getValue($element)

Arguments
$elementHTML DOM element Form element whose value we need

Returns
stringValue of the element

Details

info This API only works on elements which take user text input.
// Using in an assertion
_assertEqual("Tyto", _getValue(_textbox("company")));

// Fetching and storing value
var $userId = _getValue(_textbox("userId"));


For getting text out of div or span like elements, use _getText


_getText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_getText($el)

Arguments
$elHTML DOM element Element whose text we need

Returns
stringText content of the element

Details

The returned string is normalized for different browsers. Multiple continuous spaces are replaced with single space.
Multiple continuous newlines are replaced with single newline. Newlines are always returned as \n even on
Mac and Linux, so that scripts can work across browsers and OSes.

This API uses the textContent or innerText attributes of elements.

// Using in an assertion
_assertEqual("Tyto", _getText(_div("companyDiv")));

// Fetching and storing value
var $userId = _getText(_div("userIdDiv"));


_datalistFilter

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
9.2.0NANANANANANA

Available for modes: Browser

_datalistFilter($el[, $filter])

Arguments
$elHTML DOM element Input or datalist element
$filterstring optional Data to filter (case-insensitive).

Returns
HTML DOM element

Details

Specifies the filter data on input element which filters the list. This api should be used with _getOptions. Eg. _getOptions(_datalistFilter(_datalist("country_list"), "ind")); returns array with all options matching with "ind" in the list.


_getOptions

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.17.5.0NA9.0.0NA

Available for modes: Browser | Windows | Java | SAP

_getOptions($el[, $type])

Arguments
$elHTML DOM element Select/ Input or associated datalist to the input element
$typestring optional Type can be specified as "value" or "text".
If the type is specified as "text" then text contents of all the options are returned.
If the type is specified as "value" then values of all the options are returned.
If the type is not specified then text contents of all the options are returned.

Returns
array of stringsArray containing the values or texts of all options of the select or datalist element

Details

If we want the values of all the options of the selectbox:
var $list = _getOptions(_select("select_id"), "value");
_assertEqual("Value of First Option", $list[0]);
_assertEqual("Value of Second Option", $list[1]);
_assertEqual(2, $list.length);
For getting visible text of all options:
var $list = _getOptions(_select("select_id"));
_assertEqual("Visible Text of First Option", $list[0]);
_assertEqual("Visible Text of Second Option", $list[1]);
_assertEqual(2, $list.length);
For getting text or values for datalist(datalist support is only available from Sahi Pro v9.2.0):
var $list = _getOptions(_datalist("country_list"));
_assertEqual(["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa"], $list);
$list = _getOptions(_textbox("country"), "text");
_assertEqual(["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa"], $list);
For getting filtered text or values for datalist using _datalistFilter:
var $list = _getOptions(_datalistFilter(_datalist("country_list"), "AL"));
_assertEqual(["Aland Islands", "Albania", "Algeria"], $list);
$list = _getOptions(_datalistFilter(_textbox("country"), "AL"), "text");
_assertEqual(["Aland Islands", "Albania", "Algeria"], $list);


_getCellText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.1NANANANA

Available for modes: Browser

_getCellText($el)

Arguments
$elHTML DOM element Cell (TD) element whose text we need

Returns
stringText content of the cell element

Details

Returns the text content as a string
dangerDEPRECATED: Use _getText(element) instead.


_getSelectedText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.0.0NA9.0.0NA

Available for modes: Browser | Windows | Java | SAP

_getSelectedText($el)

Arguments
$elHTML DOM element Select dropdown element whose selected text we need

Returns
stringText content of the selected option of a select dropdown

Details

// Using in an assertion
_assertEqual("18", _getSelectedText(_select("legalAge")));

// Fetching and storing value
var $legalAge = _getSelectedText(_select("legalAge"));
warning _getSelectedText returns the text of the selected option in a select dropdown box.
_getSelectionText returns the text that the user has selected on a page.


_getAttribute

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.17.5.07.5.09.0.0NA

Available for modes: Browser | Windows | Java (8.0.0) | Android | iOS | SAP

_getAttribute($el, $attributeName)

Arguments
$elHTML DOM element Element whose attribute we need
$attributeNamestring The DOM attribute to be fetched

Returns
stringThe attribute value

Details

// Using in an assertion
_assertEqual("http://sahi.co.in/", _getAttribute(_link("Home"), "href"));

// Fetching and storing value
var $href = _getAttribute(_link("Home"), "href");
warning Some attributes may have different names in javascript and HTML. Always use the javascript attribute name.
For example,
the class attribute in HTML is accessed as className in javascript
the for attribute of labels in HTML is accessed as htmlFor in javascript
Fortunately there are not many such differences.


_exists

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.17.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_exists($el)

Arguments
$elHTML DOM element Element to check

Returns
booleantrue if element exists on the page, else false

Details

// Using in an assertion
_assertTrue(_exists(_link("Home")));
// same as
_assertExists(_link("Home")));

// Fetching and storing value
var $exists = _exists(_link("Home"));
info _exists() checks for existence of the element in the DOM. Elements may exist but not be visible to the end user.
For testing, _isVisible is a better check than _exists.


_areEqual

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.17.5.0NA9.0.0NA

Available for modes: Browser | Windows | Java | SAP

_areEqual($expected, $actual)

Arguments
$expectedany Expected value.
This value can be any normal javascript data type or an array
Regular expressions can also be used. (eg. "/del/")
$actualany Actual value.
This value can be any normal javascript data type or an array.
It is mostly some attribute of a HTML DOM element.

Returns
booleantrue if the expected and actual values match, else false

Details

Compares two Javascript data type or arrays.
// Compare string with regular expression.
var $compare1 = _areEqual("/^a.*c$/", "abc"); // $compare1 is true.

// Compare text of an element with regular expression.
var $compare2 = _areEqual("/created/", _getText(_div("result")));

// Compare arrays.
$exp = [/[a-z]+$/, "b"];
$act = ["ab", "b"];
var $compareArr = _areEqual($exp, $act)); // $compareArr is true.
info _assertEqual allows a regular expression in the expected value. But it cannot be used in an if condition.
Instead one could use _areEqual in the if condition to execute the desired code and _assert(false) can be used in the else block to indicate failure.
The advantage of using _areEqual instead of a direct equal to (==) comparison is that _areEqual allows the use of regular expression in the expected value.



_isVisible

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.0.07.5.09.0.010.0.0

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_isVisible($el[, $checkZIndex[, $doScroll]])

Arguments
$elHTML DOM element Element to check
$checkZIndexboolean optional If true, checks whether the element is on top or not(Checks whether some other element with a higher Z index is not hiding it). If false, omits this check.
Default is false.
$doScrollboolean optional If true, checks whether element is the top element by scrolling to the element. If false, checks only for the current viewport.
Default is false. Used only when checkZIndex is true.

Returns
booleantrue if element is visible, else false

Details

info In 6.0.0, $checkZIndex and $doScroll have been newly added. Scripts written before 6.0.0 do NOT need changes.
// Using in an assertion
_assertTrue(_isVisible(_link("Home"))); // doesn't check z-index
_assertTrue(_isVisible(_link("Home"), true)); //checks z-index
_assertTrue(_isVisible(_link("Home"), true, true)); //checks z-index after scrolling

// Fetching and storing value
var $visible = _isVisible(_link("Home"));


_isChecked

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
7.0.0NA7.0.17.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS (8.0.0) | SAP

_isChecked($el)

Arguments
$elHTML DOM element Element to check

Returns
booleantrue if element is checked, else false

Details

// Using in an assertion
_assertTrue(_isChecked(_radio("Male")));


_isEnabled

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
7.5.0NANA7.5.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_isEnabled($element)

Arguments
$elementelement element to be check

Returns
booleantrue if element is enabled, else false

Details

// Using in an assertion
_assertTrue(_isEnabled(_button("Download")));


_containsText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.5.07.5.09.0.0NA

Available for modes: Browser | Windows | Java (8.0.0) | iOS | SAP

_containsText($element, $text)

Arguments
$element el Element where we need to check if text is present
$textstring Text to check for. Can also be a regular expression

Returns
booleanRetruns true if element contains the text, else false

Details

<div id="d1" style="background-color:yellow"><i>Formatted</i> Text</div>
_containsText(_div("d1"), "Formatted") //=true
_containsText(_div("d1"), "Text") //=true
_containsText(_div("d1"), "Formatted Text") //=true
_containsText(_div("d1"), "/Format.*Text/") //=true
_containsText(_div("d1"), "/mat.*Te/") //=true
_containsText(_div("d1"), "Non existent") //=false


_containsHTML

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.1NANANANA

Available for modes: Browser

_containsHTML($element, $html)

Arguments
$element el Element where we need to check if text is present
$htmlstring HTML to check for. Can also be a regular expression

Returns
booleantrue if element contains the HTML, else false

Details

<div id="d1" style="background-color:yellow"><i>Formatted</i> Text</div>
_containsHTML(_div("d1"), "<i>Formatted</i>") //=true
_containsHTML(_div("d1"), "Text") //=true
_containsHTML(_div("d1"), "<i>Formatted</i> Text") //=true
_containsText(_div("d1"), "/<i>.*</i> Text/") //=true
_containsHTML(_div("d1"), "Formatted Text") //=false
_containsHTML(_div("d1"), "Non existent") //=false


_contains

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.5.0NANANA

Available for modes: Browser | Windows | Java (8.0.0)

_contains($parent, $child)

Arguments
$parentHTML DOM element Parent element
$childHTML DOM element Child element

Returns
booleanRetruns true if the specified child element is a child of the specified parent element, else false

Details

<div id="d1">
  <span>inside</span>
</div>
_contains(_div("d1"), _span("inside")) //=true


_title

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_title()

Arguments
None

Returns
stringTitle of the top most page (the title visible on the browser)

Details

var $title = _title(); // returns "Sahi Pro - Fetch APIs" for this page.

_getTableContents

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.18.0.0NANANA

Available for modes: Browser | Java

_getTableContents($tableEl[, $columns[, $rows[, $count]]])

Arguments
$tableElHTML DOM element Table element
$columnsarray of objects optionalArray of column identifiers. The identifiers can be index, text or regular expression of text
$rowsstring|integer|array of integers optionalCan be a regular expression, an array of indexes or the starting index
$countinteger optionalIf rows is the starting index, count is the ending index, If rows is a regular expression, count is the total count. If rows is an array of indexes, count is ignored.

Returns
two dimensional array of stringsThe data from the table

Details

// All table data
var $data = _getTableContents(_table("tableId"));

// All rows, but only specific columns from the table
var $data = _getTableContents(_table("tableId"), [1,3,7] );
// Returns all rows with columns 2, 4 and 8 since index is 0 based.

// All rows, but only specific columns from the table
var $data = _getTableContents(_table("tableId"), [1,"Price", "/Stock/"] );
// Returns all rows with column 2 (index is 0 based), column with header "Price" and column header matching regular expression /Stock/

// Rows containing specified keyword, but only specific columns from the table
var $data = _getTableContents(_table("tableId"), ["Price", "/Stock/"], "/pen/" );
// Returns all rows containing words matching regular expression /pen/, with columns having column header Price and column header matching
// regular expression /Stock/. Note that /pen/ is matched with all the columns of the row.

// First n rows containing specified keyword, but only specific columns from the table
var $data = _getTableContents(_table("tableId"), ["Price", "/Stock/"], "/pen/", 8);
// Returns first 8 rows contain words matching regular expression /pen/ having column header Price and column header matching
// regular expression /Stock/

// Rows having indexes specified in array, with specific columns from the table
var $data = _getTableContents(_table("tableId"), ["Price", "/Stock/"], [3,4,7]);
// Returns rows 4, 5 and 8 (index is 0 based) having all columns with header "Price" and column header matching regular expression /Stock/

// Rows from m to n, but only specific columns from the table
var $data = _getTableContents(_table("tableId"), ["Price", "/Stock/"], 3, 7 );
// Returns rows from 3 to 7 having all columns with header "Price" and column header matching regular expression /Stock/
info Prefer using _getTableContents to _collect or _collectAttributes, if you specifically need the contents of an html table (as against other collections) and you need to get the content of many such tables in a script.

_style

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.1NANANANA

Available for modes: Browser

_style($el, $styleAttribute)

Arguments
$elHTML DOM element element whose style attribute we need
$styleAttributestring style attribute to be fetched

Returns
stringThe style attribute value

Details

info Styles in HTML elements are calculated by the browser based on various CSS rules.
_style returns the computed style that is finally applicable to the element.
Accessing style directly as an attribute will not give computed style.
Always use _style instead.
// Using in assertion
_assertEqual("24px", _style(_heading1(0), "font-size"));

// Fetching and storing value
var $fontSize = _style(_heading1(0), "font-size");


_position

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.17.5.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

_position($el[, $relative])

Arguments
$elHTML DOM element element whose position we need
$relativeboolean optional If true, returns position relative to current viewport.
Default is false.

Returns
array of integersAn array [x,y] of the elements position in pixels relative to its window/frame/iframe

Details

This may be used to verify layouts by comparing positions of specific elements.
// Using in assertion
_assertEqual([10,20], _position(_image("logo")));

// Fetching and storing value
var $position = _position(_image("logo"));
_log($position[0]); //logs x coordinate (eg. 10)
_log($position[1]); //logs y coordinate (eg. 20)


_positionNative

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.1.0NANA7.5.0NANA10.0.0

Available for modes: Browser | Java

_positionNative($el)

Arguments
$elHTML DOM element element whose position we need

Returns
array of integersAn array [x,y] of the elements absolute position in pixels

Details

This may be used inside native events to find the absolute positions of elements.
// Using in assertion
var $xy =  _positionNative(_link("Link Test"));
_assertEqual(222, $xy[1]);

// Fetching and storing value
var $xy =  _positionNative(_link("Link Test"));
_log("x coordinate "+ $xy[0] ); //logs x coordinate of the element
_log("y coordinate "+ $xy[1] ); //logs y coordinate of the element


_getSelectionText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.05.07.0.17.0.0NA9.0.0NA

Available for modes: Browser | Windows | Java | SAP

_getSelectionText([$el])

Arguments
$elHTML DOM element optionalElement in which to look for selection. This is optional only for Browser Automation, as Sahi will automatically look in all frames and elements.

Returns
stringText selected by the user (using mouse drag, like we select before copy paste)

Details

// Using in an assertion
_assertEqual("Search", _getSelectionText());

// Fetching and storing value
var $currentSelectedText = _getSelectionText();
warning _getSelectionText returns the text that the user has selected on a page.
_getSelectedText returns the text of the selected option in a select dropdown box.


_getCopiedText

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
9.7.2NANANANANANA

Available for modes: Browser

_getCopiedText()

Arguments
None

Returns
stringCopied text

Details

Returns the text copied to the system clipboard by a web application.

// Using in an assertion
_assertEqual("XYZ Private Limited", _getCopiedText(_div("companyDiv")));

// Fetching and storing value
var $userId = _getCopiedText(_div("userIdDiv"));


Browser Detection APIs

info Browser detection APIs are used to identify browsers at runtime, so that specific paths in code can be chosen.

_userAgent

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.1NANANANA

Available for modes: Browser

_userAgent()

Arguments
None

Returns
stringThe userAgent string of the current browser

Details

info Useful for testing a responsive website. Refer to _getScreenSize

_getScreenSize

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.1NANANANA

Available for modes: Browser

_getScreenSize()

Arguments
None

Returns
array of integersScreen size as an array [x,y]. The first value is the inner width of the browser window and the second value is the inner height

Details

// Using in an assertion
var $screensize=_getScreenSize();
_assertTrue(($screensize[0]>=783)&&($screensize[0]<=800));
_assertTrue(($screensize[1]>=280)&&($screensize[1]<=400));
info When dealing with a responsive website, you may want to write a single script to test functionality on different devices along with the desktop.
Some parts of the website UI may be same across different devices but some parts may be different. To test the parts that are different, you would use the _getScreenSize and _userAgent APIs to identify a specific device and write specific code for that device.

_isIE

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_isIE()

Arguments
None

Returns
booleantrue if the current browser is Internet Explorer, else false

Details

_isIE9

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.5.23.57.0.1NANANANA

Available for modes: Browser

_isIE9()

Arguments
None

Returns
booleantrue if the current browser is Internet Explorer 9, else false

Details

_isIE10

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
4.5.25.07.0.1NANANANA

Available for modes: Browser

_isIE10()

Arguments
None

Returns
booleantrue if the current browser is Internet Explorer 10, else false

Details

_isEdge

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.2.0NA7.0.1NANANANA

Available for modes: Browser

_isEdge()

Arguments
None

Returns
booleantrue if the current browser is Edge, else false

Details

_isEdgeNew

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
9.0.0NANANANANANA

Available for modes: Browser

_isEdgeNew()

Arguments
None

Returns
booleantrue if the current browser is new Microsoft Edge, else false

Details

_isFF

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_isFF()

Arguments
None

Returns
booleantrue if the current browser is Firefox, else false

Details

_isFF3

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_isFF3()

Arguments
None

Returns
booleantrue if the current browser is Firefox 3, else false

Details

_isFF4

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.5.23.57.0.1NANANANA

Available for modes: Browser

_isFF4()

Arguments
None

Returns
booleantrue if the current browser is Firefox 4, else false

Details

_isChrome

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_isChrome()

Arguments
None

Returns
booleantrue if the current browser is Chrome, else false

Details

_isSafari

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_isSafari()

Arguments
None

Returns
booleantrue if the current browser is Safari, else false

Details

_isOpera

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.53.57.0.1NANANANA

Available for modes: Browser

_isOpera()

Arguments
None

Returns
booleantrue if the current browser is Opera, else false

Details

_isPhantomJS

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NA7.0.1NANANANA

Available for modes: Browser

_isPhantomJS()

Arguments
None

Returns
booleantrue if the current browser is PhantomJS, else false

Details

_isBrave

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
9.2.1NANANANANANA

Available for modes: Browser

_isBrave()

Arguments
None

Returns
booleantrue if the current browser is Brave, else false

Details



_isHTMLUnit

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.53.57.0.1NANANANA

Available for modes: Browser

_isHTMLUnit()

Arguments
None

Returns
booleantrue if the current browser is HTMLUnit, else false

Details



Rich Text Editors (Content Editable elements)

infoRefer to RTE APIs.

Generic attribute fetching mechanism

Most common attributes needed during automation are exposed via Sahi's fetch APIs.
However there are lots of DOM attributes which are not exposed.
For example the href attribute of a link is not exposed by any in-built Sahi API.
To work with such attributes, the APIs _fetch, _set and _condition are used.
info _fetch is a newer API and can be used in place of both _set and _condition.

_fetch

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_fetch($expression)

Arguments
$expressionstring|DOM expression Expression which we wish to evaluate and fetch.

Returns
stringValue of the expression

Details

info _fetch API was introduced later than _set and may be a cleaner way.
For secondary (popup) windows, _fetch can be used only with _selectWindow and not with _popup prefix.
var $rowCount = _fetch(_table("tableId").rows.length);

// You can also use an expression as a string
var $userAgent = _fetch("navigator.userAgent");
// And fetch results of custom utility functions of your web app
var $value = _fetch("jQuery('#id').val()");



// While working with popup windows use _selectWindow first
_selectWindow("popupWin");
// Fetch table row count from popupWin window
var $rowCount = _fetch(_table("tableId").rows.length);
warning _fetch is only required for fetching DOM attributes from the browser.
It is NOT required for assigning constants etc.
$x = _fetch(10) // wrong


_set

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.527.0.1NANANANA

Available for modes: Browser

_set($variable, $expression)

Arguments
$variablevariable Variable into which we wish to get the specific value.
$expressionDOM Expression Expression which we wish to evaluate and set.

Returns
null

Details

Sets the value of expression into variable
_set communicates with the browser to evaluate the expression and fetches the result.
warning _fetch may be a better way than _set to fetch values from browser.
// Fetching href attribute
var $href = null; // initialize variable
_set($href, _link("Home").href); // set it

// Getting row count of table
var $rowCount = 0; // initialize variable
_set($rowCount, _table("tableId").rows.length); // set it

// Getting column count of table (first row)
var $colCount = 0; // initialize variable
_set($colCount, _table("tableId").rows[0].cells.length); // set it


// Working with popup windows using _popup prefix
var $colCount = 0;
_popup("popupWin")._set($colCount, _table("tableId").rows[0].cells.length); // CORRECT
_set($colCount, _popup("popupWin")._table("tableId").rows[0].cells.length); // WRONG

// Better to use with _selectWindow
var $colCount = 0;
_selectWindow("popupWin"); // Select window first
_set($colCount, _table("tableId").rows[0].cells.length);
warning _set is only required for fetching DOM attributes from the browser.
It is NOT required for assigning constants etc.
_set($x, 10) // wrong


_condition

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.537.0.1NANANANA

Available for modes: Browser

_condition($expression)

Arguments
$expressionDOM expression Expression which we wish to evaluate.

Returns
booleantrue or false based on the expression

Details

if (_condition(_link(0).href == "http://sahi.co.in/")){
    _click(_link(0));
}

// Note that this is the same as

var $href = _fetch(_link(0).href);
if ($href == "http://sahi.co.in/"){
    _click(_link(0));
}

// or

var $href = null;
_set($href, _link(0).href);
if ($href == "http://sahi.co.in/"){
    _click(_link(0));
}


// For popup windows use _selectWindow and then use _condition

_selectWindow("popupWin"); // select Window
// Check condition on popup window
if (_condition(_link(0).href == "http://sahi.co.in/")){
    _click(_link(0));
}
warning _condition is only required for fetching DOM attributes from the browser. It is NOT required for comparing constants etc. like $x == 10


Multiple Elements

_collect

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.53.57.0.17.5.07.5.0NANA

Available for modes: Browser | Windows | Java | Android | iOS

_collect($elementType, $identifier[, $relations])

Arguments
$elementTypestring type of elements to collect. Eg. "_link", "_button", "/text/" etc.
$identifierstring Sahi Identifier. Can also be just an index.
$relationsrelations optional Relations like _in, _near etc.

Returns
array of element stubsArray of all elements of the specified type matching the identifier within relations

Details

// Collect all textboxes matching any identifier, in table "listing".
// Note the use of match all regular expression "/.*/"
var $textboxes = _collect("_textbox", "/.*/", _in(_table("listing"));
// Iterate and set values on all textboxes
for (var $i=0; $i<$textboxes.length; $i++) {
  _setValue($textboxes[$i], "value");
}


_count

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.53.57.0.17.5.07.5.0NANA

Available for modes: Browser | Windows | Java | Android | iOS

_count($elementType, $identifier[, $relations])

Arguments
$elementTypestring type of elements to collect. Eg. "_link", "_button", "/text/" etc.
$identifierstring|object Sahi Identifier/Attributes JSON. Can also be just an index.
$relationsrelations optional Relations like _in, _near etc.

Returns
integerCount of all elements of the specified type matching the identifier within relations

Details

// Count all textboxes matching any identifier, in table "listing".
// Note the use of match all regular expression "/.*/"
var $textboxCount = _count("_textbox", "/.*/", _in(_table("listing")); // may return 5;
// or more explicitly
var $textboxCount = _count("_textbox", {name:"q"});


_collectAttributes

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANA7.5.07.5.0NANA

Available for modes: Browser | Windows | Android | iOS | Java (8.0.0)

_collectAttributes($elementType, $identifier, $attribute[, $relations])

Arguments
$elementTypestring type of elements to collect.
Eg. "_link", "_button", "/text/" etc.
$identifierstring Sahi Identifier. Can also be just an index.
$attributestring|function attribute or function.
$relationsrelations optional Relations like _in, _near etc.

Returns
li_sArray of element attributes of all elements of the specified type matching the identifier within relations

Details

Fetching a simple attribute from multiple elements
// Collect the id attribute of all textboxes in table "listing".
// Note the use of match all regular expression "/.*/"
var $textboxIds = _collectAttributes("_textbox", "/.*/", "id", _in(_table("listing")));

// Collect text of divs whose className is menu-item
var $menuTexts = _collectAttributes("_div", "menu-item", "sahiText");
// or more explicitly
var $menuTexts = _collectAttributes("_div", {className:"menu-item"}, "sahiText");


Fetching one of few simple attributes from multiple elements
// Return name or id of textboxes. If name is found return name, else id.
// Note the use of single pipe |.
var $identifiers = _collectAttributes("_textbox", "/.*/", "name|id");


Fetching some synthesized/processed values from multiple elements by executing an inline function for each element

// For each div of className menu-item, function will be called and the current div will be passed as $el.
var $htmls = _collectAttributes("_div", "menu-item",  function ($el) {return $el.innerHTML;}, _in(_div("navbar")));


Fetching some synthesized/processed values from multiple elements by executing a defined function for each element

// getInnerHTML will be called for each element whose attribute we are collecting.
function getInnerHTML($el) {
  return $el.innerHTML;
}

// For each div of className menu-item, getInnerHTML function will be called and the current div will be passed as $el.
var $htmls = _collectAttributes("_div", "menu-item",  getInnerHTML, _in(_div("navbar")));


Fetching some synthesized/processed values from multiple elements by executing nested functions

// Notice the use of browser tag for the second level function called from getHTML.
<browser>
function getInnerHTMLOfParent(el){
  return el.parentNode.innerHTML;
}
</browser>

// This is not inside a browser tag.
function getHTML(el) {
  // calls another function.
  // The called function should be inside a browser tag,
  // because of the way Sahi serializes content between proxy and browser.
  return getInnerHTMLOfParent(el);
}
var $htmls = _collectAttributes("_div", "menu-item",  getHTML, _in(_div("navbar")));


_collectElementsInfo

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.2.1NANA7.5.07.5.0NANA

Available for modes: Browser | Windows | Java (8.0.0)

_collectElementsInfo($elementType[, $relations])

Arguments
$elementTypestring Type of elements to collect. Can be regular expression.
Eg. "_link", "_button", "/text/" etc.
$relationsrelations optional Relations like _in, _near etc.

Returns
array of objectsArray of Accessor Info object of all elements of the specified type within relations

Details

var $accs1 = _collectElementsInfo("/text/"); // Collect all _textbox and _textarea accessor info.
// Collect all _radio accessor infos in div "modes".
var $accs = _collectElementsInfo("_radio", _in(_div("modes")));
/*
$accs = [
		{"api":"_radio", "identifiers":{"id":"sahiradio", "index":0, "name":"mode", "value":"on"}, "tag":"INPUT"},
		{"api":"_radio", "identifiers":{"id":"javaradio", "index":1, "name":"mode[1]", "value":"on[1]"}, "tag":"INPUT"},
		{"api":"_radio", "identifiers":{"id":"rubyradio", "index":2, "name":"mode[2]", "value":"on[2]"}, "tag":"INPUT"}
	]
*/
for (var $i=0; $i<$accs.length; $i++) {
	var $obj = $accs[$i];
	var $identifiers = $obj["identifiers"];
	var $el = $obj["api"] + "(\"" + $identifiers["id"] + "\")";
	_log($el);
}