Selenium Grid on Windows

In this post, I will describe how to use Selenium Grid. You will learn how to install a selenium grid and how to configure it.

For Mac users, you can check here: https://www.swtestacademy.com/selenium-grid-on-mac/

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

https://github.com/SeleniumHQ/selenium/wiki/Grid2[1]

Selenium 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. Selenium team constantly updating the versions. You may see a different version, pls do not panic, just download it. ;)

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

What are 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 in our hub and the hub knows which browsers are available. Hub sends requests to the nodes based on desired capabilities and executes 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, let’s 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 the node

We can start the node with the 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 a selenium grid in our tests we need to tell our grid where the required browser executable is located. Thus, we should start to the 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 that clicks the Facebook login button. Before running the below test, start the hub and the node with the above command then run the test.

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by ONUR on 20.11.2016.
 */
public class GridExampleTest {

    static WebDriver driver;

    //Setup Driver
    @BeforeClass
    public static void setupTest() throws MalformedURLException {
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        //For Firefox use below capabilities
        //DesiredCapabilities caps = DesiredCapabilities.firefox();
        caps.setPlatform(Platform.WINDOWS);
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
    }

    @Test
    public void T01_FacebookLogin() {
        //Navigate to facebook.com
        driver.navigate().to("https://www.facebook.com/");
        driver.manage().window().maximize();

        //Login Button
        WebElement loginButton = driver.findElement(By.id("loginbutton"));

        //Actions example
        loginButton.click();
    }

    //Close Driver
    @AfterClass
    public static void quitDriver() {
        driver.quit();
    }
}

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/SeleniumGridRefactor/grid/blob/master/src/main/resources/org/openqa/grid/common/defaults/DefaultHub.json

Node Config: https://github.com/SeleniumGridRefactor/grid/blob/master/src/main/resources/org/openqa/grid/common/defaults/DefaultNodeWebDriver.json

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

starthub.bat

REM start a hub with the defaults on port 4444
REM visit http://localhost:4444/grid/console to see status
REM java -jar selenium-server-standalone-3.0.1.jar -role hub

REM Start a hub with params configured in JSON file
REM https://github.com/SeleniumGridRefactor/grid/blob/master/src/main/resources/org/openqa/grid/common/defaults/DefaultHub.json
java -jar selenium-server-standalone-3.0.1.jar -role hub -hubConfig hub.json

startnode.bat

REM start a node configured by the node.json
REM 

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

java -jar -Dwebdriver.gecko.driver=C:\Selenium\drivers\firefox\geckodriver.exe -Dwebdriver.chrome.driver=C:\Selenium\drivers\chrome\chromedriver.exe selenium-server-standalone-3.0.1.jar -role node -nodeConfig node.json 

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 Baskirt

4 thoughts on “Selenium Grid on Windows”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.