我想在多台机器中配置多个Firefox浏览器来执行并行测试。
提前致谢。
Firefox版本:60.0.1 Selenium Firefox驱动程序:3.7.1 Geckdriver版本:v0.19.0
System.setProperty("webdriver.gecko.driver","C:\\Fm2\\Try_Browsers\\Browsers\\drivers\\geckodriver_19.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.close();
我在不同的bat文件中设置了网格和节点: HUb设置: java -jar selenium-server-standalone-3.1.0.jar -port 4444 -role hub 节点设置: java -Dwebdriver.gecko.driver =。/ geckodriver_19.exe -jar selenium-server-standalone-3.1.0.jar -port 5557 -role node -hub http://localhost:4444/grid/register -browser“browserName = firefox,maxInstances = 10,platform = ANY, seleniumProtocol =的webdriver”
System.setProperty("webdriver.gecko.driver","C:\\Fm2\\Try_Browsers\\Browsers\\drivers\\geckodriver.exe");
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.close();
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{acceptInsecureCerts=true, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@21fa0ed7, version=, platform=ANY, firefox_profile=org.openqa.selenium.firefox.FirefoxProfile@49991a02}], required capabilities = Capabilities [{}]
Build info: version: '3.1.0', revision: '86a5d70', time: '2017-02-16 07:57:44 -0800'
System info: host: 'LAPTOP-9JJJC37G', ip: '192.168.100.205', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: FirefoxDriver
Command duration or timeout: 9.72 seconds
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:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0(JsonWireProtocolResponse.java:53)
at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2(JsonWireProtocolResponse.java:91)
at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:155)
at Browsers.callBrowsers.sFireFox(callBrowsers.java:45)
at Browsers.callBrowsers.main(callBrowsers.java:27)
18:16:02.462 INFO - Executing: [new session: Capabilities [{acceptInsecureCerts=true, browserName=firefox, version=, platform=ANY}]])
18:16:02.488 INFO - Creating a new session for Capabilities [{acceptInsecureCerts=true, browserName=firefox, version=, platform=ANY}]
1531124162793 geckodriver INFO geckodriver 0.19.1
1531124162811 geckodriver INFO Listening on 127.0.0.1:13887
1531124163451 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\Viki\\AppData\\Local\\Temp\\rust_mozprofile.TtfJrIS5pIAk"
Unable to read VR Path Registry from C:\Users\Viki\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\Viki\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\Viki\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\Viki\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\Viki\AppData\Local\openvr\openvrpaths.vrpath
1531124171024 Marionette INFO Listening on port 50055
18:16:11.966 WARN - Exception thrown
java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '3.1.0', revision: '86a5d70', time: '2017-02-16 07:57:44 -0800'
System info: host: 'LAPTOP-9XXXC39G', ip: '122.148.110.105', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: unknown
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:183)
at org.openqa.selenium.remote.server.DefaultSession.<init>(DefaultSession.java:119)
at org.openqa.selenium.remote.server.DefaultSession.createSession(DefaultSession.java:95)
at org.openqa.selenium.remote.server.DefaultDriverSessions.newSession(DefaultDriverSessions.java:131)
at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:59)
at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:36)
at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:111)
at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:189)
at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:222)
at org.openqa.selenium.remote.server.DriverServlet.doPost(DriverServlet.java:184)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:150)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.seleniumhq.jetty9.servlet.ServletHolder.handle(ServletHolder.java:808)
at org.seleniumhq.jetty9.servlet.ServletHandler.doHandle(ServletHandler.java:587)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.seleniumhq.jetty9.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.seleniumhq.jetty9.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.seleniumhq.jetty9.server.Server.handle(Server.java:499)
at org.seleniumhq.jetty9.server.HttpChannel.handle(HttpChannel.java:310)
at org.seleniumhq.jetty9.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.seleniumhq.jetty9.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:748)
我看到几件可能导致你错误的事情。首先,版本3.1.0很旧,你应该使用3.14.0,geckodriver也已经使用版本0.21.0。对于最新的Realeses,请看这里:seleniumhq Secound,你应该使用FirefoxOptions作为RemoteDriver(UrlToHub,FirefoxOptions)
的传输参数,并事先将你的DesiredCapabilities合并到FirefoxOptions中。正如我发现的最后一个问题,你使用http://localhost:4444/wd/hub
作为网址。当您启动集线器时,您将看到要在控制台中注册的URL,该URL不在您的IP池中,您也应该使用它。要启动集线器,您只需要命令行命令:cd pathToSeleniumStandAloneServerFolder
,启动命令为:java -jar selenium-standalone-<version>.jar -role hub
端口“4444”是默认值
然后,您将看到连接RemoteWebDriver()
的正确URL。它看起来像这样:
c:usr\Downloads>java -jar "selenium-server-standalone-3.14.0.jar" -role hub
12:56:40.723 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.14.0', revision: 'aacccce0'
12:56:40.725 INFO [GridLauncherV3$2.launch] - Launching Selenium Grid hub on port 4444
2018-09-07 12:56:41.063:INFO::main: Logging initialized @588ms to org.seleniumhq.jetty9.util.log.StdErrLog
12:56:41.447 INFO [Hub.start] - Selenium Grid hub is up and running
12:56:41.448 INFO [Hub.start] - Nodes should register to http://192.168.56.1:4444/grid/register/
12:56:41.448 INFO [Hub.start] - Clients should connect to http://192.168.56.1:4444/wd/hub
最后一行是RemoteWebDriver()
的可连接URL
现在让我们开始使用NODE:
java -Dwebdirver.gecko.driver="PathToYourFirefoxDriver" -jar selenium-standalone-server-<versio>.jar -role node -hub http:UrlToHub:port/grid/register
看看这个例子:
public WebDriver getWebDriver(final DesiredCapabilities desiredCapabilities) {
FirefoxOptions firefoxOptions;
DesiredCapabilities capabilities = new DesiredCapabilities()
capabilities.merge(desiredCapabilities);
capabilities.setPlatform(Platform.ANY);
capabilities.setBrowserName("firefox");
firefoxOptionHelper.merge(capabilities);
try {
WebDriver webDriver = new RemoteWebDriver(new URL(this.gridHubUrl), firefoxOptions);
return webDriver;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
希望它有所帮助。