0
votes

I have upgraded to Selenium 3.3.0 today.

After upgrade, my testNG executes 1st test successfully.

My second test throws me WebDriverException

org.openqa.selenium.WebDriverException: An unknown error has occurred Build info: version: 'unknown', revision: 'b526bd5', time: '2017-03-07 11:11:07 -0800' System info: host: 'INDH001138', ip: '10.244.44.33', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{moz:profile=C:\Users\z019999\AppData\Local\Temp\rust_mozprofile.45imqIpOZwTh, rotatable=false, timeouts={implicit=0, page load=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=52.0, platformVersion=6.1, moz:processID=2808, browserName=firefox, platformName=windows_nt}] Session ID: f8fdc26d-26c6-40d6-b4b4-f0bb4aae087a 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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:661) at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.alert(RemoteWebDriver.java:990) at com.myCompany.myProject.utility.SeleniumLib.isAlertPresent(SeleniumLib.java:507) at com.myCompany.myProject.utility.SeleniumLib.handleAlertAccept(SeleniumLib.java:424) at com.myCompany.myProject.utility.SeleniumLib.executeTestCase(SeleniumLib.java:613) at com.myCompany.myProject.testEngine.TestCaseEngine.solo(TestCaseEngine.java:132) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

The place where code stops is

private static boolean isAlertPresent() 
    { 
        try 
        { 
            driver.switchTo().alert(); // Place where my code fails
            return true; 
        } 
        catch (NoAlertPresentException Ex) 
        { 
            return false; 
        }
    }

Note: This code works perfectly for test case number 1. But fails for test case number 2, 3, etc..

2

2 Answers

0
votes

It seems you are switching to the alert. But you are not clicking on "OK" or "Cancel" button. So you may need to add the following lines of code to click on either "OK" or "Cancel".

//Switch to the Alert & Accept
driver.switchTo().alert().accept();

or

//Switch to the Alert & Dismiss
driver.switchTo().alert().dismiss();

Let me know if it helps you.

0
votes

If this was working before the upgrade, I would downgrade to be honest. However, if you insist, a workaround could be to try in a loop after a short 2 sec delay to eliminate the failure from trying to locate an alert too early.

So here is the full working code in a loop with a delay:

for(int i=1; i<3; i++)    
{
        try{
        TimeUnit.SECONDS.sleep(2);
        driver.switchTo().alert();
        System.out.printf("%n*** An alert was present ***");       
        alertPresent=true;
        }
        catch (NoAlertPresentException e) {
        System.out.printf("%n*** An alert was not present ***");
        alertPresent=false;
        }   
return alertPresent;
}