Bird
0
0

What is wrong with this page class method?

medium📝 Debug Q7 of 15
Selenium Java - Page Object Model
What is wrong with this page class method?
public void enterUsername(String username) {
  driver.findElement(By.id("username")).sendKeys(username);
}
AsendKeys() cannot be used with By locators
BThe method should return a boolean instead of void
CIt violates the Page Object Model by locating elements inside methods
DUsing driver.findElement is required in page classes
Step-by-Step Solution
Solution:
  1. Step 1: Recall POM best practice

    Locators should be defined as class fields, not inside methods, to improve maintainability.
  2. Step 2: Evaluate method code

    This method locates element inside method, violating POM principles.
  3. Final Answer:

    It violates the Page Object Model by locating elements inside methods -> Option C
  4. Quick Check:

    Define locators as fields, not inside methods [OK]
Quick Trick: Define locators as fields, not inside methods [OK]
Common Mistakes:
  • Thinking sendKeys() can't use By locators
  • Believing method must return boolean
  • Assuming driver.findElement is always required inside methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes