0
0
Selenium Javatesting~15 mins

ChromeOptions configuration in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Configure Chrome browser with ChromeOptions and verify browser launch
Preconditions (3)
Step 1: Create a ChromeOptions object
Step 2: Add the argument '--headless' to run Chrome in headless mode
Step 3: Add the argument '--disable-gpu' to disable GPU usage
Step 4: Initialize ChromeDriver with the ChromeOptions
Step 5: Navigate to 'https://www.example.com'
Step 6: Verify the page title is 'Example Domain'
Step 7: Close the browser
✅ Expected Result: Chrome browser launches in headless mode with specified options, navigates to example.com, and page title matches 'Example Domain'. Browser closes successfully.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify page title equals 'Example Domain'
Verify ChromeDriver is initialized with ChromeOptions including '--headless' and '--disable-gpu'
Best Practices:
Use explicit waits if needed
Use try-finally or @After method to close browser
Use descriptive variable names
Avoid hardcoded sleeps
Use ChromeOptions to configure browser capabilities
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ChromeOptionsTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");
        driver = new ChromeDriver(options);
    }

    @Test
    public void testExampleDotComTitle() {
        driver.get("https://www.example.com");
        String title = driver.getTitle();
        Assertions.assertEquals("Example Domain", title, "Page title should be 'Example Domain'");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses JUnit 5 for structure.

In setUp(), we create a ChromeOptions object and add the arguments --headless and --disable-gpu to run Chrome without UI and disable GPU acceleration.

We then initialize ChromeDriver with these options.

The test method navigates to https://www.example.com and asserts the page title is exactly Example Domain.

Finally, tearDown() closes the browser to clean up resources.

This setup ensures the browser runs headless and the test verifies the configuration by checking the page title.

Common Mistakes - 4 Pitfalls
Not adding ChromeOptions to ChromeDriver constructor
Using Thread.sleep() instead of explicit waits
Not closing the browser after test
Adding incorrect or unsupported ChromeOptions arguments
Bonus Challenge

Now add data-driven testing to run the test with three different ChromeOptions configurations: headless, incognito, and maximized window.

Show Hint