Assertions and Retry | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Interaction with Web Elements
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explored Cypress assertions , focusing on their syntax and usage in testing. Here are the key points discussed: Types of Assertions Should Assertions : Can be chained directly from Cypress commands. Expect Assertions : Must be used within a then block. Syntax Examples For example, to check if a label contains the text "email address", you can use: cy.get('label').should('contain', 'email address'); Or with expect : cy.get('label').then(label => { expect(label).to.contain('email address'); }); Exact Matches For exact text matches, use: cy.get('label').should('have.text', 'email address'); Using Invoke When using invoke to get text, validate with: expect(text).to.equal('email address'); Available Assertions Refer to the Cypress documentation for a comprehensive list of assertions, including: Chai assertions: to.not.equal , to.exist , etc. Chai jQuery assertions for DOM properties. Retry Mechanism Cypress automatically retries assertions until the default timeout (4 seconds) is reached, ensuring stability in tests. Best Practices Not every element needs an assertion. Methods like cy.get and cy.contains inherently validate element visibility and presence. For more insights, refer to the Introduction to Cypress Guide for best practices on assertions.