Selenium Grid – Test execution in Cluster (Part-3)

Parallel execution in Selenium Grid with Hub and Node(s) configuration

Before starting the parallel execution part, you should know about Selenium Grid concepts (Part-1), Configuring Hub and Node(s) (Part-2).

Before start writing script for Parallel execution, you should know about RemoteWebDriver Class, which is available in org.openqa.selenium.remote package.

What is RemoteWebDriver?

RemoteWebDriver is a class, which is part of package org.openqa.selenium.remote.

All these WebDriver implementations extend the RemoteWebDriver wherein they specifically override only the part of RemoteWebDriver that deals with how to start and stop the client part that is responsible for interacting with the server component.

Difference between WebDriver and RemoteWebDriver

WebDriver is an interface and it includes the concrete implementation of

  • InternetExplorerDriver
  • FirefoxDriver
  • ChromeDriver
  • SafariDriver
  • OperaDriver
  • EdgeDriver
  • EventFiringWebDriver
  • RemoteWebDriver

We will use WebDriver interface to run the browser in local system. Where as, we will use RemoteWebDriver class to configure the server, where we should execute the browser and test cases.

The only requirement is that for a RemoteWebDriver to work, you would always have to have it pointing to the URL of a Grid.

If anyone needs to work with Grid, then they would have to stick with RemoteWebDriver.

Syntax for RemoteWebDriver:

WebDriver driver = new RemoteWebDriver(remoteAddress, capabilities);

Start writing code to automate the test case, which needs to launch NPN Training website in multiple browsers using Hub & Node(s) configuration.

Write a method to get the browser options, according to the browser

The objective of this method is to return the respective browser options, based up on the browser definition.

public static Capabilities getOptions(String browser) {
  switch(browser) {
  case ("firefox"):
    System.out.println("Opening Firefox Browser::");
    return new FirefoxOptions();
  case ("chrome"):
    System.out.println("Opening Chrome Browser::");
    return new ChromeOptions();
  case ("ie"):
    System.out.println("Opening Internet Explorer Browser::");
    return new InternetExplorerOptions();
  default:
    System.out.println("Defaut browser is firefox");
    return new FirefoxOptions();
  }
}

Write another method to launch the browser using RemoteWebDriver.

public static RemoteWebDriver LaunchBrowser(String browser) throws MalformedURLException {
    return new RemoteWebDriver(new URL("http://<IP of Hub>:<Port of Hub>/wd/hub"),getOptions(browser));
  }

We will use TestNG to define the testcase with @Test annotation, will use @Parameters for passing multiple browser values.

@Test
@Parameters({ "browser" })
public void TestParallelExecution(String browser) throws MalformedURLException {
  WebDriver driver;
  String appURL = "http://npntraining.com";
  driver = LaunchBrowser(browser);
  driver.get(appURL);
  driver.manage().window().maximize();
}

Let’s check the testng.xml file as mentioned below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test name="FireFoxTest">
  <parameter name="browser" value="firefox"></parameter>
  <classes>
  <class name="com.npntraining.ParallelExecution"></class>
   </classes>
  </test>
  <test name="ChromeTest">
   <parameter name="browser" value="chrome"></parameter>
  <classes>
  <class name="com.npntraining.ParallelExecution"></class>
   </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

In testng.xml file, you should define parameter name and respective value. Thus, we can achieve parallel execution.

Let’s check the complete class once

public class ParallelExecution {
  
  public static RemoteWebDriver LaunchBrowser(String browser) throws MalformedURLException {
    return new RemoteWebDriver(new URL("http://<IP of Hub>:<Port of Hub>/wd/hub"),getOptions(browser));
  }

  public static Capabilities getOptions(String browser) {
    switch(browser) {
    case ("firefox"):
      System.out.println("Opening Firefox Browser::");
      return new FirefoxOptions();
    case ("chrome"):
      System.out.println("Opening Chrome Browser::");
      return new ChromeOptions();
    case ("ie"):
      System.out.println("Opening Internet Explorer Browser::");
      return new InternetExplorerOptions();
    default:
      System.out.println("Defaut browser is firefox");
      return new FirefoxOptions();
    }
  }
  @Test
  @Parameters({ "browser" })
  public void TestParallelExecution(String browser) throws MalformedURLException {
    WebDriver driver;
    String appURL = "http://npntraining.com";
    driver = LaunchBrowser(browser);
    driver.get(appURL);
    driver.manage().window().maximize();
  }
}

By executing this particular testcase, First the RemoteWebDriver will establish connection to Hub and then while executing the test case, based up on the system browser capability, the browsers will be opened in the respective Node(s).

Thus, we can achieve Parallel Browser Execution in Selenium Grid by configuring Hub and Node(s).

 


About the course

Being one of the best Selenium training institute in Bangalore we are dedicatedly engaged in offering complete selenium course training based on real-time projects by industry experts. Our main aim is to improve and enhance your skills in software testing with our innovative advance level training. At NPN training institute we not only drive classroom training on selenium we also provide selenium certification after completion of a course and guide you throughout your learning.

Pradeep

M.r Pradeep is QA expert working as a Research Analyst at NPN Training. He is having 12 years of experience in Quality Assurance with extensive of 4 years in Automation experience (Selenium, UFT, Rest Assured, Jmeter & Soap UI). He has helped the peers/students to understand and learn Automation with Selenium and API automation with Rest Assured with Hands-On approach.