0
0
Cypresstesting~5 mins

cy.contains() for text matching in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does cy.contains() do in Cypress?

cy.contains() finds an element that contains specific text on the page. It helps you locate elements by their visible text.

Click to reveal answer
beginner
How do you use cy.contains() to find a button with text 'Submit'?

You write cy.contains('button', 'Submit'). This finds a <button> element that has the text 'Submit'.

Click to reveal answer
intermediate
Can cy.contains() find partial text matches?

Yes! cy.contains() matches elements that contain the given text anywhere inside. It does not need to be an exact match of the whole text.

Click to reveal answer
intermediate
What happens if cy.contains() does not find any matching element?

The test will fail with an error saying no element with the specified text was found. Cypress waits a short time before failing to allow the page to load.

Click to reveal answer
advanced
How can you use cy.contains() with a regular expression?

You can pass a regular expression instead of a string, like cy.contains(/submit/i) to find text ignoring case.

Click to reveal answer
What does cy.contains('Login') do?
AFinds any element containing the text 'Login'
BFinds only buttons with text 'Login'
CFinds elements with the exact text 'Login' only
DClicks on the element with text 'Login'
How do you find a <div> containing 'Welcome' using cy.contains()?
Acy.contains('div', 'Welcome')
Bcy.contains('Welcome')
Ccy.get('div').contains('Welcome')
Dcy.find('div').contains('Welcome')
What will cy.contains(/submit/i) match?
AElements with text exactly 'submit'
BElements with text starting with 'submit'
CElements with text containing 'submit' ignoring case
DElements with text 'Submit' only
If cy.contains() does not find the text, what happens?
ATest retries forever
BTest fails with an error
CTest passes silently
DTest skips the step
Which is a correct way to find a button with text 'Cancel'?
Acy.contains('Cancel')
Bcy.get('button').contains('Cancel')
Ccy.find('button').contains('Cancel')
Dcy.contains('button', 'Cancel')
Explain how cy.contains() helps in locating elements by text in Cypress tests.
Think about how you find a button or link by its label on a webpage.
You got /4 concepts.
    Describe how you would use cy.contains() to find a case-insensitive match for the text 'submit'.
    Remember regex flags for ignoring case.
    You got /4 concepts.