Action methods per page in Selenium Java - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.
Practice
Solution
Step 1: Understand the role of action methods
Action methods are designed to perform user interactions such as clicking buttons or entering text.Step 2: Differentiate from other page object responsibilities
Storing data, configuring browsers, or generating reports are not the role of action methods.Final Answer:
To represent user actions like clicking or typing on the page -> Option AQuick Check:
Action methods = user actions [OK]
- Confusing action methods with data storage
- Thinking action methods configure the browser
- Assuming action methods generate reports
Solution
Step 1: Identify the correct Selenium method for clicking
The methodclick()performs a click action on the element.Step 2: Check the other methods
sendKeys()types text,getText()reads text, andclear()clears input fields.Final Answer:
public void clickButton() { driver.findElement(By.id("btn")).click(); } -> Option BQuick Check:
Click action uses click() method [OK]
- Using sendKeys() instead of click() to click
- Calling getText() which only reads text
- Using clear() which clears input fields
public void enterUsername(String name) {
driver.findElement(By.name("username")).sendKeys(name);
}What will happen when
enterUsername("testUser") is called?Solution
Step 1: Understand sendKeys() usage
ThesendKeys()method types the given string into the located input field.Step 2: Analyze the method call
CallingenterUsername("testUser")passes "testUser" tosendKeys(), so it types this text.Final Answer:
The text "testUser" will be typed into the username input field -> Option AQuick Check:
sendKeys("text") types text [OK]
- Thinking sendKeys() clears the field
- Believing sendKeys() takes no arguments
- Confusing click() with sendKeys()
public void submitForm() {
driver.findElement(By.id("submitBtn")).click();
driver.findElement(By.id("submitBtn")).click();
}What is the issue with this method?
Solution
Step 1: Review the method actions
The method callsclick()on the same button twice in a row.Step 2: Understand the impact
Clicking twice may cause unexpected behavior or duplicate submissions.Final Answer:
It clicks the submit button twice unnecessarily -> Option DQuick Check:
Duplicate clicks cause redundant actions [OK]
- Thinking duplicate lines cause compile errors
- Assuming locator syntax is wrong
- Believing the button is not found
Solution
Step 1: Check if checkbox is already selected
UseisSelected()to verify checkbox state before clicking.Step 2: Click only if not selected
Clicking only when unchecked prevents unchecking it accidentally.Final Answer:
public void selectCheckbox() { WebElement checkbox = driver.findElement(By.id("agree")); if (!checkbox.isSelected()) { checkbox.click(); } } -> Option CQuick Check:
Check isSelected() before click to avoid toggling [OK]
- Clicking checkbox without checking state
- Calling isSelected() without action
- Using clear() on checkbox which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
