Bird
Raised Fist0
Selenium Javatesting~20 mins

PageFactory initialization in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PageFactory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PageFactory initialization code?
Consider the following Selenium Java code snippet initializing a Page Object. What will be the output when the test runs and tries to click the button?
Selenium Java
public class LoginPage {
    @FindBy(id = "submitBtn")
    private WebElement submitButton;

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

    public void clickSubmit() {
        submitButton.click();
        System.out.println("Clicked Submit");
    }
}

// In test method:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com/login");
LoginPage page = new LoginPage(driver);
page.clickSubmit();
AClicked Submit
BNullPointerException at submitButton.click()
CNoSuchElementException at PageFactory.initElements
DWebDriverException due to driver not initialized
Attempts:
2 left
💡 Hint
Think about what PageFactory.initElements does and when the element is located.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies PageFactory initialized element is displayed?
Given a Page Object with a WebElement 'loginButton' initialized by PageFactory, which assertion correctly checks if the button is visible on the page?
Selenium Java
public class HomePage {
    @FindBy(id = "loginBtn")
    private WebElement loginButton;

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

    public boolean isLoginButtonVisible() {
        return loginButton.isDisplayed();
    }
}

// Test method snippet:
HomePage home = new HomePage(driver);
boolean visible = home.isLoginButtonVisible();
AassertNull(visible);
BassertTrue(visible);
CassertEquals(visible, false);
DassertFalse(loginButton == null);
Attempts:
2 left
💡 Hint
Check which assertion confirms a boolean is true.
🔧 Debug
advanced
2:30remaining
Why does this PageFactory initialization cause NullPointerException?
Examine the code below. Why does calling click() on the 'searchButton' cause a NullPointerException?
Selenium Java
public class SearchPage {
    @FindBy(name = "search")
    private WebElement searchButton;

    public SearchPage() {
        PageFactory.initElements(this, this);
    }

    public void clickSearch() {
        searchButton.click();
    }
}

// Test snippet:
SearchPage page = new SearchPage();
page.clickSearch();
APageFactory requires @FindBy on public fields only
BsearchButton locator is invalid causing element not found
CsearchButton is private and cannot be accessed
DPageFactory.initElements called with wrong arguments; driver missing
Attempts:
2 left
💡 Hint
Check the parameters passed to PageFactory.initElements.
🧠 Conceptual
advanced
1:30remaining
What is the main advantage of using PageFactory.initElements in Selenium?
Why do testers prefer using PageFactory.initElements to initialize WebElements in Page Object classes?
AIt converts WebElements to strings for easier debugging
BIt automatically retries finding elements on failure without extra code
CIt creates lazy proxies for WebElements, locating them only when used
DIt immediately finds all elements on page load to speed up tests
Attempts:
2 left
💡 Hint
Think about when the element is actually located in the DOM.
❓ framework
expert
3:00remaining
Which PageFactory initialization pattern best supports parallel test execution?
In a Selenium test framework designed for parallel execution, which PageFactory initialization approach is best to avoid WebDriver conflicts?
AInitialize PageFactory inside each test method using the test's own WebDriver instance
BCreate PageFactory objects before WebDriver initialization to reuse them
CInitialize PageFactory with a null WebDriver and set elements later
DUse a static WebDriver shared across all tests and initialize PageFactory once globally
Attempts:
2 left
💡 Hint
Consider thread safety and WebDriver instance scope in parallel tests.

Practice

(1/5)
1. What is the main purpose of using PageFactory.initElements() in Selenium Java?
easy
A. To execute JavaScript on the page
B. To start the WebDriver session
C. To initialize all WebElements annotated with @FindBy in a Page Object
D. To close the browser after test execution

Solution

  1. Step 1: Understand the role of PageFactory

    PageFactory is used to initialize WebElements in Page Object Model classes.
  2. Step 2: Purpose of initElements method

    The initElements method scans the class for @FindBy annotations and initializes those elements.
  3. Final Answer:

    To initialize all WebElements annotated with @FindBy in a Page Object -> Option C
  4. Quick Check:

    PageFactory.initElements initializes elements [OK]
Hint: Remember: initElements sets up @FindBy elements [OK]
Common Mistakes:
  • Confusing initElements with WebDriver start or stop
  • Thinking initElements executes JavaScript
  • Assuming initElements closes the browser
2. Which of the following is the correct way to initialize PageFactory elements inside a constructor?
easy
A. PageFactory.initElements(driver, this);
B. PageFactory.initElements(this, driver);
C. initElements(driver, this);
D. PageFactory.startElements(driver, this);

Solution

  1. Step 1: Recall the method signature

    The correct method call is PageFactory.initElements(WebDriver driver, Object page).
  2. Step 2: Match parameters in constructor

    Inside the constructor, driver is passed first, then this refers to the current Page Object instance.
  3. Final Answer:

    PageFactory.initElements(driver, this); -> Option A
  4. Quick Check:

    Correct parameter order is driver, then this [OK]
Hint: Remember method signature: driver first, then page object [OK]
Common Mistakes:
  • Swapping driver and this parameters
  • Calling non-existent methods like startElements
  • Omitting PageFactory class name
3. Given the following code snippet, what will be the output when loginButton.isDisplayed() is called after PageFactory initialization?
public class LoginPage {
  @FindBy(id = "loginBtn")
  private WebElement loginButton;

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

// In test method:
LoginPage page = new LoginPage(driver);
boolean visible = page.loginButton.isDisplayed();
System.out.println(visible);
medium
A. true if the element with id 'loginBtn' is visible on the page
B. Compilation error due to missing getter for loginButton
C. NullPointerException because loginButton is not initialized
D. false always because loginButton is private

Solution

  1. Step 1: Check field visibility modifier

    The loginButton is declared private, so it cannot be accessed directly from outside the LoginPage class in the test method.
  2. Step 2: Identify the compilation issue

    page.loginButton.isDisplayed() causes a compilation error; private fields require public getter methods for external access. PageFactory initializes the field but does not change its visibility.
  3. Final Answer:

    Compilation error due to missing getter for loginButton -> Option B
  4. Quick Check:

    Private fields need public getters for access [OK]
Hint: PageFactory initializes private fields but Java visibility rules still apply [OK]
Common Mistakes:
  • Assuming direct access to private fields works after PageFactory init
  • Expecting runtime false or NullPointerException instead of compile error
  • Believing PageFactory overrides Java access modifiers
4. Identify the error in the following PageFactory initialization code:
public class HomePage {
  @FindBy(name = "searchBox")
  WebElement searchInput;

  public HomePage(WebDriver driver) {
    initElements(driver, this);
  }
}
medium
A. Incorrect locator strategy in @FindBy annotation
B. Constructor should not have parameters
C. WebElement should be private
D. Missing PageFactory class before initElements method call

Solution

  1. Step 1: Check method call syntax

    The method initElements must be called with the class name PageFactory as PageFactory.initElements(...).
  2. Step 2: Verify other code parts

    The locator strategy 'name' is valid, WebElement can be package-private, and constructor can have parameters.
  3. Final Answer:

    Missing PageFactory class before initElements method call -> Option D
  4. Quick Check:

    Always prefix initElements with PageFactory [OK]
Hint: Always call PageFactory.initElements, not just initElements [OK]
Common Mistakes:
  • Calling initElements without PageFactory prefix
  • Thinking locator strategy 'name' is invalid
  • Assuming WebElement must be private
  • Believing constructors cannot have parameters
5. 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
A. Declare WebElements with @FindBy, initialize them in constructor using PageFactory.initElements(driver, this), and keep elements private
B. Declare WebElements public without @FindBy, initialize manually in each test method
C. Use @FindBy annotations but do not call PageFactory.initElements anywhere
D. Initialize WebElements as static variables without PageFactory

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]
Hint: Use @FindBy + private + initElements in constructor [OK]
Common Mistakes:
  • Making WebElements public exposing internals
  • Skipping PageFactory.initElements call
  • Initializing elements manually in tests
  • Using static WebElements without context
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard