Chris Schell Chris Schell - 23 days ago 19
Node.js Question

Detecting Alerts with if statement Webdriverjs

I am wondering if there is a good way to handle an alert within an if statement using Webdriverjs. I've tried a few different methods that I've found on here and other sites with no success. I am however, extremely new to Javascript and Node.js so it may be an implementation thing on my part.

Specifically, I am doing automated testing for a number of different clients on nearly identical pages. However, only certain clients throw an alert when a particular button is pushed. So I would like to handle this through an if statement by detecting if an alert is present and proceed accordingly, otherwise continue on.

I did read a previous post detailing a similar problem in Java, and the responder noted that in automated testing you should always be aware of how the site will act, and that perhaps this problem should be handled entirely differently. The most simple but least desirable way I can think of would be to simply write two different tests, which I could do.

Any suggestions for the code, as well as input on this problem more generally would be extremely appreciated.

Thanks in advance!

Answer

Selenium throws a UnhandledAlertException: Modal dialog present exception if you try to access a page element with an alert present. Since you seem to know what button causes the alert, you should know where to try/catch this exception and handle appropriately.

You can then use the org.openqa.selenium.Alert package to accept, dismiss, etc the popup.

http://toolsqa.com/selenium-webdriver/handling-of-alerts-javascript-alerts-and-popup-boxes/

Comments