Skip to content

Instantly share code, notes, and snippets.

@Phenomite
Created May 1, 2020 01:20
Show Gist options
  • Save Phenomite/2a4fcdb9e873f2bfa48af8d0f58576e4 to your computer and use it in GitHub Desktop.
Save Phenomite/2a4fcdb9e873f2bfa48af8d0f58576e4 to your computer and use it in GitHub Desktop.
cy.exist function to allow existence checking conditional testing
// 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