0
0
Selenium Javatesting~20 mins

PageFactory initialization in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 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.