Fetch Functions
While working on scripts, we often need to fetch content from the application screen. Examples may be: Fetching and storing a generated userId for later use in script Getting the value of form field for verification Getting the text from a UI element for assertion Getting some particular attribute of a UI element, like href of a link
_getValue
Section titled “_getValue”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_getValue($element)Arguments
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Form element whose value we need |
Returns
string | Value of the element |
// Using in an assertion_assertEqual("Tyto", _getValue(_textbox("company")));
// Fetching and storing valuevar $userId = _getValue(_textbox("userId"));For getting text out of div or span like elements, use _getText
_getText
Section titled “_getText”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_getText($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Element whose text we need |
Returns
string | Text content of the element |
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 function uses the textContent or innerText attributes of elements.
// Using in an assertion_assertEqual("Tyto", _getText(_div("companyDiv")));
// Fetching and storing valuevar $userId = _getText(_div("userIdDiv"));_datalistFilter
Section titled “_datalistFilter”- Since
- Sahi Pro 9.2.0
Modes Browser
_datalistFilter($el[, $filter])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Input or datalist element |
$filter | stringoptional | Data to filter (case-insensitive). |
Returns
HTML DOM element |
Specifies the filter data on input element which filters the list. This function should be used with _getOptions. Eg. _getOptions(_datalistFilter(_datalist("country_list"), "ind")); returns array with all options matching with “ind” in the list.
_getOptions
Section titled “_getOptions”- Since
- Sahi Pro 6.0.0
- Desktop Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaSAP
_getOptions($el[, $type])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Select/ Input or associated datalist to the input element |
$type | stringoptional | 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 strings | Array containing the values or texts of all options of the select or datalist element |
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 (Deprecated)
Section titled “_getCellText (Deprecated)”- Since
- Sahi Pro 3.5
Modes Browser
_getCellText($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Cell (TD) element whose text we need |
Returns
string | Text content of the cell element |
Returns the text content as a string
_getSelectedText
Section titled “_getSelectedText”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaSAP
_getSelectedText($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Select dropdown element whose selected text we need |
Returns
string | Text content of the selected option of a select dropdown |
Returns the text of the selected option in a select dropdown box.
// Using in an assertion_assertEqual("18", _getSelectedText(_select("legalAge")));
// Fetching and storing valuevar $legalAge = _getSelectedText(_select("legalAge"));_getAttribute
Section titled “_getAttribute”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJava (8.0.0)AndroidiOSSAP
_getAttribute($el, $attributeName)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Element whose attribute we need |
$attributeName | string | The attribute to be fetched |
Returns
string | The attribute value |
Returns the specified attribute’s value for the element.
// Using in an assertion_assertEqual("http://sahi.co.in/", _getAttribute(_link("Home"), "href"));
// Fetching and storing valuevar $href="_getAttribute(_link(""Home"), "href");_exists
Section titled “_exists”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_exists($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Element to check |
Returns
boolean | true if element exists on the page, else false |
It checks for existence of the element in the application. However, note that elements may exist but not be visible on the user interface.
// Using in an assertion_assertTrue(_exists(_link("Home")));// same as_assertExists(_link("Home")));
// Fetching and storing valuevar $exists = _exists(_link("Home"));_areEqual
Section titled “_areEqual”- Since
- Sahi Pro 6.0.0
- Desktop Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaSAP
_areEqual($expected, $actual)Arguments
| Name | Type | Description |
|---|---|---|
$expected | any | Expected value. This value can be any normal javascript data type or an array Regular expressions can also be used. (eg. "/del/") |
$actual | any | 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
boolean | true if the expected and actual values match, else false |
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._isVisible
Section titled “_isVisible”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
- Visual Assist Engine 10.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_isVisible($el[, $checkZIndex[, $doScroll]])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Element to check |
$checkZIndex | booleanoptional | 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. |
$doScroll | booleanoptional | 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
boolean | true if element is visible, else false |
// 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 valuevar $visible = _isVisible(_link("Home"));_isChecked
Section titled “_isChecked”- Since
- Sahi Pro 7.0.0
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOS (8.0.0)SAP
_isChecked($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | Element to check |
Returns
boolean | true if element is checked, else false |
Returns true if the element is checked. This is applicable for radio buttons and checkboxes.
// Using in an assertion_assertTrue(_isChecked(_radio("Male")));_isEnabled
Section titled “_isEnabled”- Since
- Sahi Pro 7.5.0
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_isEnabled($element)Arguments
| Name | Type | Description |
|---|---|---|
$element | element | element to be check |
Returns
boolean | true if element is enabled, else false |
Some elements in your application could be disabled. This function returns true if the specified element is enabled.
// Using in an assertion_assertTrue(_isEnabled(_button("Download")));_containsText
Section titled “_containsText”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJava (8.0.0)iOSSAP
_containsText($element, $text)Arguments
| Name | Type | Description |
|---|---|---|
$element | el | Element where we need to check if text is present |
$text | string | Text to check for. Can also be a regular expression |
Returns
boolean | true if element contains the text, else false |
Returns true if the element contains the specified text.
<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
Section titled “_containsHTML”- Since
- Sahi Pro 3.5
Modes Browser
_containsHTML($element, $html)Arguments
| Name | Type | Description |
|---|---|---|
$element | el | Element where we need to check if text is present |
$html | string | HTML to check for. Can also be a regular expression |
Returns
boolean | true if element contains the HTML, else false |
Returns true if the element contains the specified HTML content.
<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
Section titled “_contains”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
Modes BrowserWindowsJava (8.0.0)
_contains($parent, $child)Arguments
| Name | Type | Description |
|---|---|---|
$parent | HTML DOM element | Parent element |
$child | HTML DOM element | Child element |
Returns
boolean | true if the specified child element is a child of the specified parent element, else false |
<div id="d1"> <span>inside</span></div>_contains(_div("d1"), _span("inside")) //=true_title
Section titled “_title”- Since
- Sahi Pro 3.5
Modes Browser
_title()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
string | Title of the top most page (the title visible on the browser) |
var $title = _title(); // returns "Sahi Pro - Fetch APIs" for this page._getTableContents
Section titled “_getTableContents”- Since
- Sahi Pro 6.0.0
- Desktop Engine 8.0.0
Modes BrowserJava
_getTableContents($tableEl[, $columns[, $rows[, $count]]])Arguments
| Name | Type | Description |
|---|---|---|
$tableEl | HTML DOM element | Table element |
$columns | array of objectsoptional | Array of column identifiers. The identifiers can be index, text or regular expression of text |
$rows | string|integer|array of integersoptional | Can be a regular expression, an array of indexes or the starting index |
$count | integeroptional | If 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 strings | The data from the table |
// All table datavar $data = _getTableContents(_table("tableId"));
// All rows, but only specific columns from the tablevar $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 tablevar $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 tablevar $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 tablevar $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 tablevar $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 tablevar $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/_style
Section titled “_style”- Since
- Sahi Pro 3.5
Modes Browser
_style($el, $styleAttribute[, $pseudoEl])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | element whose style attribute we need |
$styleAttribute | string | style attribute to be fetched |
$pseudoEl | stringoptional | Use this argument to fetch style attribute from pseudo-elements like ::before, ::after, etc. |
Returns
string | The style attribute value |
// Using in assertion_assertEqual("24px", _style(_heading1(0), "font-size"));
// Fetching and storing valuevar $fontSize = _style(_heading1(0), "font-size");
// Fetch style of a pseudo elementvar $backgroundColor = _style(_listItem("entry1"), "background-color", ":before")_position
Section titled “_position”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_position($el[, $relative])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | element whose position we need |
$relative | booleanoptional | If true, returns position relative to current viewport. Default is false. |
Returns
array of integers | An array [x,y] of the elements position in pixels relative to its window/frame/iframe |
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 valuevar $position = _position(_image("logo"));_log($position[0]); //logs x coordinate (eg. 10)_log($position[1]); //logs y coordinate (eg. 20)_positionNative
Section titled “_positionNative”- Since
- Sahi Pro 6.1.0
- Desktop Engine 7.5.0
- Visual Assist Engine 10.0.0
Modes BrowserJava
_positionNative($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | element whose position we need |
Returns
array of integers | An array [x,y] of the elements absolute position in pixels |
This may be used inside native events to find the absolute positions of elements.
// Using in assertionvar $xy = _positionNative(_link("Link Test"));_assertEqual(222, $xy[1]);
// Fetching and storing valuevar $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_dimensions
Section titled “_dimensions”- Since
- Sahi Pro 11.5.0
- Desktop Engine 11.5.0
- Mobile Engine 11.5.0
- SAP Engine 11.5.0
Modes BrowserWindowsJavaAndroidiOSSAP
_dimensions($el)Arguments
| Name | Type | Description |
|---|---|---|
$el | Element | element whose dimensions we need |
Returns
array of integers | An array [w,h] of the element's dimensions in pixels |
This function gets the width and height as an array for the specified element.
// Using in assertion_assertEqual([100,40], _dimensions(_image("logo")));
// Fetching and storing valuevar $dimensions = _dimensions(_image("logo"));_log($dimensions[0]); //logs width (eg. 100)_log($dimensions[1]); //logs height (eg. 40)_getSelectionText
Section titled “_getSelectionText”- Since
- Sahi Pro 5.1.0
- Desktop Engine 7.0.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaSAP
_getSelectionText([$el])Arguments
| Name | Type | Description |
|---|---|---|
$el | HTML DOM elementoptional | Element in which to look for selection. This is optional only for Browser Automation, as Sahi will automatically look in all frames and elements. |
Returns
string | Text selected by the user (using mouse drag, like we select before copy paste) |
// Using in an assertion_assertEqual("Search", _getSelectionText());
// Fetching and storing valuevar $currentSelectedText = _getSelectionText();_getCopiedText
Section titled “_getCopiedText”- Since
- Sahi Pro 10.0.0
Modes Browser
_getCopiedText()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
string | Copied text |
Returns the text copied to the system clipboard by a web application.
_lockWindow();_focusWindow();_clickNative(_button("Copy Text to Clipboard"));_unlockWindow();var $copiedText = _getCopiedText();_assertEqual("Copied text", $copiedText);Browser Detection Functions
Section titled “Browser Detection Functions”_userAgent
Section titled “_userAgent”- Since
- Sahi Pro 6.0.0
Modes Browser
_userAgent()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
string | The userAgent string of the current browser |
_getScreenSize
Section titled “_getScreenSize”- Since
- Sahi Pro 6.0.0
Modes Browser
_getScreenSize()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
array of integers | Screen 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 |
// Using in an assertionvar $screensize=_getScreenSize();_assertTrue(($screensize[0]>=783)&&($screensize[0]<=800));_assertTrue(($screensize[1]>=280)&&($screensize[1]<=400));- Since
- Sahi Pro 3.5
Modes Browser
_isIE()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Internet Explorer, else false |
_isIE9
Section titled “_isIE9”- Since
- Sahi Pro 3.5.2
Modes Browser
_isIE9()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Internet Explorer 9, else false |
_isIE10
Section titled “_isIE10”- Since
- Sahi Pro 4.5.2
Modes Browser
_isIE10()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Internet Explorer 10, else false |
_isEdge
Section titled “_isEdge”- Since
- Sahi Pro 6.2.0
Modes Browser
_isEdge()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Edge, else false |
_isEdgeNew (Deprecated)
Section titled “_isEdgeNew (Deprecated)”- Since
- Sahi Pro 9.0.0
Modes Browser
_isEdgeNew()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is new Microsoft Edge, else false |
- Since
- Sahi Pro 3.5
Modes Browser
_isFF()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Firefox, else false |
_isFF3
Section titled “_isFF3”- Since
- Sahi Pro 3.5
Modes Browser
_isFF3()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Firefox 3, else false |
_isFF4
Section titled “_isFF4”- Since
- Sahi Pro 3.5.2
Modes Browser
_isFF4()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Firefox 4, else false |
_isChrome
Section titled “_isChrome”- Since
- Sahi Pro 3.5
Modes Browser
_isChrome()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Chrome, else false |
_isSafari
Section titled “_isSafari”- Since
- Sahi Pro 3.5
Modes Browser
_isSafari()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Safari, else false |
_isOpera
Section titled “_isOpera”- Since
- Sahi Pro 3.5
Modes Browser
_isOpera()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Opera, else false |
_isPhantomJS
Section titled “_isPhantomJS”- Since
- Sahi Pro 6.0.0
Modes Browser
_isPhantomJS()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is PhantomJS, else false |
_isBrave
Section titled “_isBrave”- Since
- Sahi Pro 9.2.1
Modes Browser
_isBrave()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is Brave, else false |
_isHTMLUnit
Section titled “_isHTMLUnit”- Since
- Sahi Pro 3.5
Modes Browser
_isHTMLUnit()Arguments
| Name | Type | Description |
|---|---|---|
None |
Returns
boolean | true if the current browser is HTMLUnit, else false |
Rich Text Editors (Content Editable elements)
Section titled “Rich Text Editors (Content Editable elements)”Generic attribute fetching mechanism
Section titled “Generic attribute fetching mechanism”Most common attributes needed during automation are exposed via Sahi’s fetch functions.
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 function.
To work with such attributes, the functions _fetch, _set and _condition are used.
_fetch
Section titled “_fetch”- Since
- Sahi Pro 3.5
Modes Browser
_fetch($expression)Arguments
| Name | Type | Description |
|---|---|---|
$expression | string|DOM expression | Expression which we wish to evaluate and fetch. |
Returns
string | Value of the expression |
var $rowCount = _fetch(_table("tableId").rows.length);
// You can also use an expression as a stringvar $userAgent = _fetch("navigator.userAgent");// And fetch results of custom utility functions of your web appvar $value = _fetch("jQuery('#id').val()");
// While working with popup windows use _selectWindow first_selectWindow("popupWin");// Fetch table row count from popupWin windowvar $rowCount = _fetch(_table("tableId").rows.length);- Since
- Sahi Pro 3.5
Modes Browser
_set($variable, $expression)Arguments
| Name | Type | Description |
|---|---|---|
$variable | variable | Variable into which we wish to get the specific value. |
$expression | DOM Expression | Expression which we wish to evaluate and set. |
Returns
null |
Sets the value of expression into variable
_set communicates with the browser to evaluate the expression and fetches the result.
// Fetching href attributevar $href="null;" // initialize variable_set($href, _link("Home").href); // set it
// Getting row count of tablevar $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 prefixvar $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 _selectWindowvar $colCount = 0;_selectWindow("popupWin"); // Select window first_set($colCount, _table("tableId").rows[0].cells.length);_condition
Section titled “_condition”- Since
- Sahi Pro 3.5
Modes Browser
_condition($expression)Arguments
| Name | Type | Description |
|---|---|---|
$expression | DOM expression | Expression which we wish to evaluate. |
Returns
boolean | true or false based on the expression |
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 windowif (_condition(_link(0).href="=" "http://sahi.co.in/")){ _click(_link(0));}Multiple Elements
Section titled “Multiple Elements”_collect
Section titled “_collect”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
Modes BrowserWindowsJavaAndroidiOS
_collect($elementType, $identifier[, $relations])Arguments
| Name | Type | Description |
|---|---|---|
$elementType | string | type of elements to collect. Eg. "_link", "_button", "/text/" etc. |
$identifier | string | Sahi Identifier. Can also be just an index. |
$relations | relationsoptional | Relations like _in, _near etc. |
Returns
array of element stubs | Array of all elements of the specified type matching the identifier within relations |
// 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 textboxesfor (var $i=0; $i<$textboxes.length; $i++) { _setValue($textboxes[$i], "value");}_count
Section titled “_count”- Since
- Sahi Pro 3.5
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
Modes BrowserWindowsJavaAndroidiOS
_count($elementType, $identifier[, $relations])Arguments
| Name | Type | Description |
|---|---|---|
$elementType | string | type of elements to collect. Eg. "_link", "_button", "/text/" etc. |
$identifier | string|object | Sahi Identifier/Attributes JSON. Can also be just an index. |
$relations | relationsoptional | Relations like _in, _near etc. |
Returns
integer | Count of all elements of the specified type matching the identifier within relations |
// 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 explicitlyvar $textboxCount = _count("_textbox", {name:"q"});_collectAttributes
Section titled “_collectAttributes”- Since
- Sahi Pro 5.1.0
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
Modes BrowserWindowsAndroidiOSJava (8.0.0)
_collectAttributes($elementType, $identifier, $attribute[, $relations])Arguments
| Name | Type | Description |
|---|---|---|
$elementType | string | type of elements to collect. Eg. "_link", "_button", "/text/" etc. |
$identifier | string | Sahi Identifier. Can also be just an index. |
$attribute | string|function | attribute or function. |
$relations | relationsoptional | Relations like _in, _near etc. |
Returns
li_s | Array of element attributes of all elements of the specified type matching the identifier within relations |
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-itemvar $menuTexts = _collectAttributes("_div", "menu-item", "sahiText");// or more explicitlyvar $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
Section titled “_collectElementsInfo”- Since
- Sahi Pro 6.2.1
- Desktop Engine 7.5.0
- Mobile Engine 7.5.0
Modes BrowserWindowsJava (8.0.0)
_collectElementsInfo($elementType[, $relations])Arguments
| Name | Type | Description |
|---|---|---|
$elementType | string | Type of elements to collect. Can be regular expression. Eg. "_link", "_button", "/text/" etc. |
$relations | relationsoptional | Relations like _in, _near etc. |
Returns
array of objects | Array of Accessor Info object of all elements of the specified type within relations |
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);}
