Overview - cy.contains() for text matching
What is it?
cy.contains() is a command in Cypress that finds elements on a web page by matching their visible text. It helps testers locate buttons, links, or any element containing specific words or phrases. This makes it easier to interact with elements without needing complex selectors. It works by searching the page for the text you provide and returns the first matching element.
Why it matters
Without cy.contains(), testers would have to rely on complicated CSS selectors or element IDs that might change often, making tests fragile and hard to maintain. Using text to find elements mimics how a user sees the page, making tests more readable and stable. This reduces test failures caused by UI changes and speeds up writing tests.
Where it fits
Before learning cy.contains(), you should understand basic Cypress commands like cy.get() and how to write simple tests. After mastering cy.contains(), you can learn advanced querying techniques, custom commands, and how to combine text matching with other selectors for precise testing.