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)