← Back to library
Cypressselectorsintermediatev10+
Scope commands with .within()
Use .within() to constrain all cy.get() calls to a specific parent — prevents false positives when the same names appear in multiple forms.
1it('fills billing and shipping forms independently', () => {
2 cy.visit('/checkout');
3
4 // .within() constrains all subsequent cy.get() calls to this element —
5 // prevents matching the same [name="email"] in both forms at once
6 cy.get('[data-testid="billing-form"]').within(() => {
7 cy.get('[name="first-name"]').type('Jane');
8 cy.get('[name="last-name"]').type('Doe');
9 cy.get('[name="postcode"]').type('SW1A 1AA');
10 });
11
12 cy.get('[data-testid="shipping-form"]').within(() => {
13 cy.get('[name="first-name"]').type('John');
14 cy.get('[name="postcode"]').type('EC1A 1BB');
15 });
16});#within#scope#parent#form#context