0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Use Selenium Grid for Parallel Test Execution

To use Selenium Grid, set up a Hub that controls test distribution and connect multiple Nodes that run tests on different browsers or machines. Configure your test scripts to use RemoteWebDriver pointing to the Hub URL to execute tests in parallel across these nodes.
📐

Syntax

Selenium Grid involves three main parts:

  • Hub: Central server that receives test requests and distributes them.
  • Node: Machines registered to the Hub that run tests on specific browsers.
  • RemoteWebDriver: Used in test scripts to connect to the Hub and run tests remotely.

Basic syntax to create a RemoteWebDriver instance:

RemoteWebDriver driver = new RemoteWebDriver(new URL("http://hub-ip:4444/wd/hub"), desiredCapabilities);

Here, desiredCapabilities specify browser type and version.

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

public class GridSyntax {
    public static void main(String[] args) throws Exception {
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
        driver.get("https://example.com");
        System.out.println(driver.getTitle());
        driver.quit();
    }
}
Output
Example Domain
💻

Example

This example shows how to run a simple test on Selenium Grid using Chrome browser. It assumes you have started a Hub and registered a Node with Chrome browser.

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

public class SeleniumGridExample {
    public static void main(String[] args) throws Exception {
        // Set browser capabilities
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        // Connect to Selenium Grid Hub
        RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

        // Open a website
        driver.get("https://www.google.com");

        // Print page title
        System.out.println("Page title is: " + driver.getTitle());

        // Close browser
        driver.quit();
    }
}
Output
Page title is: Google
⚠️

Common Pitfalls

  • Hub or Node not started: Make sure the Hub and Nodes are running before executing tests.
  • Incorrect Hub URL: Use the correct Hub IP and port (default 4444) in your test scripts.
  • Browser capabilities mismatch: Ensure the Node supports the browser and version specified in DesiredCapabilities.
  • Firewall or network issues: Nodes must be able to communicate with the Hub over the network.

Example of wrong and right Hub URL usage:

java
// Wrong way: Using localhost when Hub is on another machine
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

// Right way: Use actual Hub IP address
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"), capabilities);
📊

Quick Reference

Selenium Grid Quick Tips:

  • Start Hub: java -jar selenium-server.jar hub
  • Start Node: java -jar selenium-server.jar node --hub http://hub-ip:4444/grid/register
  • Use RemoteWebDriver with Hub URL in tests.
  • Set DesiredCapabilities to specify browser and platform.
  • Check Hub console at http://hub-ip:4444/grid/console to see registered nodes.

Key Takeaways

Set up a Selenium Grid Hub and register Nodes before running tests.
Use RemoteWebDriver with the Hub URL and desired browser capabilities in your test scripts.
Ensure network connectivity and correct Hub URL to avoid connection errors.
Check the Hub console to verify Nodes are registered and ready.
Selenium Grid enables parallel test execution across multiple browsers and machines.