0
0
Selenium Javatesting~10 mins

Page class design in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that the Page class correctly locates and interacts with the login button on a web page. It checks that clicking the button triggers the expected behavior.

Test Code - JUnit 5 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.*;

class LoginPage {
    private WebDriver driver;
    private By loginButtonLocator = By.id("login-btn");

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

    public WebElement getLoginButton() {
        return driver.findElement(loginButtonLocator);
    }

    public void clickLogin() {
        getLoginButton().click();
    }
}

public class LoginPageTest {
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testClickLoginButton() {
        loginPage.clickLogin();
        String currentUrl = driver.getCurrentUrl();
        assertTrue(currentUrl.contains("dashboard"), "URL should contain 'dashboard' after login click");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Browser opens ChromeDriverChrome browser window opens-PASS
3Navigates to https://example.com/loginLogin page is loaded in browserPage URL is https://example.com/loginPASS
4LoginPage object is created with driverPage class ready to interact with elements-PASS
5Finds login button by id 'login-btn'Login button element is located on pageElement is present and visiblePASS
6Clicks login buttonBrowser processes click event-PASS
7Checks current URL contains 'dashboard'Browser URL updated after clickassertTrue(currentUrl.contains("dashboard"))PASS
8Test ends and browser closesBrowser window closed, resources freed-PASS
Failure Scenario
Failing Condition: Login button with id 'login-btn' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThe URL contains 'dashboard'
BThe login button disappears
CThe page title changes to 'Login'
DAn alert popup appears
Key Result
Designing a Page class with clear element locators and interaction methods helps keep tests clean and easy to maintain.