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
Section titled “Include”Modes BrowserWindowsJavaAndroidiOSSAP
Includes the script at scriptPath in the current script. Include is dynamically evaluated since Sahi Pro V5.0.
Parameters
| Name | Type | Description |
|---|---|---|
$scriptPath | string | File path of sahi script. If scriptPath is a relative path, it is evaluated relative to files folder of the current project. |
Raw script
// Suppose the current script is in// D:/sahi/userdata/scripts/sahitests/ folder
// include lib.sah located in the current script's directory// D:/sahi/userdata/scripts/sahitests/lib.sah_include("lib.sah"); // using relative path
// Include using full path_include("D:/sahi/userdata/scripts/sahitests/lib.sah"); // using absolute path
// Use of back and front slashes_include("D:\sahi\userdata\scripts\sahitests\lib.sah"); // WRONG_include("D:/sahi/userdata/scripts/sahitests/lib.sah"); // CORRECT_include("D:\\sahi\\userdata\\scripts\\sahitests\\lib.sah"); // CORRECT
// Include from a subfolder// D:/sahi/userdata/scripts/sahitests/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 variablevar $includePath = "../common/lib.sah";_include($includePath);Sahi Pro Classic API _include
Include Once
Section titled “Include Once”Modes BrowserWindowsJavaAndroidiOSSAP
Similar to _include but Include Once 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.
Parameters
| Name | Type | Description |
|---|---|---|
$scriptPath | string | File path of sahi script. If scriptPath is a relative path, it is evaluated relative to files folder of the current project. |
Raw script
_includeOnce("lib.sah"); // includes lib.sah_includeOnce("lib.sah"); // second call. Will do nothing.
// For more details on usage, look at _includeSahi Pro Classic API _includeOnce
Resource
Section titled “Resource”Modes BrowserWindowsJavaAndroidiOSSAP
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.
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.
Parameters
| Name | Type | Description |
|---|---|---|
$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 files folder of the current project. Can be folder path. Wildcard * is also supported. |
Raw script
// Some examples:_resource("../excel/users.xlsx"); // Include excel file as resource.var $data = _readExcelFile("../excel/users.xlsx");Sahi Pro Classic API _resource
Import Java
Section titled “Import Java”Modes BrowserWindowsJavaAndroidiOSSAP
- 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.
Parameters
| Name | Type | Description |
|---|---|---|
$fullyQualifiedJavaClassName | string | Fully Qualified Java Class Name. |
Raw script
_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");Sahi Pro Classic API _importJava
Load Java Instance
Section titled “Load Java Instance”Modes BrowserWindowsJavaAndroidiOSSAP
Load Java Instance returns the class instance.
Parameters
| Name | Type | Description |
|---|---|---|
$fullyQualifiedJavaClassName | string | Fully Qualified JavaClass Name. |
Returns
Object | instance variable to access methods of class. |
Raw script
$userModuleInstance = _loadJavaInstance("demo.training.UserModule");//using this instance variable, methods of that class can be invoked.$userModuleInstance.login("test", "secret");Sahi Pro Classic API _loadJavaInstance

