0
0
Selenium Javatesting~10 mins

Base page class pattern in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses the base page class pattern to open a web page, find a button, click it, and verify the resulting message is displayed correctly.

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 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;

// Base page class with common methods
class BasePage {
    protected WebDriver driver;

    public BasePage(WebDriver driver) {
        this.driver = driver;
    }

    public void click(By locator) {
        driver.findElement(locator).click();
    }

    public String getText(By locator) {
        return driver.findElement(locator).getText();
    }
}

// Home page class extending base page
class HomePage extends BasePage {
    private By buttonLocator = By.id("show-message-btn");
    private By messageLocator = By.id("message");

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public void clickShowMessage() {
        click(buttonLocator);
    }

    public String getMessageText() {
        return getText(messageLocator);
    }
}

public class BasePagePatternTest {
    private WebDriver driver;
    private HomePage homePage;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/testpage");
        homePage = new HomePage(driver);
    }

    @Test
    public void testShowMessageButton() {
        homePage.clickShowMessage();
        String message = homePage.getMessageText();
        assertEquals("Hello, World!", message);
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Browser navigates to https://example.com/testpagePage loads with a button having id 'show-message-btn' and hidden message with id 'message'-PASS
3HomePage object created with driverPage elements accessible via HomePage methods-PASS
4Clicks the button with id 'show-message-btn' using BasePage click methodButton is clicked, message becomes visible-PASS
5Gets text from element with id 'message' using BasePage getText methodMessage text is 'Hello, World!'Verify message text equals 'Hello, World!'PASS
6Assertion checks if actual message text matches expectedTest verifies message correctnessassertEquals("Hello, World!", message) passesPASS
7Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Button with id 'show-message-btn' is missing or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the BasePage class provide in this test?
ACommon methods to interact with web elements like click and getText
BThe test assertions to verify results
CThe browser driver setup and teardown
DThe URL navigation logic
Key Result
Using a base page class helps reuse common web element actions, making tests cleaner and easier to maintain.