Sahi Documentation

Exception Handling and Recovery

Try Catch

For anticipated exceptions, one may use try catch blocks.

Syntax
try{
// sahi statements
}catch($e){
// Corrective action
// Can print exact source of error in log
// Can throw the same or another exception
}


Corrective Action

try{
  _click(_link("does not exist"));
}catch($e){
  _log("Exception occured"); // simple logging. no failure
  _click(_link("linkByHtml")); // Corrective action
}


Corrective Action and Log the Exception Message

try{
  _disableDefaultErrorLogging(); // This will ignore and not log the failures which occur in try block
  _click(_link("does not exist"));
}catch($e){
  _click(_link("linkByHtml")); // Corrective action
  _logException($e); // Logs the exception, but does not fail
}


Corrective Action, Log and then Fail

try{
  _disableDefaultErrorLogging(); // This will ignore and not log the failures which occur in try block
  _click(_link("does not exist"));
}catch($e){
  _click(_link("linkByHtml")); // Corrective action
  _logExceptionAsFailure($e); // Logs the exception, and fails,
  // and in the logs, points to the original line as source of failure.
}


Script Callback Functions

For unanticipated exceptions, override Sahi's callback functions to recover.

Example:
function onScriptEnd(){
  _click(_button("Logout"));
}
function onScriptError(){
  _log(">> In onScriptError");
}
function onScriptFailure(){
  _log(">> 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"));


_setRecovery

warning_setRecovery is no longer recommended and may be deprecated soon. Use the callback functions instead
The relevant APIs are:

_setRecovery(fn) _removeRecovery()

Any function can be assigned to a script as a recovery function. Once set, if there is an error during execution, the recovery function will be called before the script stops.

_navigateTo("http://sahi.co.in/demo/");

function myRecoveryFn(){
  _alert("In myRecoveryFn."); // This statement will be alerted in case of script error.
}

_setRecovery(myRecoveryFn); // Set the myRecoveryFn as recovery function.
_click(_link("Link Test")); // Works normally
_click(_link("Bad Link"));
// This statement fails and causes myRecoveryFn to be called.
_alert("done");
// This statement will not be called, because script failed in the last statement.


The recovery function can be removed via _removeRecovery();

warning_setRecovery is no longer recommended and may be deprecated soon. Use the callback functions instead