0
0
Cypresstesting~10 mins

cy.contains() for text matching in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a button with specific text is present on the page and clicks it. It verifies that the button click triggers the expected change in the page content.

Test Code - Cypress
Cypress
describe('Test cy.contains() for text matching', () => {
  it('Finds button by text and clicks it', () => {
    cy.visit('https://example.cypress.io')
    cy.contains('type').click()
    cy.url().should('include', '/commands/actions')
    cy.get('.action-email').should('be.visible')
  })
})
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opens and navigates to 'https://example.cypress.io'Browser shows the Cypress example homepage-PASS
3Finds element containing text 'type' using cy.contains('type')Button or link with text 'type' is visible on the pageElement with text 'type' is foundPASS
4Clicks the element found by cy.contains('type')Page navigates to '/commands/actions' sectionURL includes '/commands/actions'PASS
5Finds input element with class '.action-email' and checks visibilityInput field for email is visible on the pageElement '.action-email' is visiblePASS
Failure Scenario
Failing Condition: The text 'type' is not found on the page, so cy.contains() fails to find the element
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.contains('type') do in this test?
AFinds an element with the class 'type'
BFinds an element with the id 'type'
CFinds an element that has the text 'type' anywhere inside it
DFinds an element with the attribute type='button'
Key Result
Using cy.contains() is a simple and readable way to find elements by their visible text, making tests easier to write and understand.