cypress-page-object-pattern
cypress-page-object-pattern copied to clipboard
Question: anti-pattern A
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!
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:
- Queue up visiting a page, but don't do it yet
- Construct the page object
- Get the button then click on it, but you may not have even visited teh page yet
- 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