Created
May 1, 2020 01:20
-
-
Save Phenomite/2a4fcdb9e873f2bfa48af8d0f58576e4 to your computer and use it in GitHub Desktop.
cy.exist function to allow existence checking conditional testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Anti-pattern conditional testing with Cypress | |
// Uses jQuery functionality to determine existence | |
// Add to your support folder | |
Cypress.Commands.add("exist", (selector) => { | |
cy.get('body').should('exist').then(($body) => { | |
return new Cypress.Promise((resolve, reject) => { | |
if ($body.find(selector).length > 0) { | |
console.log("cy.exist() - Matching element found in DOM!"); | |
resolve(true); | |
} else { | |
console.log("cy.exist() - Element did not exist!"); | |
resolve(false); | |
} | |
}) | |
}) | |
}) | |
// Add to your step definitions | |
cy.exist("#button1").then((exists) => { | |
if (exists) { | |
//element does exist | |
} else { | |
//element does not exist | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment