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

Cypress › Locators and working with the DOM

How do you narrow the search: within, eq, first, filter?

Narrowing commands: within limits the search scope, eq, first and last take an element by index, filter keeps the matching ones by selector or function. An index expresses order, which is rarely a requirement, so a row is better found by content: cy.contains by order number, then within or find. Inside within, elements outside the subject cannot be found, such as a portal at the end of body.

Cypress · Locators and DOM

A table has ten rows with a Cancel button. How do you press the right one without depending on order?

The same button repeats in every row. cy.get('[data-cy=cancel]').click() picks several elements and cannot tell which one. This is where narrowing commands come in: within, eq, first, last, filter.

How it works and why. .within(() => {...}) runs the nested commands inside the subject: there cy.get and cy.contains search only within it. within passes the subject on unchanged. .eq(n) takes an element by index (zero-based, a negative one counts from the end), .first() and .last() are shortcuts. .filter() keeps the elements of a set that match a selector or a function (the jQuery :contains() selector works too). eq, first, last and filter are queries, so they re-run together with the preceding queries of the chain; within is a regular command and does not retry. Narrowing by content beats an index, because an index expresses an order that is rarely a requirement.

From the row to the action

1. The row by textcy.contains('[data-cy=row]', text)2. Scope: within or findall commands inside the row3. Action and assertionstatus, cancel
Figure 1. The container is found by content, then everything works within its bounds.

Example: the button in the right row.

Brittle

eq(3)

After a sort or a new order a different row sits in fourth place and the test cancels someone else's order.

Stable

contains + within

The row is found by the order number, so order does not matter.

cy.contains('[data-cy=row]', 'Order #1042').within(() => {
  cy.get('[data-cy=status]').should('have.text', 'Shipped');
  cy.get('[data-cy=cancel]').click();
});

// order is the requirement: the newest is first
cy.get('[data-cy=row]').first().should('contain', '#1043');

// filter a set by content
cy.get('[data-cy=product]').filter(':contains("Flask")').first().click();

In the first block cy.get('[data-cy=status]') searches only in the found row, so other rows' statuses do not get into the assertion. The second block uses an index where it is part of the check.

Limits and alternatives. Inside within you cannot find an element that sits outside the subject in the DOM: a dropdown list or a modal rendered at the end of body. Then leave the block and search from the root. For going up there are .closest() and .parent(), for excluding .not(). Before eq it helps to make sure the list is complete: .should('have.length', 10).

And in Playwright:

Instead of within there is locator composition, instead of filter(':contains') the filter({ hasText }) option.

const row = page.getByTestId('row').filter({ hasText: '#1042' });
await row.getByTestId('cancel').click();

Common mistakes.

  • .eq(0) on a list that is still loading: the first element is found, just not the right one. Wait for the state with an assertion first.
  • An action with a side effect inside within with no assertion: after the click the block may disappear and later commands fail.
  • Expecting a new subject from within: it returns the same element it received.

Check yourself

Why is .eq(3) worse than cy.contains(row, text) for a button in a row?

Answer

An index depends on row order, which sorting, pagination and new data change. A content lookup is tied to what the test really checks.

What will not work in within for a list that opens in a portal at the end of body?

Answer

cy.get inside within searches only within the subject, and the portal lies outside it. The search must run outside the block.

In an interview. Junior: Within limits the search to one block: I find the row by text and all commands inside search only there. Eq, first and last take by index, but I use them only when the order really matters. Middle/Senior: An index encodes order, and order is rarely a requirement, so I narrow by content: contains by the number, then within or find. I keep an index for checking the order itself, such as “newest on top”. I also know the limit of within: portals and modals sit outside the block.

More articles in this chapter