top of page

Learn Selenium Grid


The Problem

If you’re looking to run your tests on different browsers and operating system combinations but you’re unable to justify using a third-party solution like Sauce Labs or Browser Stack then what do you do?


The Solution

With Selenium Grid you can create a network of connected test machines (also called nodes). This network of test machines is controlled by a Hub, using which you can run your tests on different connected nodes. Each node is basically a computer (even a virtual machine) with a combination of Operating system and Browsers. This enables us to create a network of test machines with varying combinations of Operating system and browsers. Using Selenium Grid you can run tests on a variety of Operating System and Browser combinations. Lets understand Selenium Grid in more details below


What is Selenium Grid?

The Selenium Grid is a testing tool which allows us to run our tests on different machines against different browsers. It is a part of the Selenium Suite which specialise in running multiple tests across different browsers, operating system and machines. You can connect to it with Selenium Remote by specifying the browser, browser version, and operating system you want. You specify these values through Selenium Remote’s Capabilities.


There are two main elements to Selenium Grid — a hub, and nodes.


What is a Hub?

In Selenium Grid, the hub is a computer which is the central point where we can load our tests into. Hub also acts as a server because of which it acts as a central point to control the network of Test machines. The Selenium Grid has only one hub and it is the master of the network. When a test with given DesiredCapabilities is given to Hub, the Hub searches for the node witch matches the given configuration. For example, you can say that you want to run the test on Windows 10 and on Chrome browser with verision XXX. Hub will try to find a machine in the Grid which matches the criterion and will run the test on that Machine. If there is no match, then hub returns an error. There should be only one hub in a Grid.


What is a Node?

In Selenium Grid, a node is referred to a Test Machine which opts to connect with the Hub. This test machine will be used by Hub to run tests on. A Grid network can have multiple nodes. A node is supposed to have different platforms i.e. different operating system and browsers. The node does not need the same platform for running as that of hub.


How it works?

First you need to create a hub. Then you can connect (or “register”) nodes to that hub. Nodes are where your tests will run, and the hub is responsible for making sure your tests end up on the right one (e.g., the machine with the operating system and browser you specified in your test).


Why the Selenium Grid is used?

With Selenium Grid you can create a simple infrastructure of various browsers on different operating systems to not only distribute test load, but also give you a diversity of browsers to work with.


The Selenium Grid is used because of many reasons. Here are a few:

  • When we want to run our tests against multiple browsers, the multiple versions of browsers and the browsers running on different operating system.

  • It is also used to reduce the time taken by the test suite to complete a test pass by running tests in parallel.


Architecture and RemoteWebDriver WorkFlow

You can use RemoteWebDriver the same way you would use WebDriver locally. The primary difference is that RemoteWebDriver needs to be configured so that it can run your tests on a separate machine. The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server.


  • RemoteWebDriveris an implementation class of the WebDriver interface that a test script developer can use to execute their test scripts via the RemoteWebDriver server on a remote machine.

  • There are two parts to RemoteWebDriver: a server(hub) and a client(node)

  • The RemoteWebDriverserver is a component that listens on a port for various requests from a RemoteWebDriver Once it receives the requests, it forwards them to any of the following: Firefox Driver, IE Driver, or Chrome Driver, whichever is asked.

  • The language-binding client libraries that serve as a RemoteWebDriver The client, as it used to when executing tests locally, translates your test script requests to JSON payload and sends them across to the RemoteWebDriverserver using the JSON wire protocol.

  • When you execute your tests locally, the WebDriver client libraries talk to your Firefox Driver, IE Driver, or Chrome Driver directly. Now, when you try to execute your tests remotely, the WebDriver client libraries talk to the RemoteWebDriverserver and the server talks to either the Firefox Driver, IE Driver, or Chrome Driver, whichever the WebDriver client asks for.


How to Set Up Selenium Grid? Using Command Line


In this section, you will use 2 machines. The first machine will be the system that will run the hub while the other machine will run a node. For simplicity, let us call the machine where the hub runs as "Machine A" while the machine where the node runs will be "Machine B." It is also important to note their IP addresses. Let us say that Machine A has an IP address of 192.168.1.3 while Machine B has an IP of 192.168.1.4.


Step 1:

Download the Selenium Server from

http://docs.seleniumhq.org/download/


Step 2:

You can place the Selenium Server .jar file anywhere in your HardDrive. But for the purpose of this tutorial, place it on the C drive of both Machine A and Machine B. After doing this, you are now done installing Selenium Grid. The following steps will launch the hub and the node.


Step 3:

We are now going to launch a hub. Go to Machine A. Using the command prompt, navigate to the root of Machine A's - C drive, because that is the directory where we placed the Selenium Server.On the command prompt, type


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


The hub should successfully be launched.


Step 4:

Another way to verify whether the hub is running is by using a browser. Selenium Grid, by default, uses Machine A's port 4444 for its web interface. Simply open up a browser and go to http://localhost:4444/grid/console


Also, you can check if Machine B can access the hub's web interface by launching a browser there and going to where "iporhostnameofmachineA" should be the IP address or the hostname of the machine where the hub is running. Since Machine A's IP address is 192.168.1.3, then on the browser on Machine B you should type http://192.168.1.3:4444/grid/console


Step 5:

  • Now that the hub is already set up, we are going to launch a node. Go to Machine B and launch a command prompt there.

  • Navigate to the root of Drive C and type the code below. We used the IP address 192.168.1.3 because that is where the hub is running. We also used port 5566 though you may choose any free port number you desire.

  • NOTE: You now have to give path to the Gecko driver if using Firefox. Here is updated code that needs to be used


java -Dwebdriver.gecko.driver="C:\geckodriver.exe" -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub http://192.168.1.3:4444/grid/register -port 5566


When you press Enter, node gets started


Step 6:

Go to the Selenium Grid HUB web interface and refresh the page. You should see detailed of registered node.


Open below URL to check node registration at Hub Machine:

http://localhost:4444/grid/console


At this point, you have already configured a simple grid. You are now ready to run a test remotely on Machine B.



Designing Test Scripts That Can Run on the Grid


To design test scripts that will run on the grid, we need to use DesiredCapabilites and the RemoteWebDriver objects.

  • DesiredCapabilites is used to set the type of browser and OS that we will automate

  • RemoteWebDriver is used to set which node (or machine) that our test will run against.

To use the DesiredCapabilites object, you must first import this package

import org.openqa.selenium.remote.DesiredCapabilities;


To use the RemoteWebDriver object, you must import these packages.

import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.remote.RemoteWebDriver;


Using the DesiredCapabilites Object

We will use the platform and the browserName in our WebDriver as shown below (of course you need to import the necessary packages first).


DesiredCapabilities capability = DesiredCapabilities.firefox();

capability.setBrowserName("firefox");

capability.setPlatform(Platform.XP);


Using the RemoteWebDriver Object

Import the necessary packages for RemoteWebDriver and then pass the DesiredCapabilities object that we created above as a parameter for the RemoteWebDriver object.


Webdriver driver;

driver = new RemoteWebDriver(

new URL("http://192.168.1.4:5556/wd/hub"),capability);



Running a Sample Test Case on the Grid

Below is a simple WebDriver Testng code that you can create in Eclipse on Machine A. Once you run it, automation will be performed on Machine B.



The test should pass.


Building better QA for tomorrow

Comentários


bottom of page