QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Locators and working with the DOM

Which selectors does Cypress recommend and why?

Cypress recommends dedicated test attributes: data-cy, data-test or data-testid. Classes, tags and position serve markup and styles and change without regard for tests, while a test attribute exists only for them and changes deliberately. For semantic lookups there is cy.contains, when the text itself is the requirement. If the code cannot be changed, contains with a selector, a role or a stable id are the fallback.

Cypress · Locators and DOM

A designer renamed a button class and half the suite fell over. Which selectors survive that?

A test like cy.get('.btn.btn-primary.mt-2') passes until the markup changes. After a style refactoring it fails although the behaviour is the same. Tests need a selector that changes only when the thing under test changes.

How it works and why. The Cypress docs (Best Practices, “Selecting Elements”) recommend dedicated data-* attributes: data-cy, data-test or data-testid. The reason is simple: classes, tags and positions serve markup and styles, have many owners and are changed without a thought for tests. A test attribute exists only for tests, so changing it is deliberate and visible in code review. Cypress's selector picker also suggests such attributes first by default (data-cy, data-test, data-testid, data-qa). For semantic lookups there is cy.contains, and since Cypress 16.2 there is also a built-in query cy.getByRole('button', { name: 'Sign in' }); earlier the Testing Library plugin (findByRole) was used for this.

Selector resilience

data-cy, data-test, data-testida separate contract for testsgetByRole, contains + selectorchange together with the requirementid, style classesdepend on a developer and CSSnth-child, div > div > abreak on any layout change
Figure 1. The higher, the fewer reasons for a selector to break without a behaviour change.
Brittle

Classes and position

.btn.btn-primary.mt-2 and form > div:nth-child(3) > button break on a style tweak or a new field.

Stable

data-cy

[data-cy=submit] depends on neither styles, text nor DOM position.

Example: from brittle to stable.

// before: depends on styles and order
cy.get('form > div:nth-child(3) > button').click();

// after: a developer added the attribute, the test uses it
cy.get('[data-cy=submit]').click();

// text is the requirement: contains with a selector
cy.contains('button', 'Sign in').click();

// role and accessible name (Cypress 16.2+)
cy.getByRole('button', { name: 'Sign in' }).click();

Role and accessible name have an extra benefit: the test also checks that the button is reachable for a screen reader. But a copywriter can change a button text, while data-cy stays.

Limits and alternatives. If the code base cannot be changed (someone else's site, a third-party widget), you have to use contains, a role or a stable id. The attribute priority Cypress suggests is changed with Cypress.ElementSelector.defaults({ selectorPriority: ['data-cy', 'data-test'] }) (before Cypress 15 this API was called Cypress.SelectorPlayground). XPath is not built into Cypress, so it is not a default option.

And in Playwright:

Semantic locators have long been built in: getByRole, getByLabel, getByPlaceholder, getByTestId. The attribute for getByTestId is set with testIdAttribute, for example to data-cy.

Common mistakes.

  • Generated CSS-module classes (.css-1x2y3): they change with every build.
  • A data-cy that describes looks (blue-button) instead of purpose (submit): the name lies after a redesign.
  • A positional selector where order is not a requirement: the test fails from a new form field.

Check yourself

Why is data-cy more stable than the .btn-primary class?

Answer

A class belongs to styling and changes with the design. A test attribute exists only for tests, so it changes deliberately, not as a side effect.

When is cy.contains a better choice than data-cy?

Answer

When the text itself is the requirement (a message, a button name) or the page code cannot be changed. Even then add a selector: cy.contains('button', ...).

In an interview. Junior: The best are dedicated test attributes like data-cy: nobody touches them when styles change. Contains by text fits when the text is itself the requirement. Classes and positional selectors break on any markup change. Middle/Senior: A selector should change only together with what is tested. So: data-cy as a contract with developers, getByRole where accessibility matters, contains with a selector for text that is a requirement. If the code cannot be changed, that is a conscious trade-off I document in the test.

More articles in this chapter