Challenge - 5 Problems
cy.contains() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat does this cy.contains() command select?
Given the HTML below, what element will
cy.contains('Submit') select?Cypress
<button>Submit</button> <button>Submit Form</button> <div>Submit</div>
Attempts:
2 left
💡 Hint
cy.contains() finds the first element that contains the text anywhere inside it.
✗ Incorrect
cy.contains() finds the first element in the DOM that contains the given text, not necessarily an exact match. It stops at the first match.
❓ assertion
intermediateWhich assertion correctly checks that an element contains the text 'Welcome'?
You want to verify that a
contains the text 'Welcome'. Which assertion is correct?
Cypress
cy.get('div').contains('Welcome')
Attempts:
2 left
💡 Hint
Use the correct Cypress assertion for partial text matching.
✗ Incorrect
contain.text checks if the element's text includes the given substring. have.text requires exact match.
❓ locator
advancedWhich cy.contains() usage finds a button with exact text 'Cancel' only?
You want to find a
Attempts:
2 left
💡 Hint
Use a regular expression to match exact text in cy.contains().
✗ Incorrect
Passing a regex /^Cancel$/ to cy.contains() matches exact text only. Option A matches partial text.
🔧 Debug
advancedWhy does this cy.contains() command fail to find the text?
Given the HTML:
<span>Click here</span>, why does cy.contains('click here') fail?Cypress
cy.contains('click here')Attempts:
2 left
💡 Hint
Check text case sensitivity in cy.contains().
✗ Incorrect
cy.contains() matches text case-sensitively by default, so 'click here' does not match 'Click here'.
❓ framework
expertHow to chain cy.contains() to find a child element with specific text inside a parent?
You want to find a
with class 'card' that contains a
with text 'Hello'. Which command correctly finds the
inside the card?
Attempts:
2 left
💡 Hint
Use cy.contains() with selector to find child element inside parent.
✗ Incorrect
cy.get('div.card').contains('p', 'Hello') finds a
inside the
but then filters text, which may fail if multiple
exist.
Practice
1. What does
cy.contains('Submit') do in a Cypress test?easy
Solution
Step 1: Understand cy.contains() basic function
The command searches the page for an element that has visible text matching the given string.Step 2: Analyze the given text 'Submit'
It finds the first element that contains the text 'Submit' anywhere inside it.Final Answer:
Finds the first element containing the visible text 'Submit'. -> Option AQuick Check:
cy.contains('text') finds element by visible text [OK]
Hint: cy.contains() finds elements by visible text quickly [OK]
Common Mistakes:
- Thinking cy.contains() clicks automatically
- Confusing cy.contains() with typing commands
- Assuming it checks URL instead of page content
2. Which of the following is the correct syntax to find a button with text 'Login' using
cy.contains()?easy
Solution
Step 1: Recall cy.contains() syntax with selector
The correct syntax is cy.contains(selector, text) where selector is a string like 'button'.Step 2: Match the correct option
cy.contains('button', 'Login') uses cy.contains('button', 'Login') which is correct. Other options misuse quotes or argument order.Final Answer:
cy.contains('button', 'Login') -> Option BQuick Check:
cy.contains('selector', 'text') correct syntax [OK]
Hint: Use cy.contains('selector', 'text') to narrow search [OK]
Common Mistakes:
- Omitting quotes around selector or text
- Swapping the order of selector and text
- Concatenating strings incorrectly inside cy.contains()
3. What will the following Cypress code do?
cy.contains('Submit').click()medium
Solution
Step 1: Understand cy.contains() chaining
cy.contains('Submit') finds the first element with visible text 'Submit'.Step 2: Analyze the chained click()
Calling .click() on the found element triggers a click event on it.Final Answer:
Finds the first element with text 'Submit' and clicks it. -> Option CQuick Check:
cy.contains('text').click() clicks matched element [OK]
Hint: Chain .click() after cy.contains() to click element [OK]
Common Mistakes:
- Thinking click() types text instead
- Assuming click() cannot be chained
- Believing it clicks multiple elements
4. You wrote
cy.contains(button, 'Save').click() but the test fails with a syntax error. What is the problem?medium
Solution
Step 1: Check argument types in cy.contains()
The selector argument must be a string, so 'button' needs quotes.Step 2: Identify the syntax error cause
Using button without quotes causes a syntax error because it's treated as a variable.Final Answer:
The selector button should be a string: 'button'. -> Option DQuick Check:
Selector must be string in cy.contains() [OK]
Hint: Always quote selectors in cy.contains() [OK]
Common Mistakes:
- Forgetting quotes around selector
- Thinking click() can't be chained
- Assuming text case matters in cy.contains()
5. You want to verify that a page contains a link with text 'Learn More' inside a
<nav> element. Which Cypress command is best?hard
Solution
Step 1: Narrow search inside <nav> for link text
Use cy.get('nav') to select the nav element first.Step 2: Use contains() with selector 'a' and text 'Learn More'
Calling .contains('a', 'Learn More') inside nav finds the link with that text.Step 3: Assert existence with should('exist')
This confirms the link is present inside nav.Final Answer:
cy.get('nav').contains('a', 'Learn More').should('exist') -> Option AQuick Check:
Use get() then contains() to scope text search [OK]
Hint: Use cy.get() then contains() to scope text search [OK]
Common Mistakes:
- Using contains() without scoping to nav
- Trying to chain contains() incorrectly
- Assuming contains() alone checks element existence
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
