0
0
JUnittesting~10 mins

Why patterns improve test quality in JUnit - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks that using a test pattern (Page Object Model) helps keep tests clear and reliable by separating page details from test logic.

Test Code - JUnit
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("loginBtn");

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

    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}

public class LoginTest {

    @Test
    void testValidLogin() {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass123");
        loginPage.clickLogin();

        WebElement welcomeMessage = driver.findElement(By.id("welcomeMsg"));
        assertTrue(welcomeMessage.isDisplayed(), "Welcome message should be visible after login");

        driver.quit();
    }
}
Execution Trace - 10 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initializes-PASS
2Browser opens ChromeDriverChrome browser window opens at blank page-PASS
3Navigates to https://example.com/loginLogin page loads with username, password fields and login button-PASS
4Creates LoginPage object with driverPage object ready to interact with login page elements-PASS
5Calls enterUsername("user1")Username field found and filled with 'user1'-PASS
6Calls enterPassword("pass123")Password field found and filled with 'pass123'-PASS
7Calls clickLogin()Login button found and clicked, page processes login-PASS
8Finds element with id 'welcomeMsg'Welcome message element is present on pageCheck if welcome message is displayedPASS
9Assert welcome message is visibleWelcome message visible to userassertTrue(welcomeMessage.isDisplayed())PASS
10Closes browser and ends testBrowser closed, test finished-PASS
Failure Scenario
Failing Condition: If the login button locator changes and is not updated in LoginPage class
Execution Trace Quiz - 3 Questions
Test your understanding
Why does using the LoginPage class improve test quality?
AIt automatically fixes broken locators
BIt makes the test run faster by skipping steps
CIt separates page details from test logic making tests easier to read and maintain
DIt removes the need for assertions
Key Result
Using test patterns like the Page Object Model keeps tests clean and easier to update when page details change, improving test quality and maintainability.