0
0
Selenium Javatesting~5 mins

Action methods per page in Selenium Java

Choose your learning style9 modes available
Introduction

Action methods help you organize how you interact with a web page in tests. They make tests easier to read and maintain.

When you want to click buttons or links on a page.
When you need to fill out forms or enter text.
When you want to check if certain elements are visible or enabled.
When you want to reuse common page actions in many tests.
When you want to keep your test code clean and simple.
Syntax
Selenium Java
public class PageName {
    private WebDriver driver;

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

    public void actionMethod() {
        // code to perform action on the page
    }
}

Action methods are inside page classes representing web pages.

They use WebDriver to interact with page elements.

Examples
This method clicks the login button on the page.
Selenium Java
public void clickLoginButton() {
    driver.findElement(By.id("loginBtn")).click();
}
This method types the username into the username field.
Selenium Java
public void enterUsername(String username) {
    driver.findElement(By.name("username")).sendKeys(username);
}
This method checks if an error message is visible on the page.
Selenium Java
public boolean isErrorMessageDisplayed() {
    return driver.findElement(By.className("error-msg")).isDisplayed();
}
Sample Program

This test opens a login page, enters wrong credentials, clicks login, and checks if an error message appears.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginPage {
    private WebDriver driver;

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

    public void enterUsername(String username) {
        driver.findElement(By.id("username")).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(By.id("password")).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(By.id("loginBtn")).click();
    }

    public boolean isLoginErrorVisible() {
        return driver.findElement(By.id("errorMsg")).isDisplayed();
    }
}

class TestLogin {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("wrongUser");
        loginPage.enterPassword("wrongPass");
        loginPage.clickLogin();

        boolean errorVisible = loginPage.isLoginErrorVisible();
        System.out.println("Login error visible: " + errorVisible);

        driver.quit();
    }
}
OutputSuccess
Important Notes

Use clear and simple names for action methods to describe what they do.

Keep action methods focused on one task for easy reuse.

Always use reliable locators like id or name for elements.

Summary

Action methods represent user actions on a page.

They help keep test code organized and readable.

Use them to click, type, or check elements on the page.