Script Execution Control APIs
abstract
These APIs control the way a script executes
_setMode
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
7.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_setMode([$applicationType])
Arguments
$applicationType | string optional | Mode or Application Type to set. If not specified, application type BROWSER is assumed. |
Returns
null |
Sahi Pro Flowcharts Action :Set Mode
Details
_setMode tells the script to start executing further steps on a different mode (application type). Look at Sahi Pro multiple modes for available modes.
_setMode tells the script to start executing further steps on a different mode (application type). Look at Sahi Pro multiple modes for available modes.
_setMode("BROWSER"); // Further steps in script will be sent to browser.
_setMode("WINDOWS"); // Further steps in script will be sent to Windows applications.
_wait
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_wait($timeout[, $condition])
Arguments
$timeout | integer | time in milliseconds to wait for |
$condition | Sahi expression optional | condition to wait for. If specified, _wait will return if either the condition is met or if the time elapsed has exceeded timeout, whichever comes first. |
Returns
null |
Sahi Pro Flowcharts Action :Wait
Details
Forces script to wait for given time or given condition to be true, which ever comes first
Forces script to wait for given time or given condition to be true, which ever comes first
_wait(1000); // Will stop execution for a second
// Wait till div by id "ajaxConfirm" is populated for max 5 seconds.
_wait(5000, _getText(_div("ajaxConfirm"))!="");
// Wait till button becomes visible
_wait(5000, _isVisible(_button("Confirm")));
// Wait till "Loading ..." message disappears
_wait(1000); // may take a second before that message appears.
_wait(5000, !_isVisible(_div("Loading ..."))); // wait max 5 seconds for it to disappear.
_setXHRReadyStatesToWaitFor
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.3.2 | NA | NA | NA | NA |
Available for modes: Browser
_setXHRReadyStatesToWaitFor($readyStates[, $forURL])
Arguments
$readyStates | string | Comma separated ready states e.g "1,2,3" |
$forURL | string optional | URL for which given ready states should be set. URL pattern can also be specified here using regular expression. |
Returns
null |
Sahi Pro Flowcharts Action :Set XHR Ready States To Wait For
Details
Sets what readyStates of an XMLHttpRequest (XHR) Sahi should wait for. Sahi normally monitors all AJAX requests and proceeds only if the XHR readyState is 4. In some applications which use long polling or comet requests, there may be XHRs in readyState 1. Sahi would wait for a long time and then timeout in such cases.
Sets what readyStates of an XMLHttpRequest (XHR) Sahi should wait for. Sahi normally monitors all AJAX requests and proceeds only if the XHR readyState is 4. In some applications which use long polling or comet requests, there may be XHRs in readyState 1. Sahi would wait for a long time and then timeout in such cases.
//Setting _setXHRReadyStatesToWaitFor to "2,3" will allow Sahi to proceed if the readyStates are either 1 or 4.
_setXHRReadyStatesToWaitFor("2,3");
//Setting _setXHRReadyStatesToWaitFor to "1,2,3" for the window URL that matches regular expression /formTest/.
_setXHRReadyStatesToWaitFor("1,2,3","/formTest/");
info
Alternately, you may also modify the default behaviour for Sahi as follows:
-
Default XHR ready states to wait for can be modified in
xhr.wait_ready_states
property present insahi/userdata/config/userdata.properties file. -
URLs and their corresponding default XHR ready states to wait for can be added in
sahi/userdata/config/xhr_ready_states_wait_for_urls.txt file as shown below :1,2,3:/formTest/ 2,3:/link/
_byPassWaitMechanism
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.1.0 | NA | NA | NA | NA |
Available for modes: Browser
_byPassWaitMechanism($enable[, $url[, $urlType]])
Arguments
$enable | boolean | If true, Sahi Pro's automatic waiting mechanism will be bypassed. |
$url | string optional | Specific URL to be bypassed. URL pattern can also be specified here using regular expression. |
$urlType | string optional | Type of the url can be 'xhr' or 'fetch'. |
Returns
null |
Sahi Pro Flowcharts Action :By Pass Wait Mechanism
Details
Sahi Pro automatically waits for page loads and ajax activity. To achieve this, Sahi Pro monitors the URL requests triggered from your application using Javascript's Fetch API as well as XMLHttpRequest(XHR).
When
When
Your application may trigger URLs using Javascript's Fetch API as well as XMLHttpRequest(XHR). Thus, these URL requests can be termed to be of type 'fetch' or 'xhr'. Type of a URL request can be observed from the network logs in the browser's Developer Tools.
Parameters
Sample Script:
Sahi Pro automatically waits for page loads and ajax activity. To achieve this, Sahi Pro monitors the URL requests triggered from your application using Javascript's Fetch API as well as XMLHttpRequest(XHR).
When
$enable
parameter is set to true, Sahi Pro's automatic waiting mechanism will not be used. It will be bypassed. To stop bypassing the waiting mechanism, set this parameter to false again.
// Below step will bypass all the URL requests
_byPassWaitMechanism(true);
When
$enable
parameter is set to true, all the URL requests will be bypassed. To bypass only a specific URL, specify them using the parameter $url
.
// Below step will bypass URLs that contain the string 'connect'
_byPassWaitMechanism(true, "/connect/");
Your application may trigger URLs using Javascript's Fetch API as well as XMLHttpRequest(XHR). Thus, these URL requests can be termed to be of type 'fetch' or 'xhr'. Type of a URL request can be observed from the network logs in the browser's Developer Tools.
Parameters
$url
and $urlType
can be used together to bypass a specific url of the specified type as shown in the example below.
// Below step will bypass URLs of type 'fetch' that contain the string 'ping'
_byPassWaitMechanism(true, "/ping/", "fetch");
Sample Script:
// Example 1
//start bypassing wait mechanism
_byPassWaitMechanism(true);
_setValue(_textbox("user"), "test");
_setValue(_password("pass"), "secret");
_click(_button("Login"));
//stop bypassing wait mechanism
_byPassWaitMechanism(false);
// Example 2
//start bypassing wait mechanism but ignore only specific URLs
_byPassWaitMechanism(true, "/formTest/");
_byPassWaitMechanism(true, "/connect/", "xhr");
_setValue(_textbox("user"), "test");
_setValue(_password("pass"), "secret");
_click(_button("Login"));
//stop bypassing wait mechanism
_byPassWaitMechanism(false);
info
This API should be used carefully as it will bypass the inbuilt wait mechanism of Sahi. When required, set it to true and as soon as the need is over, set it to false.
_setWaitTimes
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
7.0.0 | NA | NA | NA | NA |
Available for modes: Browser
_setWaitTimes($windowLoadTime, $ajaxLoadTime, $flexLoadTime)
Arguments
$windowLoadTime | integer | time in milliseconds. Default is 60000ms. |
$ajaxLoadTime | integer | time in milliseconds. Default is 60000ms. |
$flexLoadTime | integer | time in milliseconds. Default is 0ms. |
Returns
null |
Sahi Pro Flowcharts Action :Set Wait Times
Details
Modify the Windows, AJAX and Flex wait times. Sahi will wait for the specified time after start of every step, following which it will move on to execute the next step, even if the activity(page/ajax/flex) is under execution. Pass the value as null to keep the last defined wait time.
Modify the Windows, AJAX and Flex wait times. Sahi will wait for the specified time after start of every step, following which it will move on to execute the next step, even if the activity(page/ajax/flex) is under execution. Pass the value as null to keep the last defined wait time.
_setWaitTimes(5000, 0, 0);
_navigateTo("http://sahi.co.in/demo/training/");//Sahi will wait for 5000ms following this step to load the page.
//If the page is not loaded within 5000ms, the execution will go to next step.
_click(_submit("Login"));
_setWaitTimes(null, 7000, 0);//Sahi will not wait for flex loads if any. It will wait for 7000ms for any AJAX activity and 5000ms for page load
//following this step before moving to next step.
_setStrictVisibilityCheck
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5.2 | 7.0.0 | 10.0.0 | NA | NA |
Available for modes: Browser | Java | iOS
_setStrictVisibilityCheck($doCheck[, $checkZIndex[, $doScroll]])
Arguments
$doCheck | boolean | set to true to enable strict visibility 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. This argument is not available for Windows mode. |
$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. This argument is not available for Windows mode. |
Returns
null |
Sahi Pro Flowcharts Action :Set Strict Visibility Check
Details
Use
Use
_setStrictVisibilityCheck
with parameter $doCheck
as true to strictly look for only visible elements and ignore elements which are not visible.
// make Sahi ignore elements which are not visible.
// do operations on visible elements ...
_setStrictVisibilityCheck(true); //doesn't check z-index
_setStrictVisibilityCheck(true, true); //checks z-index
_assertExists(_textbox("textbox_id"));
_setStrictVisibilityCheck(true, true,true); //checks z-index after scrolling
_assertExists(_textbox("textbox_id"));
// make sahi revert to original behavior of considering all elements in the DOM.
_setStrictVisibilityCheck(false);
info
- This API is useful in cases where widgets are dynamically created at multiple locations but only one of them is visible at any given time.
- During recording Sahi can be forced into either mode by choosing "Strict Visibility On" or "Strict Visibility Off" from the "Other Actions:" dropdown. Make sure you "Append to Script" to add it to the recorded script.
warning
Setting _setStrictVisibilityCheck(true) can be slightly expensive as each probable element needs to be checked for visibility.
However, some applications may be automatable only if _setStrictVisibilityCheck is true. Use judiciously.
_setAutomaticBlur
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
11.0.0 | NA | NA | NA | NA |
Available for modes: Browser
_setAutomaticBlur($enable)
Arguments
$enable | boolean | set to true to enable calling blur event. |
Returns
null |
Sahi Pro Flowcharts Action :Set Automatic Blur
Details
When an action(like click, type, etc.) is performed on a HTML element, the focus gets removed from the previously focused element. Similarly, when a action (like _click, _setValue, etc.) is performed on a HTML element, Sahi Pro by default triggers a blur event on the previously focused element to remove focus from it. However, this behaviour may not be desired for some HTML elements. In such cases, disable this automatic blur using _setAutomaticBlur API.
When an action(like click, type, etc.) is performed on a HTML element, the focus gets removed from the previously focused element. Similarly, when a action (like _click, _setValue, etc.) is performed on a HTML element, Sahi Pro by default triggers a blur event on the previously focused element to remove focus from it. However, this behaviour may not be desired for some HTML elements. In such cases, disable this automatic blur using _setAutomaticBlur API.
// make Sahi to stop automatic blur.
_setAutomaticBlur(false);
_setValue(_textbox("category"), "Automation"); // for this automatic blur is not desired.
// make Sahi revert to original behavior of automatic blur.
_setAutomaticBlur(true);
info
With this API, the automatic blur effect will be disabled only for the current page. On reload or navigating to another page, this behavior will be enabled again.
_setSpeed
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_setSpeed($speed)
Arguments
$speed | integer | speed in milliseconds |
Returns
null |
Sahi Pro Flowcharts Action :Set Speed
Details
Sets the speed of execution of steps.
Sets the speed of execution of steps.
_setSpeed(2000);
//Each step will execute with a gap of 2000 milliseconds (2 seconds).
//Default execution speed is 100ms;
infoFrom 6.1.0 onwards, this API works at a script level. Calling _setSpeed in one script does not affect other scripts.
warningIn earlier versions, DO NOT modify this unless absolutely necessary. Use _wait if and where needed.
_setPingDelayAfterStep
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.3.0 | NA | NA | NA | NA |
Available for modes: Browser
_setPingDelayAfterStep($delay)
Arguments
$delay | integer | delay in milliseconds |
Returns
null |
Sahi Pro Flowcharts Action :Set Ping Delay After Step
Details
Sahi Pro continuously pings the proxy server to await commands. In some very rare cases, it may be required to silence all ping activity after execution of a step (For example, if you are monitoring CPU or browser usage statistics after a step). In such case, calling _setPingDelayAfterStep will disable pinging for the given delay time.
Sahi Pro continuously pings the proxy server to await commands. In some very rare cases, it may be required to silence all ping activity after execution of a step (For example, if you are monitoring CPU or browser usage statistics after a step). In such case, calling _setPingDelayAfterStep will disable pinging for the given delay time.
_setPingDelayAfterStep(5000);
_click(_link("Link Test")); // pinging will stop for 5000 ms after this step
_click(_link("Back")); // pinging will stop for 5000 ms after this step
_setPingDelayAfterStep(0);
_click(_link("Abc")); // Normal pinging will resume.
_stopOnError
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_stopOnError()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Stop On Error
Details
Forces script to fail and abort on any error. Further steps will not be executed. This is the default behaviour of Sahi.
Forces script to fail and abort on any error. Further steps will not be executed. This is the default behaviour of Sahi.
_stopOnError();
info
We differentiate between errors and failures.
Error: Occurs when an element on which an action is to be performed is missing.
Eg. In
_click(_link("Link Test"))
, if _link("Link test")
does not exist, it will be an error
Failure: Assertion failures are considered failures. Sahi does not stop execution on failures.
_continueOnError
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_continueOnError()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Continue On Error
Details
Forces script to continue inspite of error. Further steps will be executed.
Forces script to continue inspite of error. Further steps will be executed.
_continueOnError();
danger
Using this can be a waste of time. If button or link is missing, it is quite likely that all
further steps will also fail. We do not recommend this.
info
It may be better to use the Script Callback Function onScriptError instead.
_runUnitTests
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_runUnitTests([$testsArray])
Arguments
$testsArray | array of strings optional | Array of test function names which should be executed |
Returns
null |
Sahi Pro Flowcharts Action :Run Unit Tests
Details
Executes all functions whose name starts with "test". If testsArray is specified, runs only tests in that array. If functions setUp() and tearDown() are defined, they are executed before and after each test, irrespective of errors in the test functions.
Executes all functions whose name starts with "test". If testsArray is specified, runs only tests in that array. If functions setUp() and tearDown() are defined, they are executed before and after each test, irrespective of errors in the test functions.
function setUp(){
_log("In setUp");
}
function tearDown(){
_log("In tearDown");
}
function testAddition() {
_assertEqual(3, 1+2);
}
function testSubtraction() {
_assertEqual(3, 5-2);
}
function testMultiplication() {
_assertEqual(8, 2*4);
}
// Invoke all tests:
_runUnitTests();
// Invoke only testAddition and testMultiplication
// Use this for debugging when you are fixing just one test
// and don't want to run all the tests.
_runUnitTests(["testAddition", "testMultiplication"]);
_fail
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_fail([$message])
Arguments
$message | string optional | Message to be logged in playback logs |
Returns
null |
Sahi Pro Flowcharts Action :Fail
Details
Causes a script to stop as a failure and log the given message. Can be used in the middle of a script.
Causes a script to stop as a failure and log the given message. Can be used in the middle of a script.
info
- When called from inside a testcase, only that testcase will fail and other testcases will not be impacted.
- When called from inside try block, execution will proceed to catch block.
- onScriptFailure and onScriptError callback functions will NOT be called.
_stop
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_stop()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Stop
Details
Causes a script to stop execution.
Causes a script to stop execution.
info
- When called from inside a _testcase, the whole script execution will be stopped. Execution will not proceed to next testcase
- When called from inside try block, execution will terminate without entering the catch block.
- onScriptFailure and onScriptError callback functions will NOT be called.
_stopTestCase
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.2.1 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_stopTestCase([$message])
Arguments
$message | string optional | Message to be logged in playback logs |
Returns
null |
Sahi Pro Flowcharts Action :Stop Test Case
Details
Causes a scenario file to stop execution of a testcase. When called from inside a testcase in a scenario file, that particular testcase execution will be stopped. Execution will proceed to the next testcase.
Causes a scenario file to stop execution of a testcase. When called from inside a testcase in a scenario file, that particular testcase execution will be stopped. Execution will proceed to the next testcase.
Script Synchronization
Scripts can be forced to synchronize with each other by acquiring locks._lock
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | 9.0.0 | NA | NA | NA |
Available for modes: Browser | Java
_lock($name[, $timeout])
Arguments
$name | string | Lock name |
$timeout | integer optional | timeout |
Returns
string | Key that can be used in the _unlock API |
Sahi Pro Flowcharts Action :Lock
Details
Acquires a lock with given name. If another script calls _lock with the same name, that script will wait till this lock is unlocked or till timeout happens. If timeout is not specified, the default timeout defined by
If you wish to lock a portion of code that deals with browser windows, example: focusing a window and taking a screenshot or focusing a window and doing file uploads, use _lockWindow instead of _lock. For example usage, refer to the _lockWindow example below. Use _lock and _unlock for functionality other than dealing with windows.
Acquires a lock with given name. If another script calls _lock with the same name, that script will wait till this lock is unlocked or till timeout happens. If timeout is not specified, the default timeout defined by
sahi.lock.timeout
property in sahi.properties
is used.
(Default is 60 seconds. Can be overridden in userdata.properties)
If you wish to lock a portion of code that deals with browser windows, example: focusing a window and taking a screenshot or focusing a window and doing file uploads, use _lockWindow instead of _lock. For example usage, refer to the _lockWindow example below. Use _lock and _unlock for functionality other than dealing with windows.
_unlock
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | 9.0.0 | NA | NA | NA |
Available for modes: Browser | Java
_unlock($key)
Arguments
$key | string | Lock key returned from _lock |
Returns
null |
Sahi Pro Flowcharts Action :Unlock
Details
Unlocks or releases the lock (acquired through the _lock API) that matches the given key.
Unlocks or releases the lock (acquired through the _lock API) that matches the given key.
danger
key is NOT the name of the lock but the value returned from the _lock method.
_lockWindow
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 9.0.0 | NA | NA | NA |
Available for modes: Browser | Windows | Java
_lockWindow([$timeout])
Arguments
$timeout | long optional | timeout |
Returns
null |
Sahi Pro Flowcharts Action :Lock Window
Details
This is a more specific version of the _lock API.
Acquires a lock with a name internal to Sahi. The same lock is used by Sahi when launching new browser windows.
If another script calls _lockWindow, that script will wait till this _lockWindow is unlocked or till timeout happens. If timeout is not specified, the default timeout defined by
This is a more specific version of the _lock API.
Acquires a lock with a name internal to Sahi. The same lock is used by Sahi when launching new browser windows.
If another script calls _lockWindow, that script will wait till this _lockWindow is unlocked or till timeout happens. If timeout is not specified, the default timeout defined by
sahi.lock.timeout
property in sahi.properties
is used.
(Default is 60 seconds. Can be overridden in userdata.properties)
info
In 5.1.0, _lock API was introduced.
As part of taking screenshots, one would first need to call _focusWindow. The user could use _lock to lock a portion of code that called _focusWindow and then a Screenshot API. This is to ensure that while one script takes the screenshot of its browser, other scripts will wait before taking screenshots.
However, Sahi itself launches browser windows to run scripts of a suite. Since this is not controlled through _lock, a Sahi browser window may get opened while a script is attempting to take a screenshot. This would end up affecting the screenshot.
To avoid this problem, we have introduced the _lockWindow API. Whenever Sahi internally opens/manages browser windows, it calls _lockWindow to lock the window related code.
Users should always use _lockWindow when managing any code related to windows, example: focusing the window and taking screenshots, focusing the window and do a native file upload, etc.
As part of taking screenshots, one would first need to call _focusWindow. The user could use _lock to lock a portion of code that called _focusWindow and then a Screenshot API. This is to ensure that while one script takes the screenshot of its browser, other scripts will wait before taking screenshots.
However, Sahi itself launches browser windows to run scripts of a suite. Since this is not controlled through _lock, a Sahi browser window may get opened while a script is attempting to take a screenshot. This would end up affecting the screenshot.
To avoid this problem, we have introduced the _lockWindow API. Whenever Sahi internally opens/manages browser windows, it calls _lockWindow to lock the window related code.
Users should always use _lockWindow when managing any code related to windows, example: focusing the window and taking screenshots, focusing the window and do a native file upload, etc.
_unlockWindow
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 9.0.0 | NA | NA | NA |
Available for modes: Browser | Windows | Java
_unlockWindow()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Unlock Window
Details
Unlocks or releases the lock acquired by calling _lockWindow.
Used in conjunction with _lockWindow.
Unlocks or releases the lock acquired by calling _lockWindow.
Used in conjunction with _lockWindow.
Suppose we have 2 scripts: one for taking a screenshot and another for fileupload via native events.
// Script 1
_focusWindow();
_takeScreenShot();
// Script 2
_focusWindow();
// file upload via native events
Both these scripts need their browsers to be in focus. How ever when run in a suite, the sequence may become like this:
_focusWindow(); // script 1
_focusWindow(); // script 2: brings second window in focus
_takeScreenShot(); // script 1: will take screenshot of window 2 which is WRONG
To synchronize these, whenever focus is being sought, we may acquire a lock and then release the lock only when our operation is complete.
Since we are dealing with windows, call _lockWindow instead of _lock so that windows opened by Sahi internally are also managed correctly.
//Script 1
_lockWindow();
_focusWindow();
_takeScreenShot();
_unlockWindow();
//Script 2
_lockWindow();
_focusWindow();
// do file upload stuff
_unlockWindow();
Now the sequence would be:
_lockWindow(); // script 1
_focusWindow(); // script 1
_lockWindow(); // script 2: will block here.
_takeScreenShot(); // script 1
_unlockWindow(); // script 1
_focusWindow(); // script 2
// do file upload stuff // script 2
_unlockWindow(); // script 2
Callback functions
Sahi provides a few hooks or callback functions which are automatically executed at various points of the script life cycle. These functions need to be defined at the start of the script.The various functions are:
onScriptFailure
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
onScriptFailure($exception)
Arguments
$exception | exception | Javascript Exception object |
Returns
null |
Sahi Pro Flowcharts Action :On Script Failure
Details
This is a callback function. It needs to be implemented by the tester. It is called whenever an assertion failure occurs in the script. $exception is the actual exception that Sahi threw. Can be used with _logException and _logExceptionAsFailure. $exception added since Sahi Pro V4.3
This is a callback function. It needs to be implemented by the tester. It is called whenever an assertion failure occurs in the script. $exception is the actual exception that Sahi threw. Can be used with _logException and _logExceptionAsFailure. $exception added since Sahi Pro V4.3
warning_fail API should not be used inside
onScriptFailure
.onScriptFailure = function($e){
// Handle whatever you want to do, first.
// Use either of the following. Prefer the former.
_logExceptionAsFailure($e); // Explicitly log the exception as failure.
_log("In onScriptError", "FAILURE"); // Log with a Failure tag.
}
onScriptError
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
onScriptError($exception)
Arguments
$exception | exception | Javascript Exception object |
Returns
null |
Sahi Pro Flowcharts Action :On Script Error
Details
This is a callback function. It needs to be implemented by the tester. It is called whenever an error occurs in the script. $exception is the actual exception that Sahi threw. Can be used with _logException and _logExceptionAsFailure. If the function returns true, the script will continue to execute. Default is false.
This is a callback function. It needs to be implemented by the tester. It is called whenever an error occurs in the script. $exception is the actual exception that Sahi threw. Can be used with _logException and _logExceptionAsFailure. If the function returns true, the script will continue to execute. Default is false.
warning_fail API should not be used inside
onScriptError
.warning
If you have handled onScriptError in the script, Sahi will not treat it as a failure unless you specifically indicate it.
To indicate that the script has failed, you can use one of the following approaches.
To indicate that the script has failed, you can use one of the following approaches.
onScriptError = function($e){
// Handle whatever you want to do, first.
// Use either of the following. Prefer the former.
_logExceptionAsFailure($e); // Explicitly log the exception as failure.
_log("In onScriptError", "FAILURE"); // Log with a Failure tag.
return false;
}
onScriptEnd
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
onScriptEnd()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :On Script End
Details
This is a callback function. It needs to be implemented by the tester. It is called at the end of a script (even if there are errors in the script and the script stops in the middle).
This is a callback function. It needs to be implemented by the tester. It is called at the end of a script (even if there are errors in the script and the script stops in the middle).
onScriptEnd = function(){
// Step performed after end of script.
_log("After End");
}
Callback example
onBeforeStep = function($step, $debugInfo){
_log("onBeforeStep executed for step : " +$step);
}
onAfterStep = function($step, $debugInfo, $status){
_log("step executed successfully with status : "+$status);
}
onScriptEnd = function(){
_click(_button("Logout"));
}
onScriptError = function($e){
_alert(">> In onScriptError");
}
onScriptFailure = function($e){
_alert(">> In onScriptFailure");
}
_navigateTo("http://sahi.co.in/demo/training/");
_setValue(_textbox("user"), "test");
_setValue(_password("password"), "secret");
_click(_submit("Login"));
_assertExists(_submit("Login")); // cause SCRIPT ASSERTION FAILURE - triggers onScriptFailure
_setValue(_textbox("q11"), "2"); // causes SCRIPT ERROR - triggers onScriptError
// Script aborts here, but executes onScriptEnd() to logout
_setValue(_textbox("q[1]"), "1");
_setValue(_textbox("q[2]"), "1");
_click(_button("Add"));
_assertEqual("1150", _textbox("total").value); // cause SCRIPT FAILURE
// If not aborted earlier, automatically calls onScriptEnd() to logout.
infoNote: Sahi already has implemented some Callback functions in
Thus when implementing your Callback function, you must override the default implementation of that Callback function as shown in the examples above.
userdata/scripts/global_include.sah
which will be called by default.
Thus when implementing your Callback function, you must override the default implementation of that Callback function as shown in the examples above.
Taking screenshots on error/failure
onScriptError = function($e){
_focusWindow();
_takeScreenShot();
}
onScriptFailure = onScriptError;
_navigateTo("http://sahi.co.in/demo/training/");
_setValue(_textbox("user"), "test");
_setValue(_password("password"), "secret");
_click(_submit("Login"));
Force Sahi to continue on error after screenshots and logging.
onScriptError = function($e){
_logExceptionAsFailure($e);
_focusWindow();
_takeScreenShot();
return true; // Forces Sahi to continue execution and not stop at error. Since Sahi Pro V4.3
}
_navigateTo("http://sahi.co.in/demo/training/");
_setValue(_textbox("user"), "test");
_setValue(_password("password"), "secret");
_click(_submit("Login"));
onBeforeStep
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
onBeforeStep($step, $debugInfo)
Arguments
$step | String | which step is about to execute. |
$debugInfo | String | debug information of the step(example : file name, lineNumber) |
Returns
null |
Sahi Pro Flowcharts Action :On Before Step
Details
This is a callback function. It needs to be implemented by the tester. It is called before every step in the script.
This is a callback function. It needs to be implemented by the tester. It is called before every step in the script.
danger
Use this sparingly since it will be executed for every step in the script
onBeforeStep = function($step, $debugInfo){
// Step performes before every steps.
_log("onBeforeStep executed for step : " +$step);
}
onAfterStep
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
onAfterStep($step, $debugInfo, $status)
Arguments
$step | String | which step is about to execute. |
$debugInfo | String | debug information of the step(example : file name, lineNumber) |
$status | String | status of function which got executed.(example : SUCCESS, FAILURE, ERROR, WAIT, UNRESPONSIVE_EXCEPTION) |
Returns
null |
Sahi Pro Flowcharts Action :On After Step
Details
This is a callback function. It needs to be implemented by the tester. It is called after every step in the script.
This is a callback function. It needs to be implemented by the tester. It is called after every step in the script.
danger
Use this sparingly since it will be executed for every step in the script
onAfterStep = function($step, $debugInfo, $status){
// Step performes after every steps.
_log("step executed successfully with status : "+$status);
}
Recovery Functions (DEPRECATED)
_setRecovery
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_setRecovery($fn)
Arguments
$fn | function | Function that will get called on failure or error |
Returns
null |
Sahi Pro Flowcharts Action :Set Recovery
Details
Sets a recovery function which will be called on failure or error.
Sets a recovery function which will be called on failure or error.
dangerDEPRECATED: Do not use. Use callback functions as specified above
_removeRecovery
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_removeRecovery()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Remove Recovery
Details
Removes previously set recovery function.
Removes previously set recovery function.
dangerDEPRECATED: Do not use. Use callback functions as specified above