Skip to content
Sahi Pro Flowcharts is retired. These pages document the last release (v12) and are no longer updated. Go to the current documentation.Flowcharts is retired. Current docs
Get Free Trial
Sahi Pro Flowcharts is retired. These pages document the last release (v12) and are no longer updated. Go to the current documentation.

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.

An alert box looks like this:

alert

There are 3 APIs used with alerts.

Modes Browser

Parameters

NameTypeDescription
$allalertsbooleanoptionalIf true, returns array of all alert messages.

Returns

stringlast alert message if $allalerts is false
array of stringsan array of all alert messages in the browser if $allalerts is true
Raw script
//return array of all alerts in the browser.
var $allAlerts = _lastAlert(true);
//return only last alert.
var $lastAlert= _lastAlert();

Sahi Pro Classic API _lastAlert

Modes Browser

Expect Alert tells Sahi to react with OK for a dialog with given message.

Parameters

NameTypeDescription
$messagestringmessage to expect in dialog. This can also be a regular expression
$persistbooleanoptionalif true, will accept a single _expectAlert statement for all alerts coming after the statement. If false, will accept only the next alert box to work with the _expectAlert statement. Default is false.
Raw script
//If Alert word expect in dialog then Sahi react with OK.
_expectAlert("/abc/",true); //Accept all alerts with "abc" message.
_expectAlert("/abc/"); //Accept only the next alert with "abc" message.

Sahi Pro Classic API _expectAlert

Modes Browser

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

Parameters

NameTypeDescription
None

Sahi Pro Classic API _clearLastAlert

_expectAlert("/abc/", false); // Tell Sahi to press OK for dialog with message containing "abc"
_click(_button("Click Me")); // trigger the alert
_assertEqual("abc", _lastAlert()); // verify the alert message

A confirm dialog looks like this:

confirm

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.

Modes Browser

Parameters

NameTypeDescription
$allconfirmsbooleanoptionalIf true, returns array of all confirm dialog messages.

Returns

stringlast confirm message if $allconfirms is false
array of stringsan array of all confirm messages in the browser if $allconfirms is true
Raw script
//return array of all Confirms in the browser.
var $allConfirms = _lastConfirm(true);
//return only last Confirm.
var $lastConfirm= _lastConfirm();

Sahi Pro Classic API _lastConfirm

Modes Browser

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

Parameters

NameTypeDescription
$messagestringmessage to expect in dialog. This can also be a regular expression
$valuebooleanif true will press OK when a confirm dialog with given message appears. If false, will click on Cancel.
$persistbooleanoptionalif 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.

Sahi Pro Classic API _expectConfirm

Modes Browser

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

Parameters

NameTypeDescription
None

Sahi Pro Classic API _clearLastConfirm

// 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

A prompt box looks like this:

prompt

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.

Modes Browser

Parameters

NameTypeDescription
$allpromptsbooleanoptionalIf true, returns array of all prompt messages.

Returns

stringlast prompt message if $allprompts is false
array of stringsan array of all prompt messages in the browser if $allprompts is true
Raw script
//return array of all Prompts in the browser.
var $allPrompts = _lastPrompt(true);
//return only last Prompt.
var $lastPrompt= _lastPrompt();

Sahi Pro Classic API _lastPrompt

Modes Browser

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

Parameters

NameTypeDescription
$promptMessagestringmessage to expect in dialog. This can also be a regular expression
$responsestringResponse to enter into the prompt. null to behave as if Cancel was pressed.
$persistbooleanoptionalif true, will accept a single _expectPrompt statement for all prompts coming after the statement. If false, will accept only the next prompt box to work with the _expectPrompt statement. Default is false. Added in 7.5.0

Sahi Pro Classic API _expectPrompt

Modes Browser

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

Parameters

NameTypeDescription
None

Sahi Pro Classic API _clearLastPrompt

_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

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

Modes Browser

Parameters

NameTypeDescription
None

Returns

booleantrue if window.print() was called, else false

Sahi Pro Classic API _printCalled

Modes Browser

Expect Print tells Sahi to react with CANCEL for a print dialog.

Parameters

NameTypeDescription
$persistbooleanoptionalif true, will accept a single _expectPrint statement for all prints coming after the statement. If false, will accept only the next print box to work with the _expectPrint statement. Default is false.

Sahi Pro Classic API _expectPrint

Modes Browser

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

Parameters

NameTypeDescription
None

Sahi Pro Classic API _clearPrintCalled