WebDriver.findElement导致SocketExceptions 10秒

时间:2013-04-18 14:00:51

标签: selenium webdriver httpclient

我遇到了一系列导致广泛失败的WebDriver测试用例的奇怪问题。

该套件中的一个测试用例目前预计会失败 - 网站中存在一个导致标签消失的漏洞。应该单击选项卡的测试用例调用WebDriver的findElement方法来查找选项卡。它等待标准超时,但是,然后,我开始在控制台中获取SocketExceptions而不是通常捕获错误的测试用例代码:#/ p>

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秒来解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

我明白了。 SocketExceptions是由于在一段时间内制作了太多的findElement调用引起的。

我有一个自定义的查找方法,除了By定位器之外,还允许匹配其他参数,如文本模式。在某些情况下(即,如果有与By定位器匹配但不符合文本模式的元素,例如在这种情况下有多个选项卡,但是那个带有我正在查找的文本的元素丢失了)这个方法可以重复findElement在超时期间调用。

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

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