One the many joys of working with Cypress is the variety of support for various assertion methodologies.
What is even more powerful is that they can be chained at the end of core Cypress API commands like cy.get
Here are coupe of examples that I put into practice recently.
- JQuery based
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
cy.get("#header > div > div > div:nth-child(2) > div > div.headButtons > div.header_button_float.logMenu > div > a.underLine") | |
// assert that the element's text matches a reg ex | |
.should(($txt) => { | |
const text = $txt.text() | |
expect(text).to.match(/Login/) | |
}) |
2. BDD type assertions
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
//assert that the element contains a particular text | |
cy.get('.whtLink').should('contain','nirvana') |
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
//find and confirm that an element is visible | |
cy.get("#header > div > div > div:nth-child(2) > div > div.headButtons > div.header_button_float.logMenu > div > a.underLine") | |
.should('be.visible') |
Simple , elegant and flexible 🙂
I will continue to practice further ways to assert using Cypress
Which assertion methodology do you particularly prefer?
Leave a Reply