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
_include
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
5.0 | NA | 7.0.1 | 7.0.0 | 7.5.0 |
Available for modes: Browser | Windows | Java | Android | iOS
_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 |
Returns
null |
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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
5.0 | NA | 7.0.1 | 7.0.0 | 7.5.0 |
Available for modes: Browser | Windows | Java | Android | iOS
_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 |
Returns
null |
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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
3.5 | 3.5 | 7.0.1 | 7.0.0 | 7.5.0 |
Available for modes: Browser | Windows | Java | Android | iOS
_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 |
Returns
null |
_resource
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
6.1.0 | N/A | NA | 7.0.0 | 7.5.0 |
Available for modes: Browser | Windows | Java | Android | iOS
_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. Can be folder path. Wildcard * is also supported. |
Returns
null |
Details
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.
infoNOTE: $resourcePath was added Since Sahi Pro: 6.1.1. For old document Refer here
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");
/* Folder structure:
|- a
|- a1.xlsx
|- a2.xls
|- b
|-b1.png
|- c
|- sample1.txt
|- d
|- sample2.txt
|
|
|
*/
// To include all files and folder inside folder "a" as resource.
_resource("../a"); // Includes "../a/a1.xlsx", "../a/a2.xls", "../a/b/b1.png", "../a/b/c/sample1.txt" and "../a/b/d/sample2.txt"
// To include all files inside folder "a" as resource.
_resource("../a/*"); // Includes "../a/a1.xlsx" and "../a/a2.xls"
_resource("../a/*.xls"); // Includes "../a/a2.xls"
_resource("../a/*/*/sample1.txt"); // Includes "../a/b/c/sample1.txt"
_resource("../a/b/*/sample*.txt"); // Includes "../a/b/c/sample1.txt" and "../a/b/d/sample2.txt"
_importJava
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
7.5.0 | NA | NA | 7.5.0 | NA |
Available for modes: Browser | Windows | Java
_importJava($fullyQualifiedJavaClassName)
Arguments
$fullyQualifiedJavaClassName | string | Fully Qualified Java Class Name. |
Returns
null |
Details
- Creates a new instance of the Java Class and sets it into a presumed variable name.
Variable name will be in this format:
$<className>.<functionName>
. First letter of the java class will be in lower case. For example: If the class name isUserModule
, instance variable name will be$userModule
. - Using
"*"
, new instance is created for all the classes, defined inside the package. Methods of these classes can be invoked using the presumed variable name.
_importJava("demo.training.UserModule");
//using the instance variable, methods of that class can be invoked.
$userModule.login("test", "secret");
_importJava("demo.training.*");
//using the instance variable, methods of that class can be invoked.
$userModule.login("test", "secret");
_loadJavaInstance
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On |
7.5.0 | NA | NA | 7.5.0 | NA |
Available for modes: Browser | Windows | Java
_loadJavaInstance($fullyQualifiedJavaClassName)
Arguments
$fullyQualifiedJavaClassName | string | Fully Qualified JavaClass Name. |
Returns
Object | instance variable to access methods of class. |
Details
_loadJavaInstance returns the class instance.
_loadJavaInstance returns the class instance.
$userModuleInstance = _loadJavaInstance("demo.training.UserModule");
//using this instance variable, methods of that class can be invoked.
$userModuleInstance.login("test", "secret");
info
Difference between
_importJava
and _loadJavaInstance
:
- _importJava presumes the variable of the specific name while _loadJavaInstance allows to create custom variable name, to access methods of the java class.
- "*" is not supported in _loadJavaInstance.