Monday, June 9, 2014

Protractor: How to avoid UnexpectedAlertOpenError

At work, we've recently started to use protractor to write end-to-end tests for a legacy AngularJs code base.

Last week we found a problem while trying to test that an element from a list was deleted:


The problem was that clicking on the delete icon was displaying an alert box, prompting the user to confirm whether they actually wanted to delete the element or not, that protractor didn't expect and was causing the following error:
UnexpectedAlertOpenError: unexpected alert open

After searching in Google for a while, we found a solution:


that fixed the problem by switching to the alert box and confirming that we wanted to delete the element.

Since it was a bit verbose and we'd probably have to use it again in some other tests, we extracted it to a helper function in our TestTools module:


I hope you might find this useful if you're using protractor.

5 comments:

  1. Yes, thanks this was helpful even without protractor, just for selenium. Alert handling is a big pain in the *** there.

    ReplyDelete
    Replies
    1. I'm glad it was useful to you.
      Best regards,
      M

      Delete
  2. It is very useful but it is not working when i use it after the select option

    ReplyDelete
  3. It is not working for me and I am getting the following error:
    ....A Jasmine spec timed out. Resetting the WebDriver Control Flow.
    The last active task was:
    Protractor.waitForAngular()

    ReplyDelete