【问题标题】:Selenium Webdriver failing to close cleanlySelenium Webdriver 无法完全关闭
【发布时间】:2015-07-29 19:42:55
【问题描述】:

我正在尝试运行一个 TestNG 套件来测试应用程序上的各种不同类型的帐户。该套件运行时没有错误,但退出时,webdriver 总是无法干净地关闭,提供以下堆栈跟踪:

    Jul 29, 2015 4:23:41 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@4c2bb6e0
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'PC100740', ip: '10.2.211.148', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:628)
    at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:474)
    at selenium.Log_In_Guest.superTearDown(Log_In_Guest.java:286)
    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:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:308)
    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)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7056 [/127.0.0.1] failed: Connection refused: connect
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:134)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:168)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:392)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:578)
    ... 21 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    ... 36 more

相关代码sn-p为:

@AfterMethod
  public void tearDown() throws Exception {
     driver.close();
     String verificationErrorString = verificationErrors.toString();
     if (!"".equals(verificationErrorString)) {
         Assert.fail(verificationErrorString);
     }
  }

  @AfterSuite
  public void superTearDown() throws Exception {
      try{
          driver.quit();
      }catch(Exception e){
          e.printStackTrace();
      }

我已经看到了许多其他关于此的主题,大约一年前,没有一个提供解决我问题的解决方案......

我正在编写的所有测试用例,JUnit 和 TestNG 都会出现这个问题。我在 Eclipse 中运行这些。

这是 Firefox 38.0(但也发生在 39)

【问题讨论】:

  • 您使用的是哪个浏览器和浏览器版本?
  • 用我的浏览器和版本(firefox 38)编辑
  • 尝试使用较低的 Firefox 版本 (33) 或尝试运行一次 HTML 单元驱动程序...看看它是否有效。
    WebDriver driver = new HtmlUnitDriver();
  • 改用driver.quit()
  • 删除driver.close();,只使用driver.quit();

标签: selenium selenium-webdriver webdriver automated-tests testng


【解决方案1】:

这是因为您已经在 @AfterMethod 中关闭了驱动程序。

您实际上是在尝试关闭驱动程序两次。另外,建议不要使用 driver.close(),而是使用 driver.quit() ALWAYS。

这段代码 sn-p 应该可以帮助你:

@AfterMethod
public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        Assert.fail(verificationErrorString);
    }
}

@AfterSuite
public void superTearDown() throws Exception {
    if(driver.getSessionId() != null)
    {
        try{
            driver.quit();
        }catch(Exception e){
          e.printStackTrace();
        }
    }
}

【讨论】:

  • 我尝试了您的 sn-p,但仍然得到相同的“命令无法干净地关闭”,但现在在每种方法之后。看起来driver.quit() 导致错误
  • 它可能是由一些我们当时还没有看到的代码引起的。只有在调用 driver.quit() 时浏览器已经关闭时才会抛出该错误。
【解决方案2】:

最好只在@AfterMethod 中使用 driver.quit()。 @AfterMethod 将在每个测试用例之后终止驱动程序实例。您不需要在@AfterSuite 中堆积驱动程序实例并杀死(除非您有任何特定原因)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多