0
0
Selenium Javatesting~10 mins

Test class consuming page objects in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, uses a page object to interact with the login form, submits credentials, and verifies successful login by checking the welcome message.

Test Code - JUnit + Selenium
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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;

// Page Object for Login Page
class LoginPage {
    private WebDriver driver;
    private WebDriverWait wait;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
    }

    public void enterUsername(String username) {
        WebElement usernameField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
        usernameField.clear();
        usernameField.sendKeys(username);
    }

    public void enterPassword(String password) {
        WebElement passwordField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password")));
        passwordField.clear();
        passwordField.sendKeys(password);
    }

    public void clickLogin() {
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn")));
        loginButton.click();
    }

    public String getWelcomeMessage() {
        WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMsg")));
        return welcomeMsg.getText();
    }
}

// Test class consuming the LoginPage object
public class LoginTest {
    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 testSuccessfulLogin() {
        loginPage.enterUsername("testuser");
        loginPage.enterPassword("password123");
        loginPage.clickLogin();
        String welcome = loginPage.getWelcomeMessage();
        assertEquals("Welcome, testuser!", welcome);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/loginLogin page is loaded with username, password fields and login button visiblePage URL is correct and login form elements are presentPASS
3Finds username input field and enters 'testuser'Username field contains 'testuser'Username field text equals 'testuser'PASS
4Finds password input field and enters 'password123'Password field contains 'password123'Password field text equals 'password123'PASS
5Finds and clicks the login buttonLogin form submitted, page starts loading next view-PASS
6Waits for welcome message element to appearWelcome message 'Welcome, testuser!' is visible on pageWelcome message text equals 'Welcome, testuser!'PASS
7Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Welcome message element does not appear or text is different
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the login button is disabled
BThat the welcome message 'Welcome, testuser!' appears
CThat the username field is cleared
DThat the password field is hidden
Key Result
Using page objects separates page details from test logic, making tests easier to read and maintain.