Forked from Phenomite/cy.exist conditional testing.js
Created
March 24, 2024 22:34
-
-
Save katagaki/08c56b8bea6f83dff099a4c9ba268a3e to your computer and use it in GitHub Desktop.
cy.exist function to allow existence checking conditional testing
This file contains hidden or 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