0
0
Selenium Javatesting~15 mins

Headless execution in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify Google Search page title using headless Chrome
Preconditions (3)
Step 1: Launch Chrome browser in headless mode
Step 2: Navigate to https://www.google.com
Step 3: Wait until the page title is 'Google'
Step 4: Verify that the page title is exactly 'Google'
Step 5: Close the browser
✅ Expected Result: The test should pass with the page title 'Google' verified successfully without opening a visible browser window.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the page title equals 'Google'
Best Practices:
Use ChromeOptions to enable headless mode
Use explicit waits to wait for page title
Use try-finally or try-with-resources to ensure browser closes
Use By locators and WebDriverWait for synchronization
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class HeadlessGoogleTitleTest {
    public static void main(String[] args) {
        // Set ChromeOptions to enable headless mode
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless=new"); // Use new headless mode for Chrome 109+

        WebDriver driver = new ChromeDriver(options);

        try {
            driver.get("https://www.google.com");

            // Wait up to 10 seconds for title to be 'Google'
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            wait.until(ExpectedConditions.titleIs("Google"));

            // Assert the title is exactly 'Google'
            String actualTitle = driver.getTitle();
            if (!"Google".equals(actualTitle)) {
                throw new AssertionError("Expected title 'Google' but found '" + actualTitle + "'");
            }

            System.out.println("Test passed: Page title is 'Google'");
        } finally {
            driver.quit(); // Ensure browser closes
        }
    }
}

This code uses ChromeOptions to enable headless mode, so the browser runs without opening a window. This is useful for running tests on servers or CI systems.

We create a WebDriver instance with these options, then navigate to Google.

We use WebDriverWait with ExpectedConditions.titleIs to wait until the page title is exactly 'Google'. This avoids timing issues.

Then we assert the title matches exactly. If not, we throw an error to fail the test.

Finally, we close the browser in a finally block to ensure cleanup even if the test fails.

This approach follows best practices: explicit waits, proper resource cleanup, and using headless mode correctly.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not using ChromeOptions to enable headless mode', 'why_bad': 'The browser will open a visible window, defeating the purpose of headless execution.', 'correct_approach': "Always create ChromeOptions and add the '--headless=new' argument before creating the ChromeDriver."}
Using Thread.sleep() instead of explicit waits
Not closing the browser after test
{'mistake': 'Hardcoding waits or using deprecated headless flags', 'why_bad': 'Older flags may not work with latest Chrome versions and cause test failures.', 'correct_approach': "Use the latest recommended headless argument '--headless=new' for Chrome 109+."}
Bonus Challenge

Now add data-driven testing to verify page titles for three different URLs: https://www.google.com, https://www.bing.com, and https://www.duckduckgo.com

Show Hint