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 3 APIs used with alerts._lastAlert
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_lastAlert([$allalerts])
Arguments
$allalerts | boolean optional | If true, returns array of all alert messages. |
Returns
string | last alert message if $allalerts is false |
array of strings | an array of all alert messages in the browser if $allalerts is true |
Sahi Pro Flowcharts Action :Last Alert
Details
//return array of all alerts in the browser.
var $allAlerts = _lastAlert(true);
//return only last alert.
var $lastAlert= _lastAlert();
info
At times, a UI action causes multiple alerts to appear. To handle such a scenario, one can use _lastAlert(true) and check the last few alerts.
info
$allalerts added in 6.0.0. Scripts written before 6.0.0 do NOT need changes.
_expectAlert
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
7.5.0 | NA | NA | NA | NA |
Available for modes: Browser
_expectAlert($message[, $persist])
Arguments
$message | string | message to expect in dialog. This can also be a regular expression |
$persist | boolean optional | if 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. |
Returns
null |
Sahi Pro Flowcharts Action :Expect Alert
Details
_expectAlert tells Sahi to react with OK for a dialog with given message.
_expectAlert tells Sahi to react with OK for a dialog with given message.
info
NOTE: To log javascript popup messages in reports, value must be provided for the properties. For more details refer here.
//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.
_clearLastAlert
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_clearLastAlert()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Clear Last Alert
Details
Removes the last alert from session so that further occurrence of alerts can be checked
Removes the last alert from session so that further occurrence of alerts can be checked
Alert Example
_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
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
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_lastConfirm([$allconfirms])
Arguments
$allconfirms | boolean optional | If true, returns array of all confirm dialog messages. |
Returns
string | last confirm message if $allconfirms is false |
array of strings | an array of all confirm messages in the browser if $allconfirms is true |
Sahi Pro Flowcharts Action :Last Confirm
Details
//return array of all Confirms in the browser.
var $allConfirms = _lastConfirm(true);
//return only last Confirm.
var $lastConfirm= _lastConfirm();
info
At times, a UI action causes multiple confirmation dialogs to appear. To handle such a scenario, one can use _lastConfirm(true) and check the last few confirmation messages.
info
$allconfirms added in 6.0.0. Scripts written before 6.0.0 do NOT need changes.
_expectConfirm
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_expectConfirm($message, $value[, $persist])
Arguments
$message | string | message to expect in dialog. This can also be a regular expression |
$value | boolean | if true will press OK when a confirm dialog with given message appears. If false, will click on Cancel. |
$persist | boolean optional | if 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. |
Returns
null |
Sahi Pro Flowcharts Action :Expect Confirm
Details
_expectConfirm tells Sahi to react with OK or Cancel for a dialog with given message.
_expectConfirm tells Sahi to react with OK or Cancel for a dialog with given message.
info
NOTE: To log javascript popup messages in reports, value must be provided for the properties. For more details refer here.
_clearLastConfirm
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_clearLastConfirm()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Clear Last Confirm
Details
Removes the last confirm from session so that further occurrence of confirms can be checked
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
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_lastPrompt([$allprompts])
Arguments
$allprompts | boolean optional | If true, returns array of all prompt messages. |
Returns
string | last prompt message if $allprompts is false |
array of strings | an array of all prompt messages in the browser if $allprompts is true |
Sahi Pro Flowcharts Action :Last Prompt
Details
//return array of all Prompts in the browser.
var $allPrompts = _lastPrompt(true);
//return only last Prompt.
var $lastPrompt= _lastPrompt();
_expectPrompt
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_expectPrompt($promptMessage, $response[, $persist])
Arguments
$promptMessage | string | message to expect in dialog. This can also be a regular expression |
$response | string | Response to enter into the prompt. null to behave as if Cancel was pressed. |
$persist | boolean optional | if 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 |
Returns
null |
Sahi Pro Flowcharts Action :Expect Prompt
Details
_expectPrompt tells Sahi to respond with given response for a prompt dialog with given promptMessage.
_expectPrompt tells Sahi to respond with given response for a prompt dialog with given promptMessage.
info
$persist added in 7.5.0. Scripts written before 7.5.0 do NOT need changes.
info
NOTE: To log javascript popup messages in reports, value must be provided for the properties. For more details refer here.
_clearLastPrompt
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_clearLastPrompt()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Clear Last Prompt
Details
Removes the last prompt from session so that further occurrence of prompts can be checked
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
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_printCalled()
Arguments
None |
Returns
boolean | true if window.print() was called, else false |
Sahi Pro Flowcharts Action :Print Called
Details
_expectPrint
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
7.5.0 | NA | NA | NA | NA |
Available for modes: Browser
_expectPrint([$persist])
Arguments
$persist | boolean optional | if 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. |
Returns
null |
Sahi Pro Flowcharts Action :Expect Print
Details
_expectPrint tells Sahi to react with CANCEL for a print dialog.
_expectPrint tells Sahi to react with CANCEL for a print dialog.
info
NOTE: To log javascript popup messages in reports, value must be provided for the properties. For more details refer here.
_clearPrintCalled
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | NA | NA | NA | NA |
Available for modes: Browser
_clearPrintCalled()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Clear Print Called
Details
Resets _printCalled to false, so that _printCalled can be checked once more in the script.
Resets _printCalled to false, so that _printCalled can be checked once more in the script.
infoYou will not see the print dialog box during playback as Sahi handles it. If you still want the print dialog to be displayed, refer the example below.
_call(_sahi.displayPrintDialog=true); // Enable displaying print dialog
_click(_submit("Print Records")); // Perform action to launch the print dialog