Challenge - 5 Problems
PageFactory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
Think about what PageFactory.initElements does and when the element is located.
✗ Incorrect
PageFactory.initElements initializes the WebElement fields so that when clickSubmit() is called, submitButton is a proxy that locates the element at click time. Since the driver is valid and the element exists, the click succeeds and prints 'Clicked Submit'.
❓ assertion
intermediate1: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();
Attempts:
2 left
💡 Hint
Check which assertion confirms a boolean is true.
✗ Incorrect
Since isLoginButtonVisible() returns a boolean indicating visibility, assertTrue(visible) correctly asserts the button is visible. Other options either check wrong conditions or wrong types.
🔧 Debug
advanced2: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();
Attempts:
2 left
💡 Hint
Check the parameters passed to PageFactory.initElements.
✗ Incorrect
PageFactory.initElements requires a WebDriver instance as the first argument to locate elements. Passing 'this' instead of a driver causes the WebElement fields to remain null, leading to NullPointerException on click.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about when the element is actually located in the DOM.
✗ Incorrect
PageFactory.initElements creates proxy objects for WebElements that locate the elements only when a method like click() or getText() is called. This lazy loading improves test performance and stability.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Consider thread safety and WebDriver instance scope in parallel tests.
✗ Incorrect
For parallel tests, each test must have its own WebDriver instance. Initializing PageFactory inside each test method with that test's driver ensures no conflicts or shared state, supporting thread safety.