Docker lets you run tests in a clean, controlled space. This helps avoid problems from different computers or setups.
0
0
Docker execution environment in Selenium Java
Introduction
You want to run Selenium tests on any computer without setup issues.
You need to test on different browsers or versions easily.
You want to share your test environment with teammates exactly as it is.
You want to run tests on a server or cloud without installing browsers manually.
You want to keep your computer clean without installing many tools.
Syntax
Selenium Java
docker run -d -p 4444:4444 selenium/standalone-chrome // In Java Selenium test: WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions());
The docker run command starts a browser server inside Docker.
Java connects to this server using RemoteWebDriver and browser options.
Examples
Starts a Firefox browser server inside Docker on port 4444.
Selenium Java
docker run -d -p 4444:4444 selenium/standalone-firefox
Connects Java Selenium test to the Firefox Docker browser server.
Selenium Java
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new FirefoxOptions());Starts the latest Chrome browser server in Docker.
Selenium Java
docker run -d -p 4444:4444 selenium/standalone-chrome:latest
Sample Program
This Java program connects to a Chrome browser running inside Docker. It opens example.com, checks the page title, prints the test result, and closes the browser.
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 DockerSeleniumTest { public static void main(String[] args) throws Exception { // Connect to Docker Selenium server WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions()); // Open a website driver.get("https://example.com"); // Check the page title String title = driver.getTitle(); if (title.equals("Example Domain")) { System.out.println("Test Passed: Title is correct."); } else { System.out.println("Test Failed: Title is incorrect."); } // Close the browser driver.quit(); } }
OutputSuccess
Important Notes
Make sure Docker is installed and running before starting the Selenium container.
Use docker ps to check if the Selenium container is running.
Always stop the Docker container after tests to free resources.
Summary
Docker creates a clean browser environment for Selenium tests.
Java connects to Docker browsers using RemoteWebDriver and browser options.
This helps run tests anywhere without setup problems.