DEV Community

Discussion on: Using Page Objects in Cypress

Collapse
 
liunate profile image
Nate Liu

I am not sure if we should wrap every single expectation into the page object function call.

For example in the todo list count example you mentioned, the check can be broken into two parts Locator and Assertion

cy
  // Locator
  .get('.todo-list li')
  // Assertion
  .should('have.length', 2)
Enter fullscreen mode Exit fullscreen mode

To me the Locator is the one exposing technical implementation(the CSS selector .todo-list li) and it's nice to have a page object like findTodoItems() to return that, disregarding the implementation detail and its fragile nature.

The second part Assertion is, by looking at its raw cypress commands should('have.length', 2), pretty straightforward. Adding another page object layer like validateTodoCount(expectedLength) seems to be an overkill.

As such, I feel we don't always need to include straightforward assertions into page object. Instead, in some assertion that is comprised of couple checks/steps/hacky things, page object assertion-function calls may be easier to understand and let us focus on the higher level business outcome.