0
0
Selenium Javatesting~10 mins

Utility classes in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a utility class to open a browser, navigate to a page, and verify the page title. It checks that the utility class methods work correctly to simplify test steps.

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

// Utility class for browser actions
class BrowserUtils {
    private WebDriver driver;

    public BrowserUtils() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        this.driver = new ChromeDriver();
    }

    public void openUrl(String url) {
        driver.get(url);
    }

    public String getTitle() {
        return driver.getTitle();
    }

    public void closeBrowser() {
        if (driver != null) {
            driver.quit();
        }
    }

    public WebDriver getDriver() {
        return driver;
    }
}

public class UtilityClassTest {
    BrowserUtils browserUtils;

    @BeforeEach
    public void setUp() {
        browserUtils = new BrowserUtils();
    }

    @Test
    public void testPageTitle() {
        browserUtils.openUrl("https://example.com");
        String title = browserUtils.getTitle();
        assertEquals("Example Domain", title);
    }

    @AfterEach
    public void tearDown() {
        browserUtils.closeBrowser();
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and BrowserUtils instance is created, launching Chrome browserChrome browser window opens, ready for commands-PASS
2BrowserUtils opens URL 'https://example.com'Browser navigates to https://example.com, page loads-PASS
3BrowserUtils retrieves the page titlePage title is 'Example Domain'Check if page title equals 'Example Domain'PASS
4JUnit assertion compares expected and actual titleAssertion verifies titles matchassertEquals('Example Domain', actualTitle)PASS
5BrowserUtils closes the browserChrome browser window closes-PASS
Failure Scenario
Failing Condition: Page title does not match expected 'Example Domain'
Execution Trace Quiz - 3 Questions
Test your understanding
What does the utility class BrowserUtils do in this test?
AIt opens the browser, navigates to a URL, gets the page title, and closes the browser
BIt only verifies the page title without opening a browser
CIt runs the test assertions directly
DIt manages test data for the test
Key Result
Using utility classes helps keep test code clean and reusable by grouping common browser actions in one place.