Getting Started with Selenium Grid

What and Why!

It’s been a while since I blogged anything and here I am about to put my Selenium Grid experience to the vault. If your problem is to either save execution time on a large test suite or is to run several test cases in parallel on different platforms, then Selenium Grid is the answer. Selenium Grid allows you to test any combination of operating system running any browser, in parallel of course.

The Architecture

Selenium Grid is purely a network of hubs and their relevant nodes. A hub would work as the central point establishing control over all the connected nodes. This is where all the scripting would be done. A node on the other hand is any PC on which your test cases would get executed. These nodes could be a Windows machine with a Chrome, a Linux machine with a Firefox, Mac with a Safari or even an Android device. Now, having Android doesn’t mean that we could execute test cases of a mobile application! This rather means that we can execute our web application on a mobile browser. You can find a descriptive image of the architecture at

Throughout this tutorial we’ll be using Selenium Grid  2.0 which has the remote control bundled with the Selenium Server jar file itself. Therefore we’ll be needing only the server jar file to host the grid. Using 2.0 also means that we need not to have an Apache Ant installation as required by 1.0.

First of all let us download and setup the Chrome driver for Google Chrome and Gecko driver for Firefox. So in our example we will be launching our tests on both Google Chrome and Firefox, in parallel! Now that you’ve got a basic idea, let us now get into the setup process!

Setting Up the Grid

Setting up the Web Drivers

In order to launch browsers to load our web application, we first need to setup the relevant web drivers. These web drivers are separate executables that allow us to control the relevant browsers. Since we’ll be running our test suite in this tutorial on both Chrome and Firefox, we’ll be needing the Chrome Driver executable and Gecko Driver executable.

Let us first setup the Chrome driver by following the below mentioned commands. In the below steps we’ll be downloading the Chrome driver, specifying executable permission to the downloaded executable and moving it to the usr/local/share directory. We’ll then be creating symbolic links to the usr/local/bin and usr/bin directories.

Now its time to setup the Gecko driver. Steps here are exactly similar to the ones followed for the Chrome Driver.

Getting the Grid up and running!

Okay now that we got the client drivers covered, let us now get the Selenium server hub up and running. As mentioned earlier, this is where all your scripting would be done at.

  • Download the latest version of Selenium Server.
  • Open a terminal and navigate to the downloaded location.
  • Execute the command shown below to run the server.

java -jar selenium-server-standalone-3.5.3.jar -role hub

Up on successful execution, your terminal would look something like this.


Execution of the above command would start a Jetty server on port 4444. You could pass the parameter “-port” to specify a custom port number of your choice. Now open a browser and visit the page http://localhost:4444/grid/console to verify that server is running without any issues. Your page should look something like this.

Screenshot from 2017-09-23 19-43-16.png

Next up is to start the node! Again, this would be the computer on which the browser would be launched and your tests would get executed. Follow the below mentioned steps to register an instance of the node on the hub that we set up in the above steps.

  • Download the latest version of Selenium driver in the node machines as well.
  • Open a terminal and execute the command given below to run the server on the node machine. Note that your role has now changed to “node”.

java -jar selenium-server-standalone-3.5.3.jar -role node -hub http://<IP of the Hub machine>:4444/grid/register

URL of the hub that is used in the above command would be the one displayed in the terminal for hub that we opened in the earlier step. In that terminal, find the “Nodes should register to” output as shown below.

You could also specify the Chrome driver location with the Selenium server command itself, if you haven’t created a Symbolic link to it. This is a popular option among Windows users as well. So your command to run the node would now look something like this.

java -jar -Dwebdriver.Chrome.driver=<location of the Chrome driver executable> selenium-server-standalone-3.5.3.jar -role node -hub http://<IP of the Hub machine>:4444/grid/register

Up on successful completion of registering the node at the hub your hub terminal would now show the entry shown below. Note that both the hub and node IP addresses are the same for me as I am running both of them in the same machine.


Visiting the URL http://localhost:4444/grid/console in the node machine would now show you a page similar to what has been shown below.

Screenshot from 2017-09-23 19-36-46

The above page shows you the information on how many nodes have got registered (Only 1 node in my case) and how many instances of different browsers could be opened at one go.

Well guess what, that’s all we had to do to setup our Selenium Grid!

Our Grid in action!

Let us now run a couple of tests in parallel and see our grid in action. For this purpose I am going to be using TestNG with Eclipse to write the test suite. Ignore the below steps if you already have an Eclipse with TestNG plugin installed.

Setting up Eclipse with TestNG

  • Download the latest version of Eclipse.
  • Open Eclipse and select a directory to be used as the work space for our project (If you are not familiar with the concept of work space, read through this).
  • Find TestNG plugin in the Eclipse Market place and drag and drop the “Install” button into your Eclipse.
  • Eclipse will now launch a new window with the TestNG plugins listed. Installation steps from here on will be pretty much self explanatory. If you’re having any doubts, read through this tutorial.
  • Once the plugin has been installed, restart your Eclipse software.

Creating the test suite using TestNG

Now that we’ve got the development environment setup, let us now create a test suite that contains two test cases. Let’s write a test case to launch Chrome and navigate to Yahoo page. We’ll validate the title of the page to pass the test. Our next test case is going to be launching Google on a Firefox browser and verifying the title in a similar fashion. First let’s start by creating the project.

  • Go to “File” -> “New” and select “Java Project”.
  • Specify a project name and click “Finish” with other options set to their default values.
  • Expand the project and you will find the “src” directory. This is will act as the source directory where we will be creating the TestNG classes.
  • Right click on the “src” directory and then select “New” -> “Other”.
  • Find and expand “TestNG” directory, select “TestNG class” and click “Next”.
  • Click on “Browse” next to the “Source Folder” and select the “src” directory. As mentioned earlier, this folder will contain our TestNG classes.
  • Specify a package name or you can leave it blank.
  • Specify “TestBase” as the “Class name” and click “Finish”.

Bringing in Maven

Now before we get to the implementation, let us convert the created project to a Maven project. Maven is a build tool which we are going to use to manage our dependencies. By having the dependencies listed through Maven, we won’t be needed to download the required libraries manually and attach to the build path. If you’re not familiar with Maven, here’s a good tutorial.

In order to convert our project to Maven, we need to install the Maven plugin for Eclipse from the Eclipse Market Place. The installation steps would be similar to how we installed TestNG plugin. Once installed, right click on the Java project we created, go to “Configure” and select “Convert to Maven project”. Now you will see that a pom.xml file will be created. Paste the content below to your pom.xml file. All we are doing here is, adding selenium and testng libraries to our project so that we could use their methods.


Now to the Test Suite!

Now let’s resume our implementation. class is where we are going to establish connection to the Hub and specify the capabilities for the platform and browser. These have been parameterized to make it configurable from outside the code and we will be retrieving the values from the TestNG xml (We’ll be creating this XML in while and this will be used to specify the test methods that we’d like to run and the order in which they should get executed.).

public class TestBase
//Declare ThreadLocal Driver for ThreadSafe Tests
protected ThreadLocal<RemoteWebDriver> driver = null;
//Do the test setup
public void setupTest (String browser, String platform) throws MalformedURLException {
//Assign driver to a ThreadLocal
driver = new ThreadLocal<>();
//Set DesiredCapabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
//Set Platform
capabilities.setCapability("platform", platform);
//Set BrowserName
capabilities.setCapability("browserName", browser);
//This is where we are invoking the browser driver
driver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities));

public WebDriver getDriver() {
return driver.get();
public void tearDown() throws Exception {

Now let us create the first test to be executed. Follow the steps we followed above to create another TestNG class and call it “FirstTest”. In this class we are going to write a test to launch the Yahoo home page and verify its title. Content of this class would be as follows.

public class FirstTest extends TestBase {

public void firstTest() throws Exception {
  System.out.println("First Test Started!");
  System.out.println("First Test's Page title is: " + getDriver().getTitle());
  Assert.assertEquals("Yahoo", getDriver().getTitle());
  System.out.println("First Test Ended!");

Now to the second test case to launch through a browser and verify its title.

public class SecondTest extends TestBase{

 public void secondTest() throws Exception {
  System.out.println("Second Test Started!");
  System.out.println("Second Test's Page title is: " + getDriver().getTitle());
  Assert.assertEquals("Google", getDriver().getTitle());
  System.out.println("Second Test Ended!");

Now let’s configure the TestNG xml file to contain the following configuration. Here we will be specifying the test methods to be executed and they’ll get executed in the order they’ve been specified. We will also be passing values to the parameters browser and platform that we had set up in the TestBase class. This allows us to specify on which platform and browser we need to run the relevant test cases.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite thread-count="5" name="Suite" parallel="tests" verbose="1">
 <test name="FirstTest">
  <parameter name="browser" value="chrome"/>
  <parameter name="platform" value="LINUX"/>
    <class name="FirstTest"/>
 </test> <!-- First Test -->
 <test name="SecondTest">
  <parameter name="browser" value="firefox"/>
  <parameter name="platform" value="LINUX"/>
    <class name="SecondTest"/>
 </test> <!-- Second Test -->
</suite> <!-- Suite -->

That’s all for the implementation. Pretty straight forward isn’t it? Now let’s run the test suite and see it in action! Right click on your TestNG.xml and select the “Run Test Suite” option. You will now see that two browsers would be opened. Chrome will be opening and Firefox will be opening, both at the same time! That’s the whole point of the Grid!

What If!

In case you get an empty browser loaded or if your browser doesn’t load at all, verify that you have set up the Chrome driver and Gecko driver properly. Ensure that you are using the latest versions of the drivers as well the browsers. If not, please update your browsers to the latest versions to conform to the driver executable versions. Also if you are running the hub and node on different machines and if you run into any issues when establishing communication between them, make sure that there are no firewall issues. If not, relevant in bound and out bound rules have to be created to allow communication between the two machines.

Well that’s all folks for now. Happy gridding alright! ^_^