0
0
Selenium Javatesting~7 mins

Docker Selenium Grid in Selenium Java

Choose your learning style9 modes available
Introduction

Docker Selenium Grid helps run automated browser tests on many browsers and machines easily. It saves time by running tests in parallel without setting up each browser manually.

You want to test your website on different browsers like Chrome and Firefox at the same time.
You need to run many tests quickly without waiting for one test to finish before starting another.
You want to avoid installing browsers and drivers on your local machine.
You want to share the same test environment with your team easily.
You want to run tests on different operating systems or browser versions without extra setup.
Syntax
Selenium Java
docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-chrome

// Java code to connect to Selenium Grid
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions());

Use docker run to start Selenium Grid containers.

In Java, use RemoteWebDriver with the Grid URL to run tests remotely.

Examples
Start a Selenium Grid container with Firefox browser.
Selenium Java
docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-firefox
Connect Java test to Selenium Grid using Firefox browser.
Selenium Java
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new FirefoxOptions());
Set up Selenium Grid hub and Chrome node on a Docker network for parallel testing.
Selenium Java
docker network create selenium-grid-network

docker run -d --net selenium-grid-network --name selenium-hub -p 4444:4444 selenium/hub

docker run -d --net selenium-grid-network --name chrome-node -e HUB_HOST=selenium-hub selenium/node-chrome
Sample Program

This Java program connects to a Selenium Grid running on localhost. It opens the example.com website, checks the page title, and prints it. If the title is wrong, it throws an error.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.URL;

public class SeleniumGridTest {
    public static void main(String[] args) throws Exception {
        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
        driver.get("https://example.com");
        String title = driver.getTitle();
        System.out.println("Page title is: " + title);
        if (!title.equals("Example Domain")) {
            throw new AssertionError("Title does not match expected value.");
        }
        driver.quit();
    }
}
OutputSuccess
Important Notes

Make sure Docker is installed and running before starting Selenium Grid containers.

Use the correct Grid URL and port in your test code (default is http://localhost:4444/wd/hub).

Always quit the WebDriver to free resources after the test finishes.

Summary

Docker Selenium Grid lets you run browser tests on many browsers and machines easily.

Use Docker commands to start Grid hub and nodes, then connect with RemoteWebDriver in Java.

This setup helps run tests faster and share environments with your team.