2

Here's my code to instantiate the webdriver.

System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new ChromeDriver(capabilities);

Now, I'd like to get the port on which the chromedriver has been started. I actually am trying to get automation on OpenFin working and it uses RemoteWebDriver to drive their application using a ChromeDriver.

Here's the link to their Git : https://github.com/openfin/hello-openfin-selenium-java-example

The problem I'm facing is, as I myself have to run the tests on a remote machine, with multiple tests running in parallel, I would require the ability to run multiple instances of chromedriver on the remote machine and then pass its URL with the ports to each chromedriver instance to the RemoteWebDriver.

sagarwadhwa1
  • 878
  • 1
  • 9
  • 15

3 Answers3

0

I'm assuming you're using Selenium Grid to run parallel tests on remote machines. So therefore you can send a http request to the grid and get the port of the browser. The resp variable will hold information about the node. like the ip and port.

HttpHost host = new HttpHost(GRID_IP, GRID_PORT);
        HttpClient client = HttpClientBuilder.create().build();
        URL testSessionApi = new URL("http://" + GRID_IP + ":GRID_PORT/grid/api/testsession?session="
                + driver.getSessionId());
        BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
                testSessionApi.toExternalForm());
        HttpResponse response = client.execute(host, r);
        String resp = EntityUtils.toString(response.getEntity());
ch3m
  • 24
  • 4
  • Yes, that is what I want to do but this returns only the information about the node and not the chromedriver instance which has been run. I would like to get the port through which I can connect to the chromeDriver `newDriver = new RemoteWebDriver(new URL("http://" + GRID_IP + ":" + CHROMEDRIVER_PORT), capabilities);` – sagarwadhwa1 May 26 '15 at 11:28
0

The best way to do this is to use a ChromeDriverService to start the ChromeDriver

ChromeDriverService service = new ChromeDriverService.Builder()
    .usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
    .usingAnyFreePort()
    .build();
service.start();

Then using service.getUrl(); one can easily get the URL which can be used to start the ChromeDriver (for RemoteWebDriver) WebDriver driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome()); or we may directly pass the service in the ChromeDriver constructor WebDriver driver = new ChromeDriver(service);.

sagarwadhwa1
  • 878
  • 1
  • 9
  • 15
-2

Please login to the remote machine where the test needs to be executed and open the task manager to see the Port # on which the chromedriver is started

Hope this helps !!