0
0
Selenium Javatesting~15 mins

Docker execution environment in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate login test using Selenium in Docker environment
Preconditions (3)
Step 1: Start Selenium Docker container with Chrome browser
Step 2: Write Selenium Java test to open the login page URL
Step 3: Enter 'admin@test.com' in the email input field with id 'email'
Step 4: Enter 'Pass123!' in the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the URL changes to dashboard page URL 'https://example.com/dashboard'
Step 7: Verify that the current URL is 'https://example.com/dashboard'
✅ Expected Result: The test passes confirming successful login and navigation to dashboard page inside Docker environment
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify email and password fields accept input
Verify login button is clickable
Verify URL after login is the dashboard URL
Best Practices:
Use explicit waits to wait for page load and element visibility
Use By.id locator strategy for stable element selection
Use Page Object Model to separate page actions and test logic
Handle Docker container startup and shutdown outside test code
Automated Solution
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.net.URL;
import java.time.Duration;

public class DockerLoginTest {
    static WebDriver driver;
    static WebDriverWait wait;

    @BeforeAll
    public static void setup() throws Exception {
        // Connect to Selenium Docker container running at localhost:4444
        ChromeOptions options = new ChromeOptions();
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testLogin() {
        driver.get("https://example.com/login");

        WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        emailInput.sendKeys("admin@test.com");

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.sendKeys("Pass123!");

        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        loginButton.click();

        // Wait until URL changes to dashboard
        wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard"));

        String currentUrl = driver.getCurrentUrl();
        assertEquals("https://example.com/dashboard", currentUrl, "User should be on dashboard page after login");
    }

    @AfterAll
    public static void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test connects to a Selenium Docker container running Chrome browser at http://localhost:4444/wd/hub. The @BeforeAll method sets up the remote WebDriver and explicit wait.

The test navigates to the login page URL, waits for the email input to be visible, then enters the email and password. It clicks the login button and waits explicitly until the URL changes to the dashboard URL.

Finally, it asserts that the current URL is exactly the dashboard URL, confirming successful login.

The @AfterAll method quits the driver to close the browser session.

Using explicit waits ensures the test does not fail due to timing issues. Using By.id locators makes element selection stable and fast. The test assumes the Docker container is started and stopped outside this code.

Common Mistakes - 3 Pitfalls
Hardcoding Thread.sleep instead of using explicit waits
Using brittle XPath locators instead of stable IDs
Starting and stopping Docker container inside test code
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid).

Show Hint