Sahi Pro - Javascript Dialogs

Browser modal dialogs can be troublesome for automation testers. Since it is a modal,
the browser does not proceed with automation unless someone acts upon these dialogs.
Sahi is built for parallel playback where browsers need not be in focus.
Sahi has specific mechanisms to deal with modal dialogs.

Sahi handles these dialogs by mocking them out. You will not see a dialog box when you playback via Sahi.
However you will be able to control the user interaction with these dialogs with specific Sahi APIs.

Alert

An alert box looks like this:

There are 2 APIs used with alerts.

_lastAlert

_lastAlert()

Arguments
None

Details

Returns the last alerted message

_clearLastAlert

_clearLastAlert()

Arguments
None

Details

Removes the last alert from session so that further occurrence of alerts can be checked

Alert Example

_click(_button("Click Me")); // trigger the alert
_assertEqual("abc", _lastAlert()); // verify the alert message

Confirm

A confirm dialog looks like this:
In a confirm dialog the user can either press OK or Cancel.
Sahi by default clicks OK on the dialog. Sahi can also be told to press Cancel.

_lastConfirm

_lastConfirm()

Arguments
None

Details

_lastConfirm returns the last confirm dialog message.

_expectConfirm

_expectConfirm($message, $value[, $persist])

Arguments
$messagestring message to expect in dialog. This can also be a regular expression
$valueboolean if true will press OK when a confirm dialog with given message appears. If false, will click on Cancel.
$persistboolean optionalif true, will accept a single _expectConfirm statement for all confirms coming after the statement. If false, will accept only the next confirm box to work with the _expectConfirm statement. Default is false.

Details

_expectConfirm tells Sahi to react with OK or Cancel for a dialog with given message.

_clearLastConfirm

_clearLastConfirm()

Arguments
None

Details

Removes the last confirm from session so that further occurrence of confirms can be checked

Confirm Example

// To click Cancel, use
_expectConfirm("/question/", false); // Tell Sahi to press Cancel for dialog with message containing "question"
_click(_button("Click For Confirm")); // Invoke the dialog
// Sahi would click cancel on it now
_assertEqual("Some question?", _lastConfirm()); // verify the actual confirm message

Prompt

A prompt box looks like this:

A prompt dialog is not very commonly used. Sahi by default behaves as if OK was pressed without entering any text.
Sahi can be forced to enter text by setting an expectation via _expectPrompt API.
The message on the prompt box can be obtained later by _lastPrompt() API.

_lastPrompt

_lastPrompt()

Arguments
None

Details

_lastPrompt returns the last prompt dialog message.

_expectPrompt

_expectPrompt($promptMessage, $response)

Arguments
$promptMessagestring message to expect in dialog. This can also be a regular expression
$responsestring Response to enter into the prompt.

Details

_expectPrompt tells Sahi to respond with given response for a prompt dialog with given promptMessage.

_clearLastPrompt

_clearLastPrompt()

Arguments
None

Details

Removes the last prompt from session so that further occurrence of prompts can be checked

Prompt Example

_expectPrompt("/Some prom/", "abc"); // set expectation
_click(_button("Click For Prompt")); // click for prompt
// abc would be entered by Sahi in the prompt box.
_assertEqual(_lastPrompt(), "Some prompt?"); // verify the full prompt dialog message

Print dialog

Calling the print dilaog during functional test automation is not of much use.
However, we can verify if window.print() was called using the following APIs

_printCalled

_printCalled()

Arguments
None

Details

Returns true if window.print() was called. Else false.

_clearPrintCalled

_clearPrintCalled()

Arguments
None

Details

Resets _printCalled to false, so that _printCalled can be checked once more in the script.