• Selenium Video Tutorials

Selenium Grid - Test Execution



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.

Selenium Grid is a tool that distributes the tests across multiple physical or virtual machines so that we can execute scripts in parallel (simultaneously). It dramatically accelerates the testing process, and runs the tests across multiple browsers and platforms and gives us quick and accurate feedback on the product.

Selenium Grid allows us to execute multiple instances of WebDriver or Selenium Remote Control allows execution of tests in parallel which use the same code base, hence the code need not be present on the system they execute. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Prerequisites For Test Execution in 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 Test Execution 1

Test Execution in Selenium Grid

The steps to set up test execution in Selenium Grid in a 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.

In the below image, it shows the successful triggering of the Hub.

Selenium Grid Test Execution 2

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 Test Execution 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.

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.

In the below image, it shows the successful registration of a Node from the same machine.

Selenium Grid Test Execution 4

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 Test Execution 5

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 TestTwo.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 performed the test execution using the Hub and Node mode of the Selenium Grid.

This concludes our comprehensive take on the tutorial on Selenium Grid - Test Execution. We’ve started with describing prerequisites to perform test execution in Selenium Grid, and how to actually perform a test execution in Selenium Grid.

This equips you with in-depth knowledge of the Selenium Grid - Test Execution. 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