0
0
Cypresstesting~10 mins

Common assertions (exist, be.visible, have.text) in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a specific button exists on the page, is visible to the user, and has the correct text label. It verifies these common assertions using Cypress commands.

Test Code - Cypress
Cypress
describe('Common assertions test', () => {
  it('checks button existence, visibility, and text', () => {
    cy.visit('https://example.cypress.io')
    cy.get('#query-btn')
      .should('exist')
      .and('be.visible')
      .and('have.text', 'Button')
  })
})
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opensBrowser window opens ready for navigation-PASS
3Navigates to 'https://example.cypress.io'Page loads with example Cypress site content-PASS
4Finds element with selector '#query-btn' using cy.getButton element with id 'query-btn' is present in DOMChecks element exists in DOMPASS
5Checks element 'exist' assertionElement is confirmed presentElement exists in the DOMPASS
6Checks element 'be.visible' assertionElement is visible on the pageElement is visible to the userPASS
7Checks element 'have.text' assertion with text 'Button'Element text content is 'Button'Element text matches expected 'Button'PASS
8Test endsAll assertions passed, test completes successfully-PASS
Failure Scenario
Failing Condition: The element with selector '#query-btn' does not exist, is hidden, or has different text
Execution Trace Quiz - 3 Questions
Test your understanding
What does the 'exist' assertion check in this test?
AThe element has the correct text
BThe element is visible to the user
CThe element is present in the page's HTML DOM
DThe element is clickable
Key Result
Use multiple chained assertions in Cypress to verify different properties of the same element efficiently, improving test clarity and reliability.