我正在使用 Selenium 网格在远程机器上运行一些测试。这就是我启动集线器一台远程机器A的方式
java -jar /usr/local/lib/selenium-server-standalone-3.141.0.jar -role hub
这就是我在远程机器 B 上启动节点的方式
java -Dwebdriver.gecko.driver="/opt/foxdriver" -jar "$HOME/selenium-server-standalone-3.141.0.jar" -role webdriver -hub "http://192.168.100.99:4444/grid/register/" -port 9999
这是在远程机器上启动浏览器的类:
package seleniumgrid;
import static java.lang.Thread.sleep;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Seleniumgrid {
WebDriver driver;
String baseURL, nodeURL;
public static void main(String[] args)
throws MalformedURLException, InterruptedException {
Seleniumgrid grid = new Seleniumgrid();
grid.doit();
}
void doit() throws MalformedURLException, InterruptedException {
String nodeURL = "http://192.168.100.100:9999/wd/hub";
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setBrowserName("firefox");
caps.setPlatform(Platform.LINUX);
driver = new RemoteWebDriver(new URL(nodeURL), caps);
sleep(60000L);
driver.quit();
}
上面的代码启动安装在远程节点(机器 B)上的 Firefox 浏览器。但是,我想启动位于“/opt/firefox/firefox”中的不同版本的 Firefox。我努力了
caps.setBrowserName("/opt/firefox/firefox");
但这只会引发以下异常:
INFO: Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create session from {
"desiredCapabilities": {
"browserName": "\u002fopt\u002fautobrowse\u002ffirefox\u002ffox\u002ffirefox",
"version": "",
"platform": "LINUX",
"acceptInsecureCerts": true
},
"capabilities": {
"firstMatch": [
{
"acceptInsecureCerts": true,
"browserName": "\u002fopt\u002fautobrowse\u002ffirefox\u002ffox\u002ffirefox",
"platformName": "linux"
}
]
}
}
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:22:52'
System info: host: 'machineB', ip: '192.168.100.100', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.172', java.version: '1.8.0_192'
Driver info: driver.version: unknown
Command duration or timeout: 202 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:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$errorHandler$0(JsonWireProtocolResponse.java:54)
at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
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:125)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
at seleniumgrid.Seleniumgrid.doit(Seleniumgrid.java:29)
at seleniumgrid.Seleniumgrid.main(Seleniumgrid.java:18)
Caused by: org.openqa.selenium.SessionNotCreatedException: Unable to create session from {
"desiredCapabilities": {
"browserName": "\u002fopt\u002fautobrowse\u002ffirefox\u002ffox\u002ffirefox",
"version": "",
"platform": "LINUX",
"acceptInsecureCerts": true
},
"capabilities": {
"firstMatch": [
{
"acceptInsecureCerts": true,
"browserName": "\u002fopt\u002fautobrowse\u002ffirefox\u002ffox\u002ffirefox",
"platformName": "linux"
}
]
}
}
有没有办法使用浏览器/opt/firefox/firefox
代替已安装的浏览器RemoteWebDriver
?