0
0
Selenium Javatesting~10 mins

Logging test steps in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, logs each step for clarity, clicks a button, and verifies the result. It shows how logging helps track test progress.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.logging.Logger;
import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class LoggingTestSteps {
    private WebDriver driver;
    private Logger logger = Logger.getLogger(LoggingTestSteps.class.getName());

    @Before
    public void setUp() {
        logger.info("Starting browser setup");
        driver = new ChromeDriver();
        logger.info("Browser started");
    }

    @Test
    public void testButtonClick() {
        logger.info("Navigating to example page");
        driver.get("https://example.com/buttonpage");

        logger.info("Locating the button by id 'click-me'");
        WebElement button = driver.findElement(By.id("click-me"));

        logger.info("Clicking the button");
        button.click();

        logger.info("Checking if the result text is displayed");
        WebElement resultText = driver.findElement(By.id("result"));
        Assert.assertEquals("Button clicked!", resultText.getText());
        logger.info("Assertion passed: Button click result is correct");
    }

    @After
    public void tearDown() {
        logger.info("Closing browser");
        if (driver != null) {
            driver.quit();
        }
        logger.info("Browser closed");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Logger records 'Starting browser setup'; ChromeDriver instance createdBrowser window opens, blank page-PASS
2Logger records 'Navigating to example page'; driver.get loads https://example.com/buttonpagePage with a button having id 'click-me' is displayed-PASS
3Logger records 'Locating the button by id 'click-me''; driver finds the button elementButton element is found and ready to interact-PASS
4Logger records 'Clicking the button'; button.click() is executedPage updates to show result text with id 'result'-PASS
5Logger records 'Checking if the result text is displayed'; driver finds result element; Assert.assertEquals verifies textResult text reads 'Button clicked!'Assert.assertEquals confirms text matches expectedPASS
6Logger records 'Assertion passed: Button click result is correct'Test passed, browser ready to close-PASS
7Logger records 'Closing browser'; driver.quit() closes browserBrowser window closed-PASS
8Logger records 'Browser closed'; test endsNo browser open, test complete-PASS
Failure Scenario
Failing Condition: Button with id 'click-me' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the logger record before clicking the button?
ALocating the button by id 'click-me'
BStarting browser setup
CClosing browser
DAssertion passed
Key Result
Logging each test step clearly helps track progress and quickly find where a test fails, making debugging easier.