0
0
Selenium Javatesting~15 mins

Docker Selenium Grid in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify Google Search using Selenium Grid on Docker
Preconditions (3)
Step 1: Start Docker Selenium Grid Hub container
Step 2: Start Docker Selenium Grid Node Chrome container connected to the Hub
Step 3: Write a Selenium test that connects to the Selenium Grid Hub URL
Step 4: Navigate to https://www.google.com
Step 5: Enter 'Selenium Grid Docker' in the search input field
Step 6: Submit the search form
Step 7: Wait for the results page to load
Step 8: Verify that the page title contains 'Selenium Grid Docker'
✅ Expected Result: The test connects to the Selenium Grid running in Docker, performs a Google search, and verifies the page title contains the search term, indicating the test ran successfully on the remote browser.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify page title contains the search term 'Selenium Grid Docker'
Best Practices:
Use RemoteWebDriver to connect to Selenium Grid Hub URL
Use explicit waits to wait for page elements or title
Use proper locator strategies (By.name for search input)
Close the WebDriver session after test completion
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Using local WebDriver instead of RemoteWebDriver to connect to Selenium Grid
Hardcoding Thread.sleep() instead of using explicit waits
{'mistake': 'Using brittle locators like absolute XPath for the Google search box', 'why_bad': 'Absolute XPath can break easily if page structure changes, making tests fragile.', 'correct_approach': 'Use stable locators like By.name("q") for the Google search input.'}
Not closing the WebDriver session after test completion
Bonus Challenge

Now add data-driven testing with 3 different search terms: 'Selenium Grid Docker', 'Docker containers', 'RemoteWebDriver Java'

Show Hint