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 Byfields. Do not keepWebElementfields. - The constructor takes the
WebDriverand finds nothing. loginAs(email, password)returns aDashboardPage. Add a way for tests to read the error message after a failed login (for exampleString errorMessage()).- Use
WebDriverWaitinstead ofThread.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.