0
0
Selenium Javatesting~15 mins

Action methods per page in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Login functionality using action methods in Page Object
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Enter 'testuser@example.com' in the email input field
Step 3: Enter 'Test@1234' in the password input field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page with URL containing '/dashboard'
✅ Expected Result: User is successfully logged in and dashboard page is displayed
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Verify email input field accepts the correct text
Verify password input field accepts the correct text
Verify login button is clickable
Verify current URL contains '/dashboard' after login
Best Practices:
Use Page Object Model to separate page actions and locators
Create action methods for each user interaction in the page class
Use explicit waits to wait for elements to be interactable
Use TestNG assertions for validation
Keep test method clean by calling page action methods
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

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

    private By emailInput = By.id("email");
    private By passwordInput = By.id("password");
    private By loginButton = By.id("loginBtn");

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

    // Action method to enter email
    public void enterEmail(String email) {
        WebElement emailField = wait.until(ExpectedConditions.elementToBeClickable(emailInput));
        emailField.clear();
        emailField.sendKeys(email);
    }

    // Action method to enter password
    public void enterPassword(String password) {
        WebElement passwordField = wait.until(ExpectedConditions.elementToBeClickable(passwordInput));
        passwordField.clear();
        passwordField.sendKeys(password);
    }

    // Action method to click login button
    public void clickLogin() {
        WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(loginButton));
        loginBtn.click();
    }
}

// Test class
public class LoginTest {
    private WebDriver driver;
    private LoginPage loginPage;

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

    @Test
    public void testValidLogin() {
        loginPage.enterEmail("testuser@example.com");
        loginPage.enterPassword("Test@1234");
        loginPage.clickLogin();

        // Wait for URL to contain '/dashboard'
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        boolean urlContainsDashboard = wait.until(ExpectedConditions.urlContains("/dashboard"));

        Assert.assertTrue(urlContainsDashboard, "URL should contain '/dashboard' after login");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This code uses the Page Object Model to separate the login page actions from the test logic.

The LoginPage class has action methods: enterEmail, enterPassword, and clickLogin. Each method waits explicitly for the element to be clickable before interacting, improving reliability.

The test class LoginTest initializes the browser and page object in @BeforeClass. The test method calls the action methods to perform login steps clearly and asserts the URL contains '/dashboard' after login.

Finally, @AfterClass closes the browser to clean up.

This structure keeps the test clean and easy to maintain, following best practices.

Common Mistakes - 4 Pitfalls
Directly locating elements and interacting in the test method instead of using page action methods
Using Thread.sleep() instead of explicit waits
Not clearing input fields before sending keys
Using brittle locators like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.

Show Hint