17

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.

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
redwulf
  • 1,317
  • 3
  • 13
  • 35

3 Answers3

26

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

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
redwulf
  • 1,317
  • 3
  • 13
  • 35
  • I'm using PhantomJS. Maybe it's an issue in a specific version? With version 1.9.7 it worked for me. – redwulf Sep 10 '14 at 14:53
  • I'm having this error : http://stackoverflow.com/questions/25739542/closing-a-window-confirm-in-protractor-with-phantomjs Using also 1.9.7. Quite strange. – Matthieu Riegler Sep 10 '14 at 14:57
  • Probably won't be of any help, but I'm actually using 1.9.7-4, installed through npm. – redwulf Sep 11 '14 at 08:25
  • 1
    ``browser.switchTo().alert()`` worked for me. See https://github.com/angular/protractor/blob/3d60689b423749686973227f4b26cbb494f60b66/spec/basic/navigation_spec.js#L8 – tomaszbak Jan 27 '15 at 17:10
13

Copied from a comment above, but I had to user browser.switchTo().alert() instead of grabbing my Protractor instance.

Ended up using:

browser.switchTo().alert().accept();

to answer an alert dialog.

z0d14c
  • 1,122
  • 1
  • 12
  • 20
0

browser.switchTo().alert()

this worked for me. The protractor.getInstance method didn't work for me.

Henry
  • 121
  • 8