Bird
0
0

Identify the error in this page class constructor:

medium📝 Debug Q6 of 15
Selenium Java - Page Object Model
Identify the error in this page class constructor:
public class HomePage {
  private WebDriver driver;
  @FindBy(name = "search")
  private WebElement searchBox;

  public HomePage(WebDriver driver) {
    driver = driver;
    PageFactory.initElements(driver, this);
  }
}
A@FindBy annotation is missing a locator strategy
BPageFactory.initElements is called with wrong parameters
CThe constructor does not assign the driver parameter to the class field
DsearchBox should be public instead of private
Step-by-Step Solution
Solution:
  1. Step 1: Analyze driver assignment

    The constructor assigns driver = driver, which assigns the parameter to itself, not the class field.
  2. Step 2: Understand correct assignment

    It should be this.driver = driver; to assign the parameter to the class field.
  3. Final Answer:

    The constructor does not assign the driver parameter to the class field -> Option C
  4. Quick Check:

    Constructor driver assignment requires this. prefix [OK]
Quick Trick: Use this.driver = driver to assign constructor parameter [OK]
Common Mistakes:
  • Missing this. causes parameter shadowing
  • Incorrect PageFactory.initElements usage
  • Thinking @FindBy needs more locator info
  • Changing element visibility unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes