In this post, I will describe how to use Selenium Grid.

First of all, you can access Selenium Grid page from below address.

https://github.com/SeleniumHQ/selenium/wiki/Grid2

Grid allows you to:

  • Scale by distributing tests on several machines ( parallel execution )
  • Manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • Minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance. [1]

Now, please go to http://www.seleniumhq.org/download/ and download Selenium Standalone Server.

grid

Then, copy the .jar file to C:\Selenium\grid\ folder.

What is Hub and Nodes?

Basically, we have a Hub which is a server that we connect from our tests and we have Nodes, they can be on different machines and they register with the hub. Simply, we have a hub and several nodes, nodes are registered our hub and hub knows that which browsers are available. Hub sends requests to the nodes based on desired capabilities and execute the tests.

How to Start and Configure Hub and Nodes

The best way to start hub and node with .bat files for Windows also it is easier and tidier to configure them with .json files.

First, lets start the hub by command window.

Hub Start Command: java -jar selenium-server-standalone-3.0.1.jar -role hub

and you can see the hub panel on your browser as follows.

Let’s start to node

We can start the node with command window as below command.

Node Start Command: java -jar selenium-server-standalone-3.0.1.jar -role node -hub http://localhost:4444/grid/register

and when you open the grid console you can see that the node registered with the hub.

selenium grid

When we wanted to use selenium grid in our tests we need to tell our grid where the required browser executable is located. Thus, we should start to node as below command before writing our tests. We added the blue part.

Node Start Command with Chrome Driver Location: java -jar -Dwebdriver.chrome.driver=C:\Selenium\drivers\chrome\chromedriver.exe selenium-server-standalone-3.0.1.jar -role node -hub http://localhost:4444/grid/register 

I wrote a very simple test which clicks Facebook login button. Before run below test, start the hub and the node with above command then run the test.

Selenium Grid Nodes and Hub Configurations by JSON

It is also described at https://github.com/SeleniumHQ/selenium/wiki/Grid2 page as shown below.

Hub Config: https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultHub.json

Node Config: https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultNodeWebDriver.json

I will use the same config which are located at above link. By using this configurations I created “starthub.bat” and “startnode.bat” files.

starthub.bat

startnode.bat

Start the HUB

Start the Node

Run the test again! When you run the test you will see that it will pass.

Thanks for reading.

All the best!
-Onur