Selenium Grid | RemoteWebDriver and DesiredCapabilities

In the Previous Tutorial, you learned how to implement the Selenium Grid architecture. In this tutorial, I’ll write code to utilise that setup and run the test on a node.

In the Selenium grid architecture, only the steps to setup test is different. Once you have launched the browser, rest of the WebDriver code is same as you have used so far.

Cheers, only the code to specify browser name, version, Platform and other environment details is different. Once you have launched browser rest all code are same as normal WebDriver code.

DesiredCapabilities

The DesiredCapabilties object specifies the desired environment capabilities that we want for the test. For instance, let us say we want to run our test on a Windows machine against Google Chrome browser:

You need to import this package at the top of the file:
import org.openqa.selenium.remote.DesiredCapabilities;

Next, instantiate DesiredCapabilities and specify all capabilities that we want for the test:

DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setPlatform(Platform.WINDOWS);
caps.setVersion("60");
You need to import this package at the top of the file:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

Next, instantiate DesiredCapabilities and specify all capabilities that we want for the test:

desired_caps = DesiredCapabilities.CHROME

The DesiredCapabilities is actually a dictionary. So, in addition to the default properties, you can also add the values explicitly:

desired_caps = DesiredCapabilities.CHROME.copy()
desired_caps['platform'] = "WINDOWS"
desired_caps['version'] = "60"

RemoteWebDriver

We need to tell WebDriver client the URL of the hub so that it can send the test commands to that server. The hub will further route it to one of the registered nodes with the matching capabilities as we specified above. As you remember in the Previous Tutorial, we started a Hub in the local machine.

Let us now write code to launch the browser. For that, we need to instantiate RemoteWebDriver and pass it the URL of the hub and the DesiredCapabilities object.

//Create a URL. We need to handle MalformedURLException
URL urlHub = new URL("http://127.0.0.1:4444/wd/hub");

WebDriver driver = new RemoteWebDriver(urlHub, caps);
grid_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=grid_url)
  • Now we may write normal WebDriver code to navigate to a URL and then perform various actions:
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("teachmeselenium");
driver.quit();
driver.get("https://cosmocode.io/automation-practice/")
driver.maximize_window()
driver.find_element_by_id("firstname").send_keys("teachmeselenium")
driver.quit()

Complete code

Before running the code you need to make sure that the hub and node is running as per the Previous Tutorial . Also, make sure at least one node has the matching browser-platform combination as specified by DesiredCapabilities object in your test.

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SeleniumGridPractice {
public static void main(String[] args) {

//Set your browser driver path
DesiredCapabilities caps = DesiredCapabilities.chrome();

//caps.setVersion("20");

caps.setPlatform(Platform.WINDOWS);

URL urlHub = null;

try {

urlHub = new URL("http://127.0.0.1:4444/wd/hub");

} catch (MalformedURLException e) {

e.printStackTrace();

}

WebDriver driver = new RemoteWebDriver(urlHub, caps);

driver.get("http://www.google.com");

driver.findElement(By.name("q")).sendKeys("teachmeselenium");

driver.quit();

}

}
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_caps = DesiredCapabilities.CHROME
grid_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=grid_url)
driver.get("https://cosmocode.io/automation-practice/")
driver.maximize_window()
driver.find_element_by_id("firstname").send_keys("teachmeselenium")
driver.quit()

Please update the DesiredCapabilities object in the code as per the platform, browser and browser version of the node.

That is Ok but isn’t the whole grid-node setup is so confusing and a lot of work.

That is why we have some providers that provide ready-to-use infrastructure over the cloud. We’ll cover it in the Next Tutorial.

12 thoughts on “Selenium Grid | RemoteWebDriver and DesiredCapabilities”

  1. If I want to run the different versions on a single browser, then how can I define the DesiredCapabilities caps = DesiredCapabilities.firefox() and how to define remote webdriver and assign that to selenium webdriver? Could you please explain?

    Reply
    • If I understand you correctly you want to run a test on different versions of the same browser. Is this correct? If yes, here are the possibilities – (There are better options to do so with TestNG, but we’ll ignore that for this tutorial and stick to basics.)
      1. Created different virtual machines inside the same host machine (or real machines in the same network) with different browser version. Let’s say VM1 has Chrome 20, VM2 has Chrome 30
      2. Register all VMs as nodes to the host machine (Hub).
      3. Create multiple DesiredCapabilities object with varying configurations. ()
      DesiredCapabilities caps = DesiredCapabilities.firefox();
      caps.setVersion(“20”);

      DesiredCapabilities caps2 = DesiredCapabilities.firefox();
      caps2.setVersion(“30”);
      4. Wrap the test steps inside a function and make it receive a Capability object and use it to launch driver.
      public void myFunc(DesiredCapabilities caps){
      WebDriver driver = new RemoteWebDriver(urlHub, caps);
      driver.get(“http://www.google.com”);
      }
      5. Call the function twice with diferent caps
      myFunc(caps);
      myFunc(caps2);

      I must repeat myself – There are better options to do so with TestNG, but we’ll ignore that for this tutorial and stick to basics.
      Hope it helps.

      Reply
  2. Hi Shadab,
    My script is unable to launch the browser, it is taking eternal time to launch the browser without any exceptions, though hub and node is successfully registered and running. Can you please help me out. Here is my script:

    public class SeleniumGrid {

    public static void main(String[] args) {

    System.setProperty(“webdriver.gecko.driver”,”./Drivers/geckodriver.exe”);
    DesiredCapabilities caps = DesiredCapabilities.firefox();

    System.out.println(“1”);

    URL hubUrl = null;
    try {

    hubUrl = new URL(“http://192.168.43.90:4444/wd/hub”);

    } catch (MalformedURLException e) {

    e.printStackTrace();

    }
    System.out.println(“2”);

    WebDriver driver = new RemoteWebDriver(hubUrl ,caps);

    System.out.println(“3”);
    driver.get(“http://www.google.com”);
    driver.quit();

    }}

    Reply
    • When you refresh the hub console what are you seeing? Also, is your hub and node registered on the same machine or different machines? Can you try with hubUrl = new URL(“http://127.0.0.1:4444/wd/hub”);

      Reply
      • Hi Shadab,

        Hub and node are on the same machine.
        Now I am able to launch the browsers, chrome and firefox.
        I entered the following content in the cmd to run the node,

        java -Dwebdriver.gecko.driver=”C:/Automation/geckodriver.exe” -Dwebdriver.chrome.driver=”C:/Automation/chromedriver.exe” -jar C:\Users\schadichal\Downloads/selenium-server-standalone-3.11.0.jar -role webdriver -hub http://192.168.1.3:4444/grid/register -port 5566

        and for hub, I just executed the following command in cmd,

        java -jar C:\Users\schadichal\Downloads/selenium-server-standalone-3.11.0.jar -role hub

        But the problem what I am facing is, the script is getting executed sequentially.
        First in Firefox and then in chrome not simultaneously.
        Can you please help.

        Here is my script:

        public class SeleniumGrid {

        public static void main(String[] args) {

        /*System.setProperty(“webdriver.gecko.driver”,”./Drivers/geckodriver.exe”);
        System.setProperty(“webdriver.chrome.driver”,”./Drivers/chromedriver.exe”);*/
        DesiredCapabilities caps = DesiredCapabilities.firefox();
        DesiredCapabilities caps1 = DesiredCapabilities.chrome();

        System.out.println(“1”);

        URL urlHub = null;
        try {

        urlHub = new URL(“http://10.27.208.12:5566/wd/hub”);

        } catch (MalformedURLException e) {

        e.printStackTrace();

        }

        System.out.println(urlHub);

        WebDriver driver = new RemoteWebDriver(urlHub,caps);
        WebDriver driver1 = new RemoteWebDriver(urlHub,caps1);

        System.out.println(“3”);
        driver.get(“http://www.google.com”);
        driver1.get(“http://www.google.com”);
        }}

        Reply
        • Hi Santosh

          Congratulations on fixing the failure yourself.

          Now, for your second issue, your script is working as expected. Just registering a hub-node setup does not guarantee parallel execution. You’ll have to write code to start two new Java threads and start both threads parallelly. In one thread you’ll use DesiredCapabilities.firefox() while in another DesiredCapabilities.chrome(). Alternatively, you can use TestNG and make two different tests run parallel by using parallel=”methods” in testng.xml file.

          Thanks
          Shadab

          Reply

Leave a Reply