0
0
Selenium Javatesting~10 mins

Docker execution environment in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a Selenium WebDriver test inside a Docker container. It opens a browser, navigates to a website, clicks a button, and verifies the page title to ensure the test environment works correctly inside Docker.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.net.URL;

public class DockerSeleniumTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() throws Exception {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
    }

    @Test
    public void testButtonClickChangesTitle() {
        driver.get("https://example.com");
        WebElement moreInfoLink = driver.findElement(By.cssSelector("a[href='https://www.iana.org/domains/example']"));
        moreInfoLink.click();
        String expectedTitle = "IANA — IANA-managed Reserved Domains";
        assertEquals(expectedTitle, driver.getTitle());
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and sets up RemoteWebDriver with ChromeOptions in headless mode connecting to Selenium Grid in Docker at http://localhost:4444/wd/hubDocker container running Selenium Grid with Chrome browser ready-PASS
2Driver navigates to https://example.comBrowser inside Docker container loads example.com homepage-PASS
3Finds the link with href 'https://www.iana.org/domains/example' and clicks itBrowser navigates to IANA reserved domains page-PASS
4Checks the page title equals 'IANA — IANA-managed Reserved Domains'Browser page title is 'IANA — IANA-managed Reserved Domains'assertEquals(expectedTitle, driver.getTitle())PASS
5Test finishes and quits the WebDriver sessionBrowser session closed, Docker container remains running-PASS
Failure Scenario
Failing Condition: Selenium Grid Docker container is not running or not reachable at http://localhost:4444/wd/hub
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the link on example.com?
AThe page title changes to 'IANA — IANA-managed Reserved Domains'
BThe URL remains https://example.com
CThe button becomes disabled
DAn alert popup appears
Key Result
Always verify that your Docker Selenium Grid container is running and accessible before running tests. Use headless browser mode to run tests smoothly inside Docker without GUI.