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

Cypress › Locators and working with the DOM

Selectors and data-cy

data-cy works as a contract between development and tests: the name describes purpose, with one prefix per project. The attribute is added by the author of the component, and selectors go through one command such as getBySel, so the format changes in one place. The attributes stay in production HTML, so if a build strips them, e2e on that build breaks.

Cypress · Locators and DOM

Where in a project do you agree on data-cy so that selectors do not drift between tests?

Choosing data-cy is half of the job. The other half: who adds it, how it is named and where a test gets its selector. Without that, half a year later some tests look for [data-cy=submit], others for [data-test=submit-btn], and developers remove the “unused” attributes.

How it works and why. A test attribute works as a contract between development and tests. According to the docs it does not depend on styles, text or position, and its presence in the code signals that the element is used by tests. The contract rests on three agreements: the name describes purpose (submit, order-price), not looks; one prefix per project; the attribute is added by whoever writes the component, not only by the test author. It also pays to move the selector into one command so the format changes in one place.

Drifted

Three formats

[data-cy=submit], [data-test=submit-btn], .submit: the same button, different selectors in different files.

Unified

One prefix and a helper

All selectors through one command, names by purpose, the format changes in one place.

Example: a getBySel command.

// cypress/support/commands.ts
Cypress.Commands.add('getBySel', (sel: string) => {
  return cy.get('[data-cy=' + sel + ']');
});

declare global {
  namespace Cypress {
    interface Chainable {
      getBySel(sel: string): Chainable<JQuery<HTMLElement>>;
    }
  }
}
export {};

// in a test
cy.getBySel('submit').click();
cy.getBySel('order-price').should('have.text', '$1,200');

Now renaming the attribute, for example to data-testid, is a single edit. Such a command does not change the scope rules: it is the same cy.get, so a nested lookup in a chain still needs find.

Limits and alternatives. Test attributes end up in production HTML. The volume is small and most teams keep them, but some strip them at build time. Whoever strips them must know that e2e on a production build breaks afterwards. An attribute does not replace accessibility: for elements with a role and a name getByRole (Cypress 16.2+) or cy.contains with a selector fits. Dynamic identifiers (row-1042) are better built from data, not from an index.

And in Playwright:

No helper is needed: getByTestId is built in and the attribute name is set once through testIdAttribute in the config.

Common mistakes.

  • Agreeing on the attribute only in the test author's head: a developer removes the “unused” one and the test fails.
  • A name by state or colour (red-btn): the name lies after a change.
  • One command that adds both force and the lookup: a hidden risk in every test.

Check yourself

What advantage does a getBySel command have over cy.get('[data-cy=...]') everywhere?

Answer

The selector format lives in one place: changing the attribute or naming rule does not touch a hundred tests.

Why should a data-cy name describe purpose, not looks?

Answer

Looks change with the design and the name (blue-button) starts to lie, while the purpose (submit) holds as long as the behaviour holds.

In an interview. Junior: I agree with developers on data-cy as a contract: names by purpose, one prefix, the attribute is added by the component author. Middle/Senior: I put selectors into one command such as getBySel so the format changes in one place. I know the trade-off: attributes stay in production HTML, and whoever strips them breaks e2e on a production build.

More articles in this chapter