Selenium Grid:通过RemoteWebDriver使用自定义浏览器位置

时间:2019-05-12 09:07:54

标签: java selenium selenium-webdriver selenium-grid selenium-remotedriver

我正在使用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中安装浏览器?

2 个答案:

答案 0 :(得分:0)

选项A:提供版本二进制文件的路径

FirefoxBinary binary = new FirefoxBinary(new File("path_to_bin"));
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(binary, profile);

选项B

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
 WebDriver driver = new FirefoxDriver();

选项C:将适当的firefox.exe版本的路径分配给webdriver.firefox.bin属性

System.setProperty("webdriver.firefox.bin", "c:\\path\\to\\firefox.exe");

还让我为单个(本地计算机)共享我的GRID配置: HUB

java -jar selenium-server-standalone-3.11.0.jar -role hub -hubConfig hub.json & pause

节点1:

java -jar -Dwebdriver.gecko.driver=geckodriver.exe -Dwebdriver.chrome.driver=chromedriver.exe selenium-server-standalone-3.11.0.jar -role node -nodeConfig node1.json & pause

NODE2:

java -jar -Dwebdriver.gecko.driver=geckodriver.exe -Dwebdriver.chrome.driver=chromedriver.exe selenium-server-standalone-3.11.0.jar -role node -nodeConfig node2.json & pause

请告诉我您是否需要.json配置(我也可以提供)。

暂时来说,and what the difference between web and mobile test automation drivers

希望这会有所帮助。

答案 1 :(得分:0)

您的目标是拥有一个多版本的硒网格。每个节点都设计为使用单个浏览器可执行路径,该路径可以是操作系统的默认路径,也可以手动指定为Java参数。

您可以启动远程计算机中的多个节点,并将它们连接到同一集线器。每个节点可以运行不同的浏览器可执行文件。我还建议为每个节点使用docker容器,以避免浏览器在后台执行任何缓存问题,或者至少对每种执行类型使用自定义浏览器配置文件。