Utility APIs

_random

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

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

_random($maxNumber)

Arguments
$maxNumberinteger maximum number

Returns
integerRandom number between 0 and maxNumber

Details

warning Two calls to _random() may return the same number. DO NOT use it for uniqueness.


_extract

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
4.3NA7.0.17.0.07.5.09.0.0

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

_extract($str, $pattern[, $onlyGroups])

Arguments
$strstring String to find pattern in
$patternstring Regular expression as native regex (/a/) or as a string ("/a/")
$onlyGroupsboolean optional if true, returns only the groups matched.

Returns
stringContent from a string if there is only one matched pattern
array of stringsArray of matched pattern from the specified string

Details

This API helps extract specific content from strings.

Example
// To get value "250" from "Rs. 250"
_extract("Rs. 250", "/Rs. (.*)/", true); // returns "250"
_extract("Rs. 250", "/Rs. (.*)/"); // returns an array ["Rs. 250", "250"]


// Multiple groups with and without onlyGroups
var $str = "The traffic light changed from red to green";
var $pattern = "/from (.*) to (.*)/";

var $extracted = _extract($str, $pattern);
// $extracted now has the overall match "from red to green" and the individual groups "red" and "green"
_assertEqual(["from red to green", "red", "green"], $extracted);

// Setting onlyGroups=true, we do not get back the overall match.
var $extracted = _extract($str, $pattern, true);
// $extracted now has only the individual groups "red" and "green"
_assertEqual(["red", "green"], $extracted);


_trim

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
5.1.0NA7.0.17.0.07.5.09.0.0

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

_trim($arg)

Arguments
$argstring|array of strings|two dimensional array of strings String or 1-d or 2-d array.

Returns
stringtrimmed string if $arg is a string
array of stringstrimmed array if $arg is an array
two dimensional array of stringstrimmed 2 dimensional array if $arg is a 2 dimensional array

Details

var $s = " sahi pro  ";
$s = _trim($s);
_assertEqual("sahi pro", $s);

var $arr = ["",,,"sahi", "pro",,,"",,];
$arr = _trim($arr);
_assertEqual("sahi", $arr[0]);
_assertEqual("pro", $arr[1]);
_assertEqual(2, $arr.length);

var $array2D = [[],[],[],["","",""],["abc", "xyz", "c"], ["1", "2", "3"], ["1", "2", "3"], ["1", "2", "3"], ["1", "2", "3"],[],[""],[]];
$array2D = _trim($array2D);
_assertEqual("xyz", $array2D[0][1]);
_assertEqual(5, $array2D.length);

_toJSON

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
6.0.0NA7.0.17.0.07.5.09.0.0

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

_toJSON($object)

Arguments
$object JSON Object JSON to convert to string.

Returns
stringJSON string

Details

It converts a JSON object to String.

Example
var $libObj = {
  "name":"John Johnson",
  "street":"Oslo West 16",
  "age":33,
  "phone":"555 1234567"};
// Gives string corresponding to json object.
var $jsonString = _toJSON($libObj);

_encrypt

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

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

_encrypt($password)

Arguments
$passwordstring password to be encrypted

Returns
stringEncrypted password string

Details

Example
var $pass = _encrypt("secret");
_setPassword(_password("password"), $pass);
info Salt is used to encrypt the password.
You can change the salt by changing the property script.salt_for_password_encryption in userdata/config/userdata.propertiesfile.
infoIf script.salt_for_password_encryption property is present in userdata/config/userdata_hidden.properties as well, then this property will be considered only from userdata_hidden.propertiesfile.

_getSessionId

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
7.5.0NANANANA9.0.0

Available for modes: Browser

_getSessionId([$windowIdentifier])

Arguments
$windowIdentifierstring optionalthe windowIdentifier can be the window name, the window title, the window url or a regular expression of any of these.

Returns
array of stringsArray of sessionId

Details

Example
var $sessionId = _getSessionId("Sahi Start");
$sessionId = _getSessionId("/Sahi/");
$sessionId = _getSessionId("/Sahi/[0]");
$sessionId = _getSessionId();