Docker Selenium Grid in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.net.URL; import java.time.Duration; public class GoogleSearchGridTest { public static void main(String[] args) throws Exception { // URL of the Selenium Grid Hub running in Docker URL gridUrl = new URL("http://localhost:4444/wd/hub"); // Desired capabilities for Chrome browser DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setBrowserName("chrome"); // Create RemoteWebDriver instance to connect to Grid WebDriver driver = new RemoteWebDriver(gridUrl, capabilities); try { // Navigate to Google driver.get("https://www.google.com"); // Wait for the search box to be visible WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement searchBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q"))); // Enter search text String searchTerm = "Selenium Grid Docker"; searchBox.sendKeys(searchTerm); // Submit the search form searchBox.submit(); // Wait for the title to contain the search term wait.until(ExpectedConditions.titleContains(searchTerm)); // Assertion: verify title contains search term if (driver.getTitle().contains(searchTerm)) { System.out.println("Test Passed: Title contains search term."); } else { System.out.println("Test Failed: Title does not contain search term."); } } finally { // Close the browser session driver.quit(); } } }
This Java program uses Selenium WebDriver to connect to a Selenium Grid Hub running in Docker at http://localhost:4444/wd/hub. It sets the desired browser to Chrome.
The test navigates to Google, waits explicitly for the search input box to appear, enters the search term "Selenium Grid Docker", and submits the form.
It then waits until the page title contains the search term, which confirms the search results loaded correctly.
Finally, it asserts the page title contains the search term and prints the test result. The WebDriver session is closed in a finally block to ensure cleanup.
This approach uses explicit waits and proper locator strategies, following best practices for Selenium Grid tests.
Now add data-driven testing with 3 different search terms: 'Selenium Grid Docker', 'Docker containers', 'RemoteWebDriver Java'
Practice
Solution
Step 1: Understand Docker Selenium Grid's role
Docker Selenium Grid allows running tests on different browsers and machines at the same time, improving test coverage and speed.Step 2: Compare options with this purpose
Only To run tests on multiple browsers and machines simultaneously matches this main purpose. Options B, C, and D describe unrelated or incorrect uses.Final Answer:
To run tests on multiple browsers and machines simultaneously -> Option DQuick Check:
Purpose = run tests on many browsers [OK]
- Thinking it speeds up writing tests
- Confusing it with Selenium WebDriver itself
- Assuming it is for debugging code
Solution
Step 1: Identify the command to run a container
The command to start a container in detached mode and map port 4444 is 'docker run -d -p 4444:4444 selenium/hub'.Step 2: Check other options
docker start selenium/node-chrome starts an existing container, C builds an image, and D stops a container. Only B starts the hub container correctly.Final Answer:
docker run -d -p 4444:4444 selenium/hub -> Option CQuick Check:
Start hub = docker run -d -p 4444:4444 selenium/hub [OK]
- Using 'docker start' instead of 'docker run' to create container
- Confusing build and run commands
- Stopping instead of starting the container
RemoteWebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
new ChromeOptions()
);
driver.get("https://example.com");
System.out.println(driver.getTitle());What will be printed if the page title is "Example Domain"?
Solution
Step 1: Understand RemoteWebDriver usage
The code creates a RemoteWebDriver connected to the Selenium Grid hub URL and opens the page https://example.com.Step 2: Check what driver.getTitle() returns
driver.getTitle() returns the page title, which is "Example Domain" for that URL.Final Answer:
Example Domain -> Option AQuick Check:
driver.getTitle() = page title [OK]
- Confusing URL with page title
- Expecting null if no errors
- Assuming exceptions without errors
docker run -d --link selenium-hub:hub selenium/node-chrome
But your tests fail to connect to the node. What is the likely problem?
Solution
Step 1: Analyze the linking option
The '--link' option is deprecated and may not correctly link the node to the hub, causing connection failure.Step 2: Check other possible causes
Docker daemon (A) or hub not running (B) issues would cause broader failures. Chrome is preinstalled in official node images (C) so unlikely cause.Final Answer:
The node container is not linked properly to the hub -> Option BQuick Check:
Deprecated --link causes node-hub connection fail [OK]
- Assuming Chrome is missing inside node container
- Ignoring hub container status
- Not checking Docker daemon status
Solution
Step 1: Understand multi-browser setup
To run tests on multiple browsers, you must start the hub and connect nodes for each browser to the hub's network.Step 2: Evaluate options
Start hub container, then start both selenium/node-chrome and selenium/node-firefox containers connected to the hub network correctly starts hub and both browser nodes connected properly. Start only selenium/node-chrome container; Firefox tests will run on Chrome node misses Firefox node, A misses nodes, and D starts nodes before hub which is incorrect.Final Answer:
Start hub container, then start both selenium/node-chrome and selenium/node-firefox containers connected to the hub network -> Option AQuick Check:
Hub + all browser nodes connected = multi-browser tests [OK]
- Starting nodes before hub
- Running tests without nodes
- Assuming one node can run all browsers
Ready to prove it?
Pass the test and this pattern moves unknown
