Bird
0
0

Examine the following Page Object class:

medium📝 Debug Q6 of 15
Selenium Java - Page Object Model
Examine the following Page Object class:
public class HomePage {
  @FindBy(xpath = "//button[@id='logout']")
  WebElement logoutButton;

  public HomePage(WebDriver driver) {
    PageFactory.initElements(this, driver);
  }
}

What is the issue with this code?
AThe <code>logoutButton</code> should be declared as <code>private</code>.
BThe parameters in <code>initElements</code> are in the wrong order.
CThe <code>logoutButton</code> annotation is missing a locator strategy.
DThe constructor should not call <code>PageFactory.initElements()</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Check initElements method signature

    The correct signature is PageFactory.initElements(WebDriver driver, Object page).
  2. Step 2: Analyze parameter order

    The code uses PageFactory.initElements(this, driver); which reverses the parameters.
  3. Step 3: Other options

    Declaring WebElement as private is good practice but not an error.
    The locator is correctly specified.
    Calling initElements in constructor is standard practice.
  4. Final Answer:

    The parameters in initElements are in the wrong order. -> Option B
  5. Quick Check:

    initElements(driver, this) is correct order [OK]
Quick Trick: initElements(driver, this) correct order [OK]
Common Mistakes:
  • Swapping parameters in initElements
  • Ignoring access modifiers
  • Misunderstanding annotation usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes