Bird
0
0

Given the following code snippet, what will be the output when button.isDisplayed() is called?

medium📝 Predict Output Q13 of 15
Selenium Java - Page Object Model
Given the following code snippet, what will be the output when button.isDisplayed() is called?
public class LoginPage {
  @FindBy(name = "loginBtn")
  WebElement button;

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

// In test method:
LoginPage page = new LoginPage(driver);
boolean visible = page.button.isDisplayed();
System.out.println(visible);
AThrows NullPointerException because button is not initialized
Bfalse always because @FindBy does not locate elements
Ctrue if the element with name 'loginBtn' is visible on the page
DCompilation error due to missing @FindBy import
Step-by-Step Solution
Solution:
  1. Step 1: Understand PageFactory initialization

    The constructor calls PageFactory.initElements(driver, this), which initializes all @FindBy annotated elements, including button.
  2. Step 2: Check behavior of isDisplayed()

    If the element with name 'loginBtn' exists and is visible, isDisplayed() returns true; otherwise false. No NullPointerException occurs because initialization is done.
  3. Final Answer:

    true if the element with name 'loginBtn' is visible on the page -> Option C
  4. Quick Check:

    PageFactory initializes elements = true if visible [OK]
Quick Trick: PageFactory.initElements must be called to avoid null elements [OK]
Common Mistakes:
MISTAKES
  • Assuming elements are null without initialization
  • Confusing isDisplayed() with element presence
  • Ignoring the need to call PageFactory.initElements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes