0
0
Selenium Javatesting~10 mins

Headless execution in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a browser in headless mode, navigates to a webpage, finds a heading element, and verifies its text content.

Test Code - JUnit
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
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;

public class HeadlessTest {
    private WebDriver driver;

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

    @Test
    public void testHeadingText() {
        driver.get("https://example.com");
        WebElement heading = driver.findElement(By.tagName("h1"));
        assertEquals("Example Domain", heading.getText());
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Initialize ChromeDriver with headless option '--headless=new'Chrome browser is configured to run without UI (headless mode)-PASS
2Navigate to 'https://example.com'Browser loads the Example Domain webpage in headless mode-PASS
3Find the <h1> element on the pageThe <h1> element with text 'Example Domain' is located-PASS
4Assert that the heading text equals 'Example Domain'Heading text is retrieved from the elementVerify heading.getText() == 'Example Domain'PASS
5Close the browser and quit the driverBrowser session ends cleanly-PASS
Failure Scenario
Failing Condition: The <h1> element is not found or its text does not match 'Example Domain'
Execution Trace Quiz - 3 Questions
Test your understanding
What does the '--headless=new' option do in this test?
AEnables debugging mode in Chrome
BRuns Chrome browser without opening a visible window
CRuns Chrome browser with a new UI theme
DDisables JavaScript on the page
Key Result
Using headless mode allows tests to run faster and without opening a browser window, which is useful for automated test environments like CI pipelines.