Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a confirm dialog with Protractor

This seems to be a pretty simple question, but I really can't find an answer online and I was not able to find an answer myself.

I'm using AngularJS for my application and at some point, I have a native JavaScript confirmation box/dialog which asks the user if he/she wants to accept or cancel a change.

How can I simulate the selected option in my tests (with Protractor)? Is it possible to access the confirmation box and "click" either Ok or Cancel and act accordingly in my test? I'm guessing something like

ptor.switchTo().<something>

would be possible, but I can't seem to find an answer.

like image 316
redwulf Avatar asked May 27 '14 14:05

redwulf


People also ask

How do I get an alert message on protractor?

findElement(protractor.By.id('alertButton')); button. click(); }); it('tells the alert message', function () { var alertDialog = ptor. switchTo(). alert(); expect(alertDialog.

When you would use the confirm () dialog box?

A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons: OK and Cancel. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false.

What is confirm script?

Definition and Usage The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

How do I perform a test in protractor?

Once the Protractor is set up, in order to perform the test, one needs a spec file and a configuration file. While the spec file is the actual test script, the configuration file specifies the details of the test such as where to find the test files, which browser and framework are to be used for running them along with other configurations.

How to handle alert in selenium protractor?

The protractor web driver uses a void dismiss () command to cancel the alert. The following command is executed for handling alert in Selenium Protractor wit dismiss () alert class method: The accept () alert class method is used to accept an alert and continue with the webpage operation.

What are the characteristics of alerts in protractor?

The characteristics of alerts often block the source page and force the intended user to read the alert before they can access the web page. The Alerts in the protractor framework are not part of a window therefore they cannot be handled by utilization of JavaScript Executor.

Can protractor be used to test cross browser compatibility?

When performing tests on a remote server, Protractor can be used to test Cross Browser Compatibility for a wide range of devices using a Real Device Cloud. BrowserStack’s real device cloud provides access to a fleet of 2000+ desktop browsers and real mobile devices like iPhone, iPad, Galaxy, OnePlus, Pixel, Xiaomi, and Redmi, to name a few. 5.


1 Answers

I guess I can answer my own question as this may be of use to someone else.

First, you need to get your Protractor instance:

var ptor = protractor.getInstance();

Confirmation dialogs are handled the same way as alerts, so something like this did the trick:

var alertDialog = ptor.switchTo().alert();
alertDialog.accept();  // Use to accept (simulate clicking ok)
alertDialog.dismiss(); // Use to simulate cancel button

So simple and elegant, yet hard to find an answer. Hope this helps someone else

like image 148
redwulf Avatar answered Sep 21 '22 17:09

redwulf