Bird
0
0

Given the following code snippet, what will be the output when loginButton.isDisplayed() is called after PageFactory initialization?

medium📝 Predict Output Q13 of 15
Selenium Java - Page Object Model
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);
Atrue if the element with id 'loginBtn' is visible on the page
BCompilation error due to missing getter for loginButton
CNullPointerException because loginButton is not initialized
Dfalse always because loginButton is private
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes