Use the Most Robust Selector for Cypress Tests

Share this video with your friends

Send Tweet

Which selectors your choose for your tests matter, a lot. In this lesson, we'll see the recommended Cypress best practices for selectors, and why we should prefer the data-cy attribute.

Recommended Homework:

  • Finish the example using cy.contains, and remove the false positives to get a richer sense of why data-cy is a better choice for our selector
Dan
Dan
~ 5 years ago

I'm getting Error: CypressError: Timed out retrying: Expected to find element: 'Hello world', but never found it. for some reason. Haven't looked into it as this probably isn't a feature I would use, but just a heads up

James
James
~ 5 years ago

why not to use id attribute?

It is good to be intentional, data attributes for testing are easy to identify and are less likely to be miss used by someone looking to attach JavaScript behaviour or CSS to an element. And they can be changed with a higher certainty that they will not break anything un-intentionally.