0
0
Selenium Javatesting~10 mins

Action methods per page in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that the login page's action methods work correctly by entering valid credentials and checking successful login.

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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

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 {
    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 testValidLogin() {
        loginPage.enterUsername("testuser");
        loginPage.enterPassword("password123");
        loginPage.clickLogin();
        boolean loggedIn = driver.findElement(By.id("welcomeMessage")).isDisplayed();
        assertTrue(loggedIn, "User should be logged in and welcome message displayed");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Browser opens ChromeDriverChrome browser window opened-PASS
3Navigates to https://example.com/loginLogin page loaded with username, password fields and login buttonPage URL is correctPASS
4Finds username field and enters 'testuser'Username input filled with 'testuser'Username field contains 'testuser'PASS
5Finds password field and enters 'password123'Password input filled with 'password123'Password field contains 'password123'PASS
6Finds and clicks login buttonLogin form submitted, page navigates-PASS
7Finds welcome message element by id 'welcomeMessage'Welcome message displayed on pageWelcome message is displayedPASS
8Assertion checks user is logged in by verifying welcome message visibilityTest verifies login successassertTrue(loggedIn) passesPASS
9Browser closes and test endsChrome browser closed-PASS
Failure Scenario
Failing Condition: Welcome message element not found after login attempt
Execution Trace Quiz - 3 Questions
Test your understanding
What action does the test perform after opening the login page?
AEnter username and password using page action methods
BDirectly click the login button without entering credentials
CClose the browser immediately
DNavigate to a different URL
Key Result
Using action methods in page classes helps keep test code clean and reusable by separating page interactions from test logic.