Distributed Testing using Selenium Grid Concepts in Virtual Machine

Selenium Grid transparently distributes our tests across multiple physical or virtual machines so that we can run them in parallel, cutting down the time required for running tests in different browsers, operating systems, and machines in parallel.

Here is an example of the Selenium Grid architecture having capabilities to run tests on Linux, Windows, Mac, iOS, and Android platforms:

Selenium Grid

What is a Hub and Node?

The Hub

  • The hub is the central point where you load your tests into.
  • There should only be one hub in a grid.
  • The hub is launched only on a single machine, say, a computer whose O.S is Windows 7 and whose browser is IE.

The Nodes

  • Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
  • There can be one or more nodes in a grid.
  • Nodes can be launched on multiple machines with different platforms and browsers.
  • The machines running the nodes need not be the same platform as that of the hub.

Setting up Selenium Grid Server for parallel Execution.

To Start with the Selenium Grid, we should follow the few steps as follows:

Step 1

Download the Selenium Server Jar from http://www.seleniumhq.org/download/ site.

Selenium Standalone Server

Download version 3.4.0

Step 2

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

We are now going to launch a hub. Go to Virtual Machine A. Using the command prompt, navigate to the root of Virtual Machine A’s — C Drive Software Folder, because that is the directory where we placed the Selenium Server.

On the command prompt, type the following command:

The hub should successfully be launched. Your command prompt should look similar to the image below

Step 3

We can verify whether the hub is running by using a browser. Selenium Grid, by default, uses Machine A’s port 8885 for its web interface. Simply open a browser and go to http://localhost:8885/grid/console

Step 4

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 10.83.0.119 because that is where the hub is running. We also used port 8887 though you may choose any free port number you desire. And Similarly, the chromedriver_win32.exe should be placed in the same folder with the nodeConfig_TestProject.json containing the below properties as Defined below

nodeConfig_ TestProject.json file Properties

Note: Copy the following properties into the notepad and save it as <filename>.json extension into the same folder where selenium standalone server jars is present.

Input Json file

Step 5

Go to the Selenium Grid web interface and refresh the page. You should see something like this.

Go to the Selenium Grid web interface and refresh the page. You should see something like this.

Step 6

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

  • 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.
  • 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.

Step 7

To verify the test case has run successfully we can view in the Virtual Machine B command prompt which will display the fields as below

Advantages of Selenium Grid:

  • Support Multiple Browsers: Chrome, Firefox, IE etc.
  • Support Multiple OS and Architecture: Windows, Linux, Mac OS, UNIX, etc.
  • Speeds up the Test Execution Time: It speeds up the test suite completion time as it is capable of running multiple tests in parallel. For example, if we have 5 nodes and we need to execute a test suite of 50 tests then it is going to take 5 times lesser time than a single machine that runs this test suit without Selenium Grid. When a node is free , it automatically picks up the test case waiting in the execution queue.
  • Cloud Based Execution: Availability, Reliability, Save Hardware and Software Maintenance Cost

Disadvantages of Selenium Grid:

  • Less Secure compared to cloud-based labs: it is less secure than more flexible and powerful cloud-based labs.
  • Poor Scalability: it has relatively poor scalability compared with many modern applications. It certainly is unable to scale up or down on demand.
  • Reconfiguration: it is pretty static. Each server is configured in advance with a subset of the required browser instances available. If you want to vary this, you have to reconfigure.

Test Automation Architect