2013-12-18 237 views
4

我有一个框架,其中我正在运行我的测试;任何错误都会导致WebDriver实例调用类似于我称之为driver.quit()方法的handleexception方法。我觉得driver.quit()试图关闭浏览器,但是然后浏览器会抛出一个对话框,显示你想离开这个页面的消息 - 这是不需要处理的。没有异常抛出,但剩余的测试用例在启动浏览器时不起作用,但抛出模态对话框存在异常,这是可以理解的。 我该如何解决这个问题?driver.quit()不关闭浏览器窗口

handleexception方法:

Driver.getFailedTestCaseNames().add(this.getTestcaseName()); 
    String failedStepStr = ""; 
    failedStepStr = "Test Case failed while executing step " + stepNo + " - Method Name:" + t.getMethodnm() + ", Field Name:" + t.getField() + " , Field value:" + t.getValue();   

    LOG.error("Test Case Name: " + testcaseName + " failed while executing step " + stepNo + " - Method Name:" + t.getMethodnm() + ", Field Name:" + t.getField() + " , Field value:" 
      + t.getValue() + ExceptionUtils.getStackTrace(th)); 

    CaptureScreenshot objCapturScreenshot = new CaptureScreenshot(); 

    objCapturScreenshot.caputureImage(testcaseName); 
    System.out.println(" in handle exception"); 
    driver.quit(); 

    System.out.println("Test case failed : "+this.getTestcaseName()); 
    throw new TestAutomationException(failedStepStr, th); 

日志对于犯测试用例是:

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died. 
    Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01' 
    System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11' 
    Driver info: driver.version: RemoteWebDriver 
     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) 
     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569) 
     at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:423) 
     at com.bmc.core.test.domain.TestCase.handleException(TestCase.java:364) 
     at com.bmc.core.test.domain.TestCase.execute(TestCase.java:136) 
     at sun.reflect.GeneratedMethodAccessor40.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:80) 
     at org.testng.internal.Invoker.invokeMethod(Invoker.java:702) 
     at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894) 
     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219) 
     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:768) 
     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:53) 
     at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) 
     at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185) 
     at org.testng.TestNG.runSuitesLocally(TestNG.java:1110) 
     at org.testng.TestNG.run(TestNG.java:1022) 
     at org.testng.TestNG.privateMain(TestNG.java:1325) 
     at org.testng.TestNG.main(TestNG.java:1294) 
    Caused by: org.openqa.selenium.WebDriverException: java.net.SocketTimeoutException: Read timed out 
    Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01' 
    System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11' 
    Driver info: driver.version: RemoteWebDriver 
     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:75) 
     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527) 
     ... 26 more 
    Caused by: java.net.SocketTimeoutException: Read timed out 
     at java.net.SocketInputStream.socketRead0(Native Method) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166) 
     at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90) 
     at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281) 
     at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92) 
     at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62) 
     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254) 
     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289) 
     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252) 
     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219) 
     at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300) 
     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127) 
     at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712) 
     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517) 
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 
     at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:319) 
     at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:298) 
     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66) 
     ... 27 more 

日志用于后续测试案例是:

com.bmc.core.test.exception.TestAutomationException: Test Case failed while executing step 1 - Method Name:waitAndSwitchToWindow, 
    Field Name:Inquira Information Manager , Field value:20000 
      at com.bmc.core.test.domain.TestCase.handleException(TestCase.java:400) 
      at com.bmc.core.test.domain.TestCase.execute(TestCase.java:136) 
      at sun.reflect.GeneratedMethodAccessor40.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:80) 
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:702) 
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894) 
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219) 
      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:768) 
      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:53) 
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) 
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185) 
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1110) 
      at org.testng.TestNG.run(TestNG.java:1022) 
      at org.testng.TestNG.privateMain(TestNG.java:1325) 
      at org.testng.TestNG.main(TestNG.java:1294) 
    Caused by: java.lang.reflect.InvocationTargetException 
      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 com.bmc.core.test.domain.TestCase.execute(TestCase.java:99) 
      ... 22 more 
    Caused by: org.openqa.selenium.UnhandledAlertException: Modal dialog present: 
    Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01' 
    System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11' 
    Session ID: ac1a28e7-2c24-443f-9c0e-d308645fab2d 
    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, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false, 
    initialBrowserUrl=http://www.localohst:10879/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, 
    ie.browserCommandLineSwitches=, 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:191) 
      at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:172) 
      at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141) 
      at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) 
      at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569) 
      at org.openqa.selenium.remote.RemoteWebDriver.getWindowHandles(RemoteWebDriver.java:432) 
      at com.bmc.core.test.app.Base.waitAndSwitchToWindow(Base.java:278) 
      ... 27 more 

回答

1

保罗的建议后,我想出了这个片段,并能正常工作....它关闭退出所有的浏览器不管他们是否扔在关闭的警报......

try 
    { 
    Set<String> windowIterator = driver.getWindowHandles();    
    Iterator<String> iter = windowIterator.iterator(); 
    System.out.println("no of windows to close ="+windowIterator.size()); 
    LOG.debug("no of windows to close ="+windowIterator.size()); 
    int i=0; 
    while (iter.hasNext()) 
    { 
     try 
     { 
      i++; 
      System.out.println("lopp value = "+i); 
      LOG.debug("loop value = "+i); 
      String windowHandle = iter.next(); 
      driver.switchTo().window(windowHandle); 
      String windowTitle = driver.getTitle(); 
      System.out.println("title="+driver.getTitle()+" - attempting to close this window"); 
      LOG.debug("title="+driver.getTitle()+" - attempting to close this window"); 
      driver.close(); 
      Thread.sleep(5000); 

      while(isAlertPresent()) 
       { 
       try 
       { 
        driver.switchTo().alert().accept(); 
        System.out.println("alert accepted after close"); 
        LOG.debug("alert accepted after close"); 
        Thread.sleep(5000); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
        LOG.debug("alert loop... inner while --> exception : "+e.getMessage()); 
       } 
       }    
      System.out.println("title="+windowTitle+" - window closed"); 
      LOG.debug("title="+windowTitle+" - window closed"); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      LOG.debug("window loop... outer while --> exception : "+e.getMessage()); 
     } 
    } 
    Thread.sleep(5000); 
    while(isAlertPresent()) 
    { 
     driver.switchTo().alert().accept(); 
     System.out.println("alert accepted before quit"); 
     LOG.debug("alert accepted before quit"); 
     Thread.sleep(5000); 
    }  
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     LOG.debug("outer exception : "+e.getMessage()); 
    } 

    driver.quit(); 
    System.out.println("quitting the driver instance"); 
    LOG.debug("quitting the driver instance"); 
    //================================================================================== 
+0

嗨,请分享isAlertPresent()方法的代码吗?感谢 –

+2

抱歉,没有显示它前面 \t公共布尔isAlertPresent() \t { \t尝试 \t { \t警报警报= driver.switchTo()警报(); \t \t \t String str = alert.getText(); \t \t \t System.out.println(str); \t \t \t \t return true; \t} \t catch(NoAlertPresentException Ex) \t { \t return false; \t} \t} –

0
driver.switchTo().alert().accept(); 

上述代码出现后应该关闭对话框。

+0

我们可以调用已经调用quit()之后的驱动程序对象实例?否则我如何获得对话框? –

+0

使用driver.close()尝试关闭浏览器,然后我的代码再使用quit。 –