简体   繁体   中英

Selenium Grid: Use custom browser location with RemoteWebDriver

I am using Selenium grid to run some tests on a remote machine. This is how I start the hub one remote machine A

java -jar /usr/local/lib/selenium-server-standalone-3.141.0.jar -role hub

and this is how I start the node on remote machine 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

This is the class that starts the browser on the remote machine:

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();
}

The above code starts the Firefox browser that is installed on the remote node (machine B). I would like, however, to start a different version of Firefox that resides in "/opt/firefox/firefox". I have tried

caps.setBrowserName("/opt/firefox/firefox");

But this just throws the following exception:

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"
      }
    ]
  }
}

Is there a way to use the browser in /opt/firefox/firefox instead of the installed one with RemoteWebDriver ?

Option A: provide the path to the binary of the version

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

Option B

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

Option C: assign a path to proper version of firefox.exe to webdriver.firefox.bin property

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

Also let me share my GRID config for single (local machine): HUB

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

NODE1:

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

Please let me know if You need .json config here (I may provide them as well).

For aside extent- how and what the difference between web and mobile test automation drivers

Hope this helps.

Your goal is to have a multi version selenium grid. Each node is designed to work with a single browser executable path, either the default for the OS or manually specified as a Java param.

You can launch multiple nodes in your remote machine and connect them to the same hub. Each node can run different browser executable. I would also recommend using docker containers for each node in order to avoid any caching issues that the browser is doing behind the scenes or at least use a custom browser profile for each execution type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM