What if you could fix all your test problems by changing code in just one place?
Why Action methods per page in Selenium Java? - Purpose & Use Cases
Imagine testing a website manually by clicking buttons, filling forms, and checking results on every page one by one.
You have to remember each step and repeat it exactly the same way every time.
Manual testing is slow and tiring.
It's easy to forget steps or make mistakes.
When the website changes, you must redo everything from scratch.
Action methods per page organize all actions for a page into one place.
This makes tests easier to write, read, and update.
When the page changes, you only update the methods for that page.
driver.findElement(By.id("login")).click(); driver.findElement(By.id("username")).sendKeys("user"); driver.findElement(By.id("password")).sendKeys("pass"); driver.findElement(By.id("submit")).click();
loginPage.clickLogin(); loginPage.enterUsername("user"); loginPage.enterPassword("pass"); loginPage.submit();
It enables fast, clear, and maintainable automated tests that adapt easily to page changes.
When a website redesign changes button IDs, you only update the action methods in one place instead of all tests.
Manual testing is slow and error-prone.
Action methods group page actions for easy reuse.
Tests become simpler to write and maintain.
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
