测试失败 - 使用 Selenium Grid 启动

Tests fail - launched with SeleniumGrid

硒网格2 Selenium Grid Hub v.3.4.0 硒版本 3.4.0 Chrome 版本 v:59.0.3071.115

    org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
Command duration or timeout: 674 milliseconds
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementsById(RemoteWebDriver.java:457)
    at org.openqa.selenium.By$ById.findElements(By.java:210)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
    at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.openqa.selenium.remote.Augmenter$CompoundHandler.intercept(Augmenter.java:189)
    at org.openqa.selenium.remote.RemoteWebDriver$$EnhancerByCGLIB$2ca716.findElements(<generated>)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:882)
    at org.openqa.selenium.support.ui.ExpectedConditions.access[=10=]0(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:206)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:202)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-514.6.2.el7.x86_64', java.version: '1.8.0_111'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementsById(RemoteWebDriver.java:457)
    at org.openqa.selenium.By$ById.findElements(By.java:210)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
    at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.openqa.selenium.remote.Augmenter$CompoundHandler.intercept(Augmenter.java:189)
    at org.openqa.selenium.remote.RemoteWebDriver$$EnhancerByCGLIB$2ca716.findElements(<generated>)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:882)
    at org.openqa.selenium.support.ui.ExpectedConditions.access[=10=]0(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:206)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:202)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils.call(Utils.java:40)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:34)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=10=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=10=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.testng.junit.JUnit4TestRunner.start(JUnit4TestRunner.java:81)
    at org.testng.junit.JUnit4TestRunner.run(JUnit4TestRunner.java:69)
    at org.testng.TestRunner.run(TestRunner.java:682)
    at org.testng.TestRunner.runWorkers(TestRunner.java:1005)
    at org.testng.TestRunner.privateRunJUnit(TestRunner.java:713)
    at org.testng.TestRunner.run(TestRunner.java:614)
    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: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.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:132)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSingleClass(TestNGDirectoryTestSuite.java:112)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:147)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementsById(RemoteWebDriver.java:457)
    at org.openqa.selenium.By$ById.findElements(By.java:210)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
    at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.openqa.selenium.support.events.EventFiringWebDriver.invoke(EventFiringWebDriver.java:104)
    at com.sun.proxy.$Proxy6.findElements(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElements(EventFiringWebDriver.java:177)
    at org.openqa.selenium.remote.server.handler.FindElements.call(FindElements.java:50)
    at org.openqa.selenium.remote.server.handler.FindElements.call(FindElements.java:35)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.openqa.selenium.remote.server.DefaultSession.run(DefaultSession.java:176)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

我的 remoteDriver 方法

private WebDriver getRemoteDriver(Browser browser) throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = getCapabilities(browser);
        desiredCapabilities.setCapability("platform", "LINUX");

        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        final RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteUrl), desiredCapabilities);
        remoteWebDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        remoteWebDriver.setFileDetector(new LocalFileDetector());

        return new Augmenter().augment(remoteWebDriver);
    }

问题随机出现,但只有当我 运行 通过 Jenkins->Grid 进行测试时才会出现。当我 运行 他们在本地时,他们毫无问题地通过了。对于上述问题,我在调用 driver.quit()

时收到错误
    org.openqa.selenium.NoSuchSessionException: Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
 os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-514.6.2.el7.x86_64', java.version: '1.8.0_131'
    Driver info: driver.version: unknown
    Command duration or timeout: 70 milliseconds

当我升级版本的 Selenium Grid 时一切都开始了(我尝试在没有 SG 降级的情况下解决它)。 谁能帮忙?谢谢

您提到您的集线器配置如下

-browserTimeout 0 -debug false -help false -jettyMaxThreads -1 -port 4444 -role hub 
-timeout 10 -cleanUpCycle 5000 -host 172.18.0.1 -maxSession 5 
-hubConfig /opt/selenium/config.json 
-capabilityMatcher org.openqa.grid.internal.utils.DefaultCapabilityMatcher 
-newSessionWaitTimeout -1 -throwOnCapabilityNotPresent true 

您已将 -timeout 配置为 10 seconds,这意味着集线器发现特定会话空闲时间超过 10 秒它将从服务器端清理。

更糟糕的是,您设置了 30 秒的隐式超时 remoteWebDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

如果您查看您的堆栈跟踪,您会发现您的连接被拒绝错误都源于调用 driver.findElements() 的地方,并且 implicit waits 恰好影响了这个地方。

所以这里发生的事情是,如果没有立即找到元素,您的代码会要求 webdriver 服务器组件 (chromedriver/geckodriver/IEDriverServer) 等待 30 秒(轮询),并且在网格端,您已经配置了集线器并指示它清理任何闲置超过 10 秒的测试会话(即超过 10 秒没有 JSONWireProtocol 命令被发送)您对节点的测试)。因此,您的集线器会清理您的会话,最终也会摆脱您的浏览器和服务器组件,并且您的测试会尝试与不再存在的服务器通信。

要解决此问题,我建议您执行以下操作:

  1. 摆脱隐式等待,改用显式等待。
  2. 如果您绝对必须使用隐式等待,请将您的 -timeout 值提高到更高的值或将其保留为默认值(即 1800 秒)

试一试,让我知道进展如何。

PS : 您的代码中也不需要 Augmenter,因为您没有以任何方式丰富 webdriver 并且 Augmenter returns 返回相同的 RemoteWebDriver 如果它发现它是一个 RemoteWebDriver 实例(在你的情况下是这样)