cypress-page-object-pattern icon indicating copy to clipboard operation
cypress-page-object-pattern copied to clipboard

Question: anti-pattern A

Open th3hunt opened this issue 7 years ago • 1 comments

Hello and thank you for this nice write-up.

Could you please elaborate on

pattern A breaks Cypress.io internal async queuing and necessitates excessive use of .thens and/or callbacks.

Why is that?

Thanks a lot!

th3hunt avatar Jun 12 '18 07:06 th3hunt

Sure, it very much depends on how you use them but consider the following:

cy.visit('https://example.cypress.io/');
const mainPage = new MainPage();
mainPage.getButton().click();
cy.get('#first').should('have.attr', 'data-validation', 'required');

This says:

  1. Queue up visiting a page, but don't do it yet
  2. Construct the page object
  3. Get the button then click on it, but you may not have even visited teh page yet
  4. Queue up an assertion.

The key is the cy commands happen later in async. See here: https://docs.cypress.io/guides/references/best-practices.html#Assigning-Return-Values

Hypercubed avatar Jun 12 '18 22:06 Hypercubed