Sahi Pro - Fetch APIs
abstract
While working on scripts, we often need to fetch content from the web page.
Examples may be:
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
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.
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 Pro: 3.5Since Sahi OS: 3
_getValue($element)
Arguments
$element | HTML DOM element | Form element whose value we need |
Details
Returns the value as a string
For getting text out of div or span like elements, use _getText
Returns the value as a string
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 Pro: 3.5Since Sahi OS: 2
_getText($el)
Arguments
$el | HTML DOM element | Element whose text we need |
Details
Returns the text content as a string
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.
Returns the text content as a string
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"));
_getOptions
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_getOptions($el[, $type])
Arguments
$el | HTML DOM element | Select element |
$type | string optional | If "value" is passed, option values are returned. If anything else is passed, the visible texts of the options are returned. If type is not passed, the visible texts of the options are returned. |
Details
Returns an array containing the values or texts of all options of the select element.
If we want the values of all the options of the selectbox:
Returns an array containing the values or texts of all options of the select 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);
_getCellText
Since Sahi Pro: 3.5Since Sahi OS: 2
_getCellText($el)
Arguments
$el | HTML DOM element | Cell (TD) element whose text we need |
Details
Returns the text content as a string
Returns the text content as a string
dangerDEPRECATED:
Use
Use
_getText(element)
instead._getSelectedText
Since Sahi Pro: 3.5Since Sahi OS: 2
_getSelectedText($el)
Arguments
$el | HTML DOM element | Select dropdown element whose selected text we need |
Details
Returns the text content of the selected option of a select dropdown as a string
Returns the text content of the selected option of a select dropdown as a string
// 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.
_getSelectionText returns the text that the user has selected on a page.
_getAttribute
Since Sahi Pro: 3.5Since Sahi OS: 3
_getAttribute($el, $attributeName)
Arguments
$el | HTML DOM element | Element whose attribute we need |
$attributeName | string | The DOM attribute to be fetched |
Details
Returns the attribute as a string.
Returns the attribute as a string.
// 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
the
Fortunately there are not many such differences.
For example,
the
class
attribute in HTML is accessed as className
in javascriptthe
for
attribute of labels in HTML is accessed as htmlFor
in javascriptFortunately there are not many such differences.
_exists
Since Sahi Pro: 3.5Since Sahi OS: 3
_exists($el)
Arguments
$el | HTML DOM element | Element to check |
Details
Returns true if element exists on the page.
Returns true if element exists on the page.
// 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.
Elements may exist but not be visible to the end user.
For testing, _isVisible is a better check than _exists.
_areEqual
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_areEqual($expected, $actual)
Arguments
$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. |
Details
Compares two Javascript data type or arrays. Returns true if the expected and actual values match, else false.
Compares two Javascript data type or arrays. Returns true if the expected and actual values match, else false.
// 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.
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 Pro: 3.5Since Sahi OS: 2
_isVisible($el[, $checkZIndex[, $doScroll]])
Arguments
$el | HTML DOM element | Element to check |
$checkZIndex | boolean 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. |
$doScroll | boolean 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. |
Details
Returns true if element is visible, else false.
Returns true if element is visible, else false.
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"));
_containsText
Since Sahi Pro: 3.5Since Sahi OS: 2
_containsText($element, $text)
Arguments
$element | el | Element where we need to check if text is present |
$text | string | Text to check for. Can also be a regular expression |
Details
Returns boolean true or false.
Returns boolean true or false.
<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 Pro: 3.5Since Sahi OS: 2
_containsHTML($element, $html)
Arguments
$element | el | Element where we need to check if text is present |
$html | string | HTML to check for. Can also be a regular expression |
Details
Returns boolean true or false.
Returns boolean true or false.
<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 Pro: 3.5Since Sahi OS: 2
_contains($parent, $child)
Arguments
$parent | HTML DOM element | Parent element |
$child | HTML DOM element | Child element |
Details
Returns true if $child is a child of $parent
Returns true if $child is a child of $parent
<div id="d1">
<span>inside</span>
</div>
_contains(_div("d1"), _span("inside")) //=true
_title
Since Sahi Pro: 3.5Since Sahi OS: 3
_title()
Arguments
None |
Details
Returns the title of the top most page (the title visible on the browser)
Returns the title of the top most page (the title visible on the browser)
var $title = _title(); // returns "Sahi Pro - Fetch APIs" for this page.
_getTableContents
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_getTableContents($tableEl[, $columns[, $rows[, $count]]])
Arguments
$tableEl | HTML DOM element | Table element |
$columns | object optional | Array of column identifiers. The identifiers can be index, text or regular expression of text |
$rows | object optional | Can be a regular expression, an array of indexes or the starting index |
$count | integer optional | 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. |
Details
Returns the data from the table as a 2 dimensional array
Returns the data from the table as a 2 dimensional array
// 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 Pro: 3.5Since Sahi OS: 2
_style($el, $styleAttribute)
Arguments
$el | HTML DOM element | element whose style attribute we need |
$styleAttribute | string | style attribute to be fetched |
Details
Returns the style attribute value as string
Returns the style attribute value as string
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.
_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 Pro: 3.5Since Sahi OS: 2
_position($el[, $relative])
Arguments
$el | HTML DOM element | element whose position we need |
$relative | boolean optional | If true, returns position relative to current viewport. Default is false. |
Details
Returns 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.
Returns 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 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 Pro: 6.1.0Since Sahi OS: NA
_positionNative($el)
Arguments
$el | HTML DOM element | element whose position we need |
Details
Returns 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.
Returns 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 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 Pro: 5.1.0Since Sahi OS: 5.0
_getSelectionText($el)
Arguments
$el | object | Element in which to look for selection. Need not be specified as Sahi will automatically look in all frames and elements. |
Details
Returns the text selected by the user (using mouse drag, like we select before copy paste)
Returns the text selected by the user (using mouse drag, like we select before copy paste)
// 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.
_getSelectedText returns the text of the selected option in a select dropdown box.
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 Pro: 6.0.0Since Sahi OS: NA
_userAgent()
Arguments
None |
Details
Returns the userAgent string of the current browser.
Returns the userAgent string of the current browser.
info
Useful for testing a responsive website. Refer to _getScreenSize
_getScreenSize
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_getScreenSize()
Arguments
None |
Details
Returns the screen size as an array of two values. The first value is the inner width of the browser window and the second value is the inner height.
Returns the screen size as an array of two values. The first value is the inner width of the browser window and the second value is the inner height.
// 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.
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 Pro: 3.5Since Sahi OS: 3
_isIE()
Arguments
None |
Details
Returns true if the current browser is Internet Explorer
Returns true if the current browser is Internet Explorer
_isIE9
Since Sahi Pro: 3.5.2Since Sahi OS: 3.5
_isIE9()
Arguments
None |
Details
Returns true if the current browser is Internet Explorer 9
Returns true if the current browser is Internet Explorer 9
_isIE10
Since Sahi Pro: 4.5.2Since Sahi OS: 5.0
_isIE10()
Arguments
None |
Details
Returns true if the current browser is Internet Explorer 10
Returns true if the current browser is Internet Explorer 10
_isEdge
Since Sahi Pro: 6.2.0Since Sahi OS: NA
_isEdge()
Arguments
None |
Details
Returns true if the current browser is Edge
Returns true if the current browser is Edge
_isFF
Since Sahi Pro: 3.5Since Sahi OS: 3
_isFF()
Arguments
None |
Details
Returns true if the current browser is Firefox
Returns true if the current browser is Firefox
_isFF3
Since Sahi Pro: 3.5Since Sahi OS: 3
_isFF3()
Arguments
None |
Details
Returns true if the current browser is Firefox 3
Returns true if the current browser is Firefox 3
_isFF4
Since Sahi Pro: 3.5.2Since Sahi OS: 3.5
_isFF4()
Arguments
None |
Details
Returns true if the current browser is Firefox 4
Returns true if the current browser is Firefox 4
_isChrome
Since Sahi Pro: 3.5Since Sahi OS: 3
_isChrome()
Arguments
None |
Details
Returns true if the current browser is Chrome
Returns true if the current browser is Chrome
_isSafari
Since Sahi Pro: 3.5Since Sahi OS: 3
_isSafari()
Arguments
None |
Details
Returns true if the current browser is Safari
Returns true if the current browser is Safari
_isOpera
Since Sahi Pro: 3.5Since Sahi OS: 3.5
_isOpera()
Arguments
None |
Details
Returns true if the current browser is Opera
Returns true if the current browser is Opera
_isPhantomJS
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_isPhantomJS()
Arguments
None |
Details
Returns true if the current browser is PhantomJS
Returns true if the current browser is PhantomJS
_isHTMLUnit
Since Sahi Pro: 3.5Since Sahi OS: 3.5
_isHTMLUnit()
Arguments
None |
Details
Returns true if the current browser is HTMLUnit
Returns true if the current browser is HTMLUnit
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 Pro: 3.5Since Sahi OS: 3
_fetch($expression)
Arguments
$expression | DOM expression or String | Expression which we wish to evaluate and fetch. |
Details
Returns the value of expression
Returns the value of expression
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.
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.
It is NOT required for assigning constants etc.
$x = _fetch(10) // wrong
_set
Since Sahi Pro: 3.5Since Sahi OS: 2
_set($variable, $expression)
Arguments
$variable | variable | Variable into which we wish to get the specific value. |
$expression | DOM Expression | Expression which we wish to evaluate and set. |
Details
Sets the value of expression into variable
_set communicates with the browser to evaluate the expression and fetches the result.
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.
It is NOT required for assigning constants etc.
_set($x, 10) // wrong
_condition
Since Sahi Pro: 3.5Since Sahi OS: 3
_condition($expression)
Arguments
$expression | DOM expression | Expression which we wish to evaluate. |
Details
Returns true or false
Returns true or false
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
It is NOT required for comparing constants etc. like
$x == 10
Multiple Elements
_collect
Since Sahi Pro: 3.5Since Sahi OS: 3.5
_collect($apiType, $identifier[, $relations ...])
Arguments
$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. |
Details
Returns an array of element stubs of all elements of type apiType matching the identifier within relations
Returns an array of element stubs of all elements of type apiType 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 textboxes
for (var $i=0; $i<$textboxes.length; $i++) {
_setValue($textboxes[$i], "value");
}
_count
Since Sahi Pro: 3.5Since Sahi OS: 3.5
_count($apiType, $identifier[, $relations ...])
Arguments
$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. |
Details
Returns count of all elements of type apiType matching the identifier within relations
Returns count of all elements of type apiType 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;
_collectAttributes
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
_collectAttributes($apiType, $identifier, $attribute[, $relations ...])
Arguments
$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. |
Details
Returns an array of element attributes of all elements of type apiType matching the identifier within relations
Fetching a simple attribute from multiple elements
Fetching one of few simple attributes from multiple elements
Fetching some synthesized/processed values from multiple elements by executing an inline function for each element
Fetching some synthesized/processed values from multiple elements by executing a defined function for each element
Fetching some synthesized/processed values from multiple elements by executing nested functions
Returns an array of element attributes of all elements of type apiType 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-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 Pro: 6.2.1Since Sahi OS: NA
_collectElementsInfo($apiType[, $relations ...])
Arguments
$apiType | string | Type of elements to collect. Can be regular expression. Eg. "_link" , "_button" , "/text/" etc. |
$relations ... | relations optional | Relations like _in, _near etc. |
Details
Returns array of Accessor Info object of all elements of type apiType within relations
Returns array of Accessor Info object of all elements of type apiType 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);
}