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() |
| Notes | Is called when ever an assertion failure occurs in the script. |
| Function | onScriptError() |
| Notes | Is called when ever an error occurs in the script. |
| 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.