Bird
0
0

You want to create a Page Object for a login page with username and password fields and a login button. Which of the following is the best practice for initializing elements using PageFactory?

hard📝 Application Q15 of 15
Selenium Java - Page Object Model
You want to create a Page Object for a login page with username and password fields and a login button. Which of the following is the best practice for initializing elements using PageFactory?
ADeclare WebElements with @FindBy, initialize them in constructor using PageFactory.initElements(driver, this), and keep elements private
BDeclare WebElements public without @FindBy, initialize manually in each test method
CUse @FindBy annotations but do not call PageFactory.initElements anywhere
DInitialize WebElements as static variables without PageFactory
Step-by-Step Solution
Solution:
  1. Step 1: Understand Page Object best practices

    WebElements should be private and annotated with @FindBy for clear locators and encapsulation.
  2. Step 2: Proper initialization

    PageFactory.initElements(driver, this) should be called in the constructor to initialize elements automatically.
  3. Final Answer:

    Declare WebElements with @FindBy, initialize them in constructor using PageFactory.initElements(driver, this), and keep elements private -> Option A
  4. Quick Check:

    Private elements + @FindBy + initElements in constructor = best practice [OK]
Quick Trick: Use @FindBy + private + initElements in constructor [OK]
Common Mistakes:
MISTAKES
  • Making WebElements public exposing internals
  • Skipping PageFactory.initElements call
  • Initializing elements manually in tests
  • Using static WebElements without context

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes