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.
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.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sets up RemoteWebDriver with ChromeOptions in headless mode connecting to Selenium Grid in Docker at http://localhost:4444/wd/hub | Docker container running Selenium Grid with Chrome browser ready | - | PASS |
| 2 | Driver navigates to https://example.com | Browser inside Docker container loads example.com homepage | - | PASS |
| 3 | Finds the link with href 'https://www.iana.org/domains/example' and clicks it | Browser navigates to IANA reserved domains page | - | PASS |
| 4 | Checks the page title equals 'IANA — IANA-managed Reserved Domains' | Browser page title is 'IANA — IANA-managed Reserved Domains' | assertEquals(expectedTitle, driver.getTitle()) | PASS |
| 5 | Test finishes and quits the WebDriver session | Browser session closed, Docker container remains running | - | PASS |