Sahi Pro - Include APIs
To reuse a function from one script in another, we include the relevant script using the include APIs.
You may also like to see Including Sahi script globally
You may also like to see Including Sahi script globally
_include
Since Sahi Pro: 5.0Since Sahi OS: NA
_include($scriptPath)
Arguments
$scriptPath | string | File path of sahi script. If scriptPath is a relative path, it is evaluated relative to including scripts's path |
Details
Includes the script at scriptPath in the current script.
_include is dynamically evaluated since Sahi Pro V5.0.
Includes the script at scriptPath in the current script.
_include is dynamically evaluated since Sahi Pro V5.0.
// Suppose the current script is in
// D:/sahi/userdata/scripts/demo/ folder
// include lib.sah located in the current script's directory
// D:/sahi/userdata/scripts/demo/lib.sah
_include("lib.sah"); // using relative path
// Include using full path
_include("D:/sahi/userdata/scripts/demo/lib.sah"); // using absolute path
// Use of back and front slashes
_include("D:\sahi\userdata\scripts\demo\lib.sah"); // WRONG
_include("D:/sahi/userdata/scripts/demo/lib.sah"); // CORRECT
_include("D:\\sahi\\userdata\\scripts\\demo\\lib.sah"); // CORRECT
// Include from a subfolder
// D:/sahi/userdata/scripts/demo/common/lib.sah
_include("common/lib.sah");
// Include from a parent's sub folder
// D:/sahi/userdata/scripts/common/lib.sah
_include("../common/lib.sah");
// Using a variable
var $includePath = "../common/lib.sah";
_include($includePath);
_includeOnce
Since Sahi Pro: 5.0Since Sahi OS: NA
_includeOnce($scriptPath)
Arguments
$scriptPath | string | File path of sahi script. If scriptPath is a relative path, it is evaluated relative to including scripts's path |
Details
Similar to _include but _includeOnce only includes the script once even when called multiple times.
This is useful in a scenario where during development a single flow is broken
into multiple scripts and each is developed and tested independently and then
included all together into one script.
Similar to _include but _includeOnce only includes the script once even when called multiple times.
This is useful in a scenario where during development a single flow is broken
into multiple scripts and each is developed and tested independently and then
included all together into one script.
_includeOnce("lib.sah"); // includes lib.sah
_includeOnce("lib.sah"); // second call. Will do nothing.
// For more details on usage, look at _include
_dynamicInclude
Since Sahi Pro: 3.5Since Sahi OS: 3.5
_dynamicInclude($scriptPath)
Arguments
$scriptPath | string | File path of sahi script. If scriptPath is a relative path, it is evaluated relative to including scripts's path |
_resource
Since Sahi Pro: 6.1.0Since Sahi OS: N/A
_resource($resourcePath)
Arguments
$resourcePath | string | File path of the resource files such as image, excel, csv etc which are used inside script. If resourcePath is a relative path, it is evaluated relative to the script that includes it. |
Details
In a distributed run, only the participating scripts in the suite are zipped and sent over to the nodes.
Hence if your scripts refer to any resources like CSV files (through CSV APIs)
or Excel files (through Excel APIs) or images etc, they have to be explicitly included in your
scripts using the _resource API.
Please use relative path for resources so that they can work on all the nodes.
In summary, any resource that your script uses (other than scripts themselves), needs to be included through _resource APIs.
Some examples:
In a distributed run, only the participating scripts in the suite are zipped and sent over to the nodes.
Hence if your scripts refer to any resources like CSV files (through CSV APIs)
or Excel files (through Excel APIs) or images etc, they have to be explicitly included in your
scripts using the _resource API.
Please use relative path for resources so that they can work on all the nodes.
In summary, any resource that your script uses (other than scripts themselves), needs to be included through _resource APIs.
info
Distributed runs involve multiple nodes and Sahi installation folders could be at different locations on these nodes. Hence it is advisable to use relative paths for resources, so that they are resolved correctly on all nodes.
Some examples:
_resource("../excel/users.xlsx"); // Include excel file as resource.
var $data = _readExcelFile("../excel/users.xlsx");
_resource("snapshot_login_short.png");
_navigateTo("http://sahitest.com/demo/training");
_setValue(_textbox("user"), "test");
var $same = _compareImages("snapshot_login_short.png", "snapshot_login_short.png", 10);
_assert($same);