2

In my tests, I have many alerts that appear. For example, if I don't fill out a required item in my form, I get an alert. So far, I have been able to switch to them and perform my actions (verify text, click ok/cancel) by using the below code.

basicClick(AppObjects.Continue);
        String warningText2 = driver.switchToAlert().getText();
        Assert.assertEquals(warningText2, "You have not responded to all of the items on this page.\n\nPlease click 'OK' to return to the current page, or 'Cancel' to go to the next page.");
        driver.switchToAlert().accept()

I am now trying to test another alert. It is in a different part of my application and caused by different circumstances than the previous alerts, but it is still just a javascript alert that appears. I use the code below to try and verify the text and click OK, but it is not working.

basicClick(AppObjects.Continue);
        String warningText3 = driver.switchToAlert().getText();
        Assert.assertEquals(warningText3, "A comment would be helpful, but is not required.\n\nPlease click 'OK' to return to the current page, or 'Cancel' to go to the next page");
        driver.switchToAlert().accept();

I am getting the below errors when trying to switch to this new alert using web driver. How can I get around this issue and verify the text inside the alert and click OK/Cancel?

Here is the error I get when using IE or FF

org.openqa.selenium.UnhandledAlertException: Modal dialog present: A comment would be helpful, but is not required.

Please click 'OK' to return to the current page, or 'Cancel' to go to the next page Build info: version: '2.42.0', revision: '5e82430', time: '2014-05-22 20:18:07' System info: host: 'jgubbels', ip: '10.50.13.78', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_40' Session ID: 5375f9ca-7077-403b-b8e2-fb671ee238b6 Driver info: org.openqa.selenium.ie.InternetExplorerDriver Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=10, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]

Here is the error I get when using Chrome

org.openqa.selenium.UnhandledAlertException: unexpected alert open
(Session info: chrome=35.0.1916.114) (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 3 milliseconds: null Build info: version: '2.42.0', revision: '5e82430', time: '2014-05-22 20:18:07' System info: host: 'j', ip: 'xx.xx.xxxx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_40' Session ID: fb3d4212ee1801c6aafbf6f95a9f9b6d Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\IBM_AD~1\AppData\Local\Temp\scoped_dir10676_19364}, rotatable=false, locationContextEnabled=true, version=35.0.1916.114, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:151) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:596) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:611) at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:321) at com.ibm.atmn.waffle.core.webdriver.WebDriverExecutor.saveScreenshotWithFilename(WebDriverExecutor.java:311) at com.ibm.atmn.waffle.base.BaseTestListener.onTestFailure(BaseTestListener.java:72) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1895) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1292) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

TestRaptor
  • 1,305
  • 8
  • 24
  • 42

3 Answers3

0

Are you sure that you're waiting until the alert is present? Doing QA, I found that the hardest part of the job tended to be getting the script to wait until the necessary element on the page had been rendered.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • I am waiting for the alert to appear. I have tried adding the line sleep(10000); just to make sure. When I watch the test, the alert appears instantly, the test pauses for 10 seconds to be safe, and I still get the error. – TestRaptor Jun 06 '14 at 15:32
  • Following [this](http://stackoverflow.com/questions/19550514/selenium-unhandledalertexception-was-unhandled) and [this](https://groups.google.com/forum/#!topic/webdriver/QhZyaAzEk3c), how about something like `try { sleep(10000); } except (UnandledAlertException) { // run your test }`? Also, consider grabbing the `IAlert` object and working with that. It looks like [someone else has had a similar issue](https://code.google.com/p/selenium/issues/detail?id=4986). – Patrick Collins Jun 06 '14 at 15:39
0

Are you sure that your alert is exactly equal to the one tested ? I would print the output of warningText3 in logs and first compare it by hand to validate the format of the alert.

aorfevre
  • 5,034
  • 3
  • 21
  • 51
  • Thats really the main problem I am having. I can't print the output of warningText3, because I am unable to locate the alert. When my test runs and I try to switch to the alert to grab text or click OK, it acts like the alert doesn't exist. – TestRaptor Jun 10 '14 at 13:47
0

I don't know why the javascript alerts would behave differently, but here is the solution I found to my problem. I just moved the assert statement to after the alert was closed. I'm not sure why it has to be moved for this alert and not others, but it solved my problem.

basicClick(AppObjects.Continue);
        String warningText3 = driver.switchToAlert().getText();
        driver.switchToAlert().accept();
        Assert.assertEquals(warningText3, "A comment would be helpful, but is not required.\n\nPlease click 'OK' to return to the current page, or 'Cancel' to go to the next page");
TestRaptor
  • 1,305
  • 8
  • 24
  • 42