Bird
Raised Fist0
Selenium Javatesting~10 mins

Logging test steps in Selenium Java - Test Execution Trace

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of adding logging statements in Selenium Java tests?
easy
A. To make the test run faster
B. To show what the test is doing step-by-step
C. To change the test results
D. To hide errors from the user

Solution

  1. Step 1: Understand logging purpose

    Logging records actions during test execution to track progress.
  2. Step 2: Identify correct purpose

    Logging helps see what the test does, not speed or hide errors.
  3. Final Answer:

    To show what the test is doing step-by-step -> Option B
  4. Quick Check:

    Logging = Show test steps [OK]
Hint: Logging tells what happens during tests [OK]
Common Mistakes:
  • Thinking logging speeds up tests
  • Believing logging changes test results
  • Assuming logging hides errors
2. Which of the following is the correct way to create a Logger instance in Selenium Java?
easy
A. Logger logger = Logger.createLogger();
B. Logger logger = new Logger();
C. Logger logger = Logger.getLogger("TestLogger");
D. Logger logger = Logger();

Solution

  1. Step 1: Recall Logger creation syntax

    In Java, Logger is created using getLogger with a name string.
  2. Step 2: Check options for correct syntax

    Only Logger logger = Logger.getLogger("TestLogger"); uses Logger.getLogger with a string, which is correct.
  3. Final Answer:

    Logger logger = Logger.getLogger("TestLogger"); -> Option C
  4. Quick Check:

    Logger creation = getLogger("name") [OK]
Hint: Use Logger.getLogger("name") to create logger [OK]
Common Mistakes:
  • Trying to instantiate Logger with new
  • Using wrong method names like createLogger
  • Calling Logger as a function
3. What will be the output in the console after running this code snippet?
Logger logger = Logger.getLogger("TestLogger");
logger.info("Starting test");
logger.warning("Element not found");
medium
A. Error: Logger not initialized
B. Starting test\nElement not found
C. No output
D. INFO: Starting test\nWARNING: Element not found

Solution

  1. Step 1: Understand logger methods

    logger.info prints info level messages; logger.warning prints warning level messages.
  2. Step 2: Predict console output format

    Output includes level labels INFO and WARNING before messages.
  3. Final Answer:

    INFO: Starting test\nWARNING: Element not found -> Option D
  4. Quick Check:

    Logger output = Level + message [OK]
Hint: Logger prints level name before message [OK]
Common Mistakes:
  • Expecting messages without level labels
  • Thinking logger prints nothing by default
  • Confusing info and warning outputs
4. Identify the error in this Selenium Java logging code:
Logger logger = Logger.getLogger("TestLogger")
logger.info("Test started");
medium
A. Missing semicolon after getLogger statement
B. Logger cannot be used without import
C. logger.info should be logger.log
D. Logger name must be a class, not a string

Solution

  1. Step 1: Check Java syntax

    Java statements must end with a semicolon (;).
  2. Step 2: Find missing semicolon

    The first line lacks a semicolon after getLogger call.
  3. Final Answer:

    Missing semicolon after getLogger statement -> Option A
  4. Quick Check:

    Java lines end with ; [OK]
Hint: Check for missing semicolons in Java code [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Confusing logger.info with logger.log
  • Thinking Logger name must be a class
5. You want to log each step of a Selenium test to help debug failures. Which approach is best to add logging for multiple steps?
hard
A. Create one Logger instance and call logger.info() before each test action
B. Create a new Logger instance before every test step
C. Use System.out.println() instead of Logger for all steps
D. Add logging only after test finishes

Solution

  1. Step 1: Understand efficient logging practice

    One Logger instance reused for all steps is efficient and clear.
  2. Step 2: Evaluate other options

    Creating many Logger instances wastes resources; System.out.println lacks levels; logging only after test misses step details.
  3. Final Answer:

    Create one Logger instance and call logger.info() before each test action -> Option A
  4. Quick Check:

    Reuse one Logger, log each step [OK]
Hint: Use one Logger, log before each action [OK]
Common Mistakes:
  • Creating multiple Logger instances unnecessarily
  • Using print statements instead of Logger
  • Logging only at test end
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard