• Selenium Video Tutorials

Selenium Grid - Configuration



The latest version of Selenium Grid has a lot of differences from the older versions of Selenium Grid. The older version of Selenium Grid had only one mode - Hub and Node, while the latest version of Selenium Grid supports three modes - Standalone, Hub and Node, and Distributed.

The previous versions of Selenium Grid consisted of only two modes - Hub and Node, while the latest version of Selenium Grid has six components like the Router, Distributor, Nodes, Session Queue, Session Map, and Event Bus.

Prerequisites to Configure the Selenium Grid

Step 1 − Install Java(version above 8) in the system and check if it is present with the command: java -version. The java version installed will be visible if installation has been completed successfully.

Step 2 − Check Grid status by opening a browser and entering −

  • For the UI version, type http://localhost:4444.

  • For Non UI version, type http://localhost:4444/status.

For both the cases, we would get the error - This site can’t be reached. Since Selenium Grid has not been started yet.

Selenium Grid Configuration 1

Standalone Mode Configuration in Selenium Grid

In the latest version of Selenium Grid, Hub and Node need not be launched separately. Thus the Hub and Node can be triggered at the same time within the same machine. With a single Standalone command, both the Hub and the Node will be launched at one time. Thus the Standalone helps to save resources and time.

The steps to configure Standalone Mode are listed below −

Step 1 − Download Selenium Standalone Jar from the below link and save it in a folder −

https://github.com/SeleniumHQ/.

Step 2 − From the location of the folder where the Selenium Standalone Jar had been stored, run the below command from the terminal −

java -jar selenium-server-<version>.jar standalone.

Step 3 − Check Grid status again by opening a browser and entering http://localhost:4444.

The Error − This site can’t be reached would no longer be there, and we would get Grid status showing different browsers. This would prove Selenium Grid had been triggered in the Standalone mode.

Selenium Grid Configuration 2

Step 4

Code Implementation in Base.java

package BaseClass;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Base {
   public WebDriver setBrowser(String browserName) throws MalformedURLException {
      WebDriver driver = null;
      DesiredCapabilities dc = new DesiredCapabilities();
      if(browserName.equalsIgnoreCase("chrome")) {
         dc.setBrowserName("chrome");
      } else if(browserName.equalsIgnoreCase("edge")) {
         dc.setBrowserName("MicrosoftEdge");
      }

      // Initiate RemoteWebDriver
      driver = new RemoteWebDriver(new URL("http://localhost:4444"),dc);
      return driver;
   }
}

Code Implementation in TestOne.java

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestOne extends Base {
   public WebDriver driver = null;
   @Test
   public void testOne() {
   
      // launch application
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testOne");
   }
   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("chrome");
   }
   @AfterMethod
   public void tearDown() {
   
      // quitting browser
      driver.quit();
   }
}

Code Implementation in TestOne.java

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestTwo extends Base {
   public WebDriver driver = null;
   @Test
   public void testTwo() {
   
      // launch application
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testTwo");
   }
   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("edge");
   }

   @AfterMethod
   public void tearDown() {
   
      // quitting browser
      driver.quit();
   }
}

Configurations in testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Grid Test">
   <test thread-count = "5" name="Test">
      <classes>
         <class name="Grid.TestOne" />
         <class name="Grid.TestTwo"/>
      </classes>
   </test>
</suite>

Step 5 − Run the test from the testng.xml file.

It will show the following output

Page title is: Selenium Practice - Links obtained from testOne
Page title is: Selenium Practice - Links obtained from testTwo

===============================================
Grid Test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

In the above example, we had configured the Standalone mode of the Selenium Grid.

Hub and Node Mode Configuration in Selenium Grid

In the latest version of Selenium Grid, Hub and Node mode need Hub and Node to be triggered in separate machines. A Hub comprises the components like the Router, Distributor, Session Map, New Session Queue, and Event Bus.

The steps to configure Hub and Node Mode are listed below −

Step 1 − Download Selenium Standalone Jar from the below link and save it in a folder −

https://github.com/SeleniumHQ/selenium/releases.

Step 2 − From the location of the folder where the Selenium Standalone Jar had been stored, run the below command from the terminal −

java -jar selenium-server-<version>.jar hub.

Please Note − The hub IP Address in the command line logs received while registering the hub.

Step 3 − Check Grid status by opening a browser and entering http://localhost:4444.

The Error − This site can’t be reached would no longer be there, and we would get Grid status showing the message - The Grid has no registered Nodes yet. This was because Node had not been registered yet.

Selenium Grid Configuration 3

Run Node in Same Machine

After configuring Hub by following the above steps 1,2, and 3, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window −

java -jar selenium-server-<version>.jar node.

This would help to launch the Node in the same machine.

Check Grid again status by opening a browser and entering http://localhost:4444.

The Message Showing − The Grid has no registered Nodes yet would not be there, in place of that, the Node would reflect.

Selenium Grid Configuration 4

Run Node in Different Machine

Step 1 − In another machine download Selenium Standalone Jar from the below link and save it in a folder −

https://github.com/SeleniumHQ/selenium/releases.

Step 2 − Navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening the terminal window −

java -jar selenium-server-<version>.jar node 
   --detect-drivers true --publish-events tcp://{hub IP Address}:4442 
   --subscribe-events tcp://{hub IP Address}:4443.

Please Note − The hub IP Address would be available in the command line logs received while registering the hub obtained in Step 3.

This would help to launch the Node in a different machine.

Step 3 − Check Grid again status by opening a browser and entering http://localhost:4444.

The Message Showing − The Grid has no registered Nodes yet would not be there, in place of that, the Node would reflect.

Selenium Grid Configuration 5

Step 4 − Run the same test that was written for the Standalone mode from the testng.xml file.

It will show the following output

Page title is: Selenium Practice - Links obtained from testOne
Page title is: Selenium Practice - Links obtained from testTwo

===============================================
Grid Test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

In the above example, we had configured the Hub and Node mode of the Selenium Grid.

Distributed Configuration in Selenium Grid

The Distributed mode of Selenium Grid is used when there is a large number of Nodes to set up a big size Grid where there is only one Hub and so many Nodes across multiple machines. In such a situation, Hub and Node mode is not an ideal choice.

Also, in a Distributed mode, every component - Router, Distributor, Session Map, New Session Queue, and Event Bus can be started individually with the help of different commands.

The steps to configure Distributed Mode are listed below −

Step 1 − Download Selenium Standalone Jar from the below link and save it in a folder −

https://github.com/SeleniumHQ/selenium/releases

Step 2 − From the location of the folder where the Selenium Standalone Jar had been stored, run the below command from the terminal to kick off the Event Bus −

java -jar selenium-server-<version>.jar event-bus.

Please note the IP Address where the Event bus had been started in the command line logs in the terminal.

Step 3 − After configuring Event Bus by following the Step 2, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Session Map −

java -jar selenium-server-<version>.jar sessions.

Please note the IP Address where the Session Map had been started in the command line logs in the terminal.

Step 4 − After configuring Session Map by following the Step 3, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening new another terminal window to kick off the Session Queue −

java -jar selenium-server-<version>.jar sessionqueue.

Please note the IP Address where the Session Queue had been started in the command line logs in the terminal.

Step 5 − After configuring Session Queue by following the Step 4, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Distributor −

java -jar selenium-server-<version>.jar distributor 
   --sessions http://{IP Address of Session Map}:5556 
   --sessionqueue http://{IP Address of Session Queue}:5559 --bind-bus false.

Please note the IP Address where the Distributor had been started in the command line logs in the terminal.

Step 6 − After configuring the Distributor by following the Step 5, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Router −

java -jar selenium-server-<version>.jar router 
   --sessions http://{IP Address of Session Map}:5556 
   --distributor http://{IP Address of Distributor}:5553 
   --sessionqueue http://{IP Address of Session Queue}:5559.

Please note the IP Address where the Router had been started in the command line logs in the terminal.

After configuring the Router by following the Step 6, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening new another terminal window −

java -jar selenium-server-<version>.jar node.

This would help to launch the Node in the same machine.

Run Node in Different Machine

After configuring the Router by following the Step 6, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening a new terminal window −

java -jar selenium-server-<version>.jar node 
   --detect-drivers true --publish-events tcp://{hub IP Address}:4442 
   --subscribe-events tcp://{hub IP Address}:4443.

We had used the hub IP Address received while registering the hub during Hub and Node Configurations.

This would help to launch the Node in a different machine.

This concludes our comprehensive take on the tutorial on Selenium Grid - Configuration. We’ve started with describing prerequisites to configure the Selenium Grid, and walked through steps for Standalone, Hub and Node, and Distributed modes configuration in Selenium Grid.

This equips you with in-depth knowledge of the Selenium Grid Configuration. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements