I am trying to create an if-else condition that includes cy.get()
if (!cy.get('.modal.modal--active')) {
cy.reload()
} else {
// some codes here
}
Upon testing the code the .modal.modal--active element is not present, instead of executing the cy.reload() line, it returns an error.
Expected to find element: .modal.modal--active, but never found it.
Is it possible to have the if block running and not return AssertionError?
Cypress commands should be chained, not used as expressions in
if()statements.cy.get(something)fails the test whensomethingisn't found.Adding a negative assertion like
cy.get(something).should('not.exist')does the opposite, it passes whensomethingisn't found.The third way (undocumented) is
.should(() => undefined), something like thisThe documented way is using jQuery
.find()as follows:Generally
if/elseis not supported in Cypress testing because best practice is to set the test page so thatif/elseconditions are not necessary.