Skip to content
SDETRole
SDETRole

Refactor into a Page Object

advanced Page Object Model Challenge 10 of 13

The LoginPage below finds its elements in the constructor, uses an absolute XPath, sleeps, and asserts on the URL inside the page object. Refactor it into a clean page object.

Assume a DashboardPage class with a DashboardPage(WebDriver driver) constructor already exists. Write only the LoginPage class; tests will do the asserting.

Requirements

  • Store locators as private final By fields. Do not keep WebElement fields.
  • The constructor takes the WebDriver and finds nothing.
  • loginAs(email, password) returns a DashboardPage. Add a way for tests to read the error message after a failed login (for example String errorMessage()).
  • Use WebDriverWait instead of Thread.sleep, and replace the absolute XPath.
  • No assertions inside the page object.

Expected output

A `LoginPage` with private `By` locator fields, a constructor that only takes the driver, methods that return the next page object (`DashboardPage loginAs(...)`) or data for the test to assert (`String errorMessage()`), explicit waits, and no assertions or sleeps.

Checks (6)

  • ○Stores locators as By fields
  • ○No assertions inside the page object
  • ○Navigation methods return a page object
  • ○No Thread.sleep
  • ○No absolute XPath
  • ○Does not cache WebElements in fields
Solution.javaValidated · not executed

Ctrl/⌘ + Enter to validate · your code is saved in this browser

Results

Validate your code against the checks.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →