Discover how a single command can save you hours of frustrating searching in your tests!
Why cy.contains() for text matching in Cypress? - Purpose & Use Cases
Imagine you have a webpage with many buttons and links, all with different texts. You want to check if a button with the text "Submit" is present. Manually searching through the HTML code or clicking every button to find it is like looking for a needle in a haystack.
Manually scanning the page or writing long selectors for each element is slow and tiring. You might miss the exact text or pick the wrong element. This causes mistakes and wastes time, especially when the page changes often.
The cy.contains() command in Cypress lets you quickly find any element by its visible text. It works like a smart search that finds the right element without complicated selectors. This makes tests faster, clearer, and less error-prone.
cy.get('button').eq(3).click(); // guessing the button position
cy.contains('button', 'Submit').click(); // directly finds button by text
With cy.contains(), you can write simple, reliable tests that focus on what users see and interact with, not on complex code details.
Testing a signup form where the "Register" button might move or change style, but the text stays the same. Using cy.contains('button', 'Register') ensures your test clicks the right button every time.
Manually finding elements by text is slow and error-prone.
cy.contains() finds elements by visible text easily.
This makes tests simpler, faster, and more reliable.
Practice
cy.contains('Submit') do in a Cypress test?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]
- Thinking cy.contains() clicks automatically
- Confusing cy.contains() with typing commands
- Assuming it checks URL instead of page content
cy.contains()?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]
- Omitting quotes around selector or text
- Swapping the order of selector and text
- Concatenating strings incorrectly inside cy.contains()
cy.contains('Submit').click()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]
- Thinking click() types text instead
- Assuming click() cannot be chained
- Believing it clicks multiple elements
cy.contains(button, 'Save').click() but the test fails with a syntax error. What is the problem?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]
- Forgetting quotes around selector
- Thinking click() can't be chained
- Assuming text case matters in cy.contains()
<nav> element. Which Cypress command is best?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]
- Using contains() without scoping to nav
- Trying to chain contains() incorrectly
- Assuming contains() alone checks element existence
Ready to prove it?
Pass the test and this pattern moves unknown
