1

我在一套导致广泛故障的 WebDriver 测试用例中遇到了一个奇怪的问题。

该套件中的一个测试用例目前预计会失败 - 网站中有一个未解决的错误导致选项卡消失。应该单击选项卡的测试用例调用 WebDriver 的 findElement 方法来查找选项卡。它等待标准超时,但随后,我没有抛出通常的异常并继续前进,而是开始在控制台中获取 SocketExceptions,这些异常不会被通常捕获错误的测试用例代码捕获:

Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: No buffer space available (maximum connections reached?): connect
Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect 
INFO: Retrying connect 
Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect 
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: No buffer space available (maximum connections reached?): connect 
Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect 
INFO: Retrying connect 
Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect 
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: No buffer space available (maximum connections reached?): connect 
Apr 18, 2013 8:47:23 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect 
INFO: Retrying connect

SocketExceptions 导致 UnreachableBrowserException:

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 22:18:01'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_32'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:493)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:502)
    at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:372)
    at com.brainshark.uitests.common.TestBase.afterClass(TestBase.java:243)
    at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at com.brainshark.uitests.runners.BrainsharkTestRunner.run(BrainsharkTestRunner.java:51)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:319)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:297)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:136)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:357)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:472)
    ... 32 more

这会导致每个测试用例快速失败,并且在大约 10 秒内,每次尝试运行下一个测试用例都会触发上述失败。然后,神奇的事情发生了,异常停止了。

有谁知道我为什么会看到这种行为?或者什么需要 10 秒才能重置?我是否应该通过尝试检测此类情况并在尝试执行下一个测试用例之前保持静止 10 秒来解决此问题?谢谢!

4

1 回答 1

2

我想到了。SocketExceptions 是由在太短的时间内进行太多 findElement 调用引起的。

我有一个自定义的查找方法,除了 By 定位器之外,它还允许匹配文本模式等附加参数。在某些情况下(即,如果存在与 By 定位符匹配但与文本模式不匹配的元素,例如在这种情况下,有多个选项卡,但缺少我要查找的文本的选项卡)此方法可以使重复findElement 在其超时期间调用。

我从来没有注意到开发中的问题,因为我在编写测试时使用了 20 秒的超时,所以它们失败得更快。在我们的实际运行中,我们使用了 180 秒的超时时间,这足以让 Selenium 使用 findElement 调用过载。

我添加了一些睡眠,以确保我最多每四分之一秒调用一次 findElement,并且还放入了一个 try/catch 来捕获如果仍然发生过载将导致的 UnreachableBrowserException。如果是这样,我只需睡 40 秒,然后再试一次。它现在似乎运作良好。

于 2013-04-18T14:57:04.137 回答