Top Rounds
[Hide Navigation]

Sahi Call Back 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:

Function onScriptFailure($exception)
Notes 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




Function onScriptError($exception)
Notes Is called whenever an error 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

If the function returns true, the script will continue to execute. Default is false. Added since Sahi Pro V4.3


Function onScriptEnd()
Notes Is called at the end of a script (even if there are errors in the script and the script stops in the middle).

Example:

function onScriptEnd(){
	_click(_button("Logout"));
}
function onScriptError(){
	_alert(">> In onScriptError");
}
function onScriptFailure(){
	_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.

Taking screenshots

Use _focusWindow() and _takeScreenShot() (available since Sahi Pro V4.3)

function onScriptError($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.

function onScriptError($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"));



---


Top Rounds