Bird
Raised Fist0
Selenium Javatesting~20 mins

Page class design in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Page Object 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 Selenium Page Object method?
Consider this Java Selenium Page Object method that returns the page title. What will be the output if the page title is "Welcome Page"?
Selenium Java
public class HomePage {
    private WebDriver driver;

    public HomePage(WebDriver driver) {
        this.driver = driver;
    }

    public String getPageTitle() {
        return driver.getTitle();
    }
}

// Assume driver.getTitle() returns "Welcome Page"
A"Welcome Page"
B"homepage"
Cnull
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Think about what driver.getTitle() returns in Selenium.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the presence of a button in a Page Object?
You have a Page Object with a method isSubmitButtonDisplayed() that returns a boolean. Which assertion correctly checks that the submit button is visible?
Selenium Java
public class LoginPage {
    private WebDriver driver;
    private By submitButton = By.id("submit");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public boolean isSubmitButtonDisplayed() {
        return driver.findElement(submitButton).isDisplayed();
    }
}
AassertTrue(loginPage.isSubmitButtonDisplayed());
BassertFalse(loginPage.isSubmitButtonDisplayed());
CassertNull(loginPage.isSubmitButtonDisplayed());
DassertEquals(loginPage.isSubmitButtonDisplayed(), false);
Attempts:
2 left
💡 Hint
The method returns true if the button is visible.
❓ locator
advanced
2:00remaining
Which locator follows best practices for a Page Object element?
You want to locate a login input field in your Page Object. Which locator is best practice?
ABy.cssSelector("input")
BBy.xpath("//input[@type='text']")
CBy.id("loginInput")
DBy.className("input-field")
Attempts:
2 left
💡 Hint
Use the most specific and stable locator.
🔧 Debug
advanced
2:00remaining
Why does this Page Object method throw NoSuchElementException?
This method tries to click a button but throws NoSuchElementException. What is the likely cause?
Selenium Java
public void clickLogin() {
    driver.findElement(By.id("loginBtn")).click();
}
AThe driver is not initialized properly.
BThe method should use findElements instead of findElement.
CThe click() method is deprecated and causes the exception.
DThe element with id "loginBtn" is not present or not yet loaded on the page.
Attempts:
2 left
💡 Hint
NoSuchElementException means Selenium cannot find the element.
❓ framework
expert
2:00remaining
What is the main benefit of using the Page Object Model in Selenium tests?
Choose the best explanation for why the Page Object Model (POM) is used in Selenium test automation.
AIt automatically generates test data for form inputs.
BIt separates test code from page-specific code, improving maintainability and readability.
CIt speeds up test execution by running tests in parallel.
DIt replaces the need for assertions in test scripts.
Attempts:
2 left
💡 Hint
Think about how POM organizes code.

Practice

(1/5)
1. What is the main purpose of a page class in Selenium Java?
easy
A. To handle API requests
B. To write test assertions directly
C. To group web page elements and actions in one place
D. To manage database connections

Solution

  1. Step 1: Understand the role of page classes

    Page classes organize locators and actions related to a specific web page.
  2. Step 2: Identify the main purpose

    This organization improves code readability and reuse by keeping page details in one place.
  3. Final Answer:

    To group web page elements and actions in one place -> Option C
  4. Quick Check:

    Page class purpose = Group elements and actions [OK]
Hint: Page classes group elements and actions per page [OK]
Common Mistakes:
  • Confusing page classes with test classes
  • Using page classes for database or API logic
  • Writing assertions inside page classes
2. Which of the following is the correct way to declare a locator in a Selenium page class using Java?
easy
A. public void submitButton = driver.findElement(By.id("submit"));
B. public String submitButton = "submit";
C. private WebElement submitButton = driver.findElement(By.id("submit"));
D. private By submitButton = By.id("submit");

Solution

  1. Step 1: Recall best practice for locators

    Locators should be stored as By objects, not WebElement, to delay element lookup.
  2. Step 2: Check each option

    private By submitButton = By.id("submit"); correctly declares a private By locator with By.id("submit").
  3. Final Answer:

    private By submitButton = By.id("submit"); -> Option D
  4. Quick Check:

    Locators = By objects [OK]
Hint: Use By for locators, not WebElement directly [OK]
Common Mistakes:
  • Declaring locators as WebElement fields
  • Using public visibility for locators
  • Assigning void type to variables
3. Given the following page class snippet, what will loginPage.getTitle() return if the page title is "Welcome Page"?
public class LoginPage {
  private WebDriver driver;
  public LoginPage(WebDriver driver) {
    this.driver = driver;
  }
  public String getTitle() {
    return driver.getTitle();
  }
}
medium
A. "Welcome Page"
B. "Login Page"
C. null
D. Throws NullPointerException

Solution

  1. Step 1: Understand getTitle() method

    The method calls driver.getTitle(), which returns the current page title.
  2. Step 2: Match with given page title

    The page title is "Welcome Page", so getTitle() returns this string.
  3. Final Answer:

    "Welcome Page" -> Option A
  4. Quick Check:

    driver.getTitle() = "Welcome Page" [OK]
Hint: driver.getTitle() returns current page title string [OK]
Common Mistakes:
  • Assuming getTitle() returns page class name
  • Expecting null if not set explicitly
  • Confusing with element text retrieval
4. Identify the error in this page class method:
public void clickLogin() {
  WebElement loginBtn = driver.findElement(By.id("login"));
  loginBtn.click();
}
medium
A. No error, method is correct
B. Should store locator as a By field, not find element inside method
C. Missing exception handling for element not found
D. Method should return WebElement instead of void

Solution

  1. Step 1: Review locator usage best practice

    Locators should be stored as By fields in the page class, not found inside methods.
  2. Step 2: Identify the problem

    Finding element inside method reduces reuse and violates page class design principles.
  3. Final Answer:

    Should store locator as a By field, not find element inside method -> Option B
  4. Quick Check:

    Locators as By fields = best practice [OK]
Hint: Keep locators as fields, find elements in methods [OK]
Common Mistakes:
  • Finding elements repeatedly inside methods
  • Ignoring locator reuse
  • Assuming exception handling is mandatory here
5. You want to design a page class for a login page with username, password fields, and a login button. Which design approach is best to follow?
hard
A. Store locators as private By fields; create public methods to enter username, enter password, and click login
B. Store WebElement fields publicly and access them directly in tests
C. Write all test steps inside the page class constructor
D. Use static methods only without storing any locators

Solution

  1. Step 1: Recall page class design principles

    Locators should be private By fields; actions should be public methods for reuse and encapsulation.
  2. Step 2: Evaluate options

    Store locators as private By fields; create public methods to enter username, enter password, and click login follows best practice by separating locators and actions clearly.
  3. Final Answer:

    Store locators as private By fields; create public methods to enter username, enter password, and click login -> Option A
  4. Quick Check:

    Private locators + public methods = best design [OK]
Hint: Private locators + public action methods = good page class [OK]
Common Mistakes:
  • Making locators public and exposing WebElements
  • Putting test logic in constructors
  • Using static methods without instance 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