Verify form fields inside a specific section using cy.within()
Preconditions (2)
✅ Expected Result: The form fields inside the 'user-form' section accept input and the form submits successfully showing the success message
describe('Form section scoped queries with cy.within()', () => { beforeEach(() => { cy.visit('/path-to-form-page'); }); it('fills and submits the form inside #user-form section', () => { cy.get('#user-form').within(() => { cy.get('input[name="firstName"]').type('John').should('have.value', 'John'); cy.get('input[name="lastName"]').type('Doe').should('have.value', 'Doe'); cy.get('button[type="submit"]').click(); }); cy.get('#success-msg').should('be.visible'); }); });
The test starts by visiting the page containing the form.
Using cy.get('#user-form').within(), we scope all queries inside the form section. This means all cy.get() calls inside the within block only look inside the #user-form element.
We type 'John' and 'Doe' into the respective input fields and assert the values were entered correctly.
Then we click the submit button inside the same section.
Finally, outside the within block, we verify that the success message is visible, confirming the form submission worked.
This approach keeps selectors scoped and tests reliable.
Now add data-driven testing with 3 different sets of first and last names
cy.within() in Cypress tests?cy.within()cy.within() scopes all commands inside it to a specific part of the page, so only elements inside that section are targeted.cy.within().within as focusing on one page part [OK]cy.within() with global selectorscy.within() to scope commands inside a <form> element with id loginForm?cy.within() usagewithin() with a callback containing scoped commands.cy.within() does not take a selector as first argument. cy.within(() => cy.get('#loginForm')) misuses within by nesting get inside callback. cy.get('#loginForm').scope(() => { /* commands */ }) uses a non-existent scope method.within() on a selected element [OK]within()scope()get inside within callback<div id="section1"> <button class="btn">Click Me</button> </div> <div id="section2"> <button class="btn">Don't Click Me</button> </div>
cy.get('#section1').within(() => {
cy.get('.btn').click()
})within() scopewithin() limits commands to elements inside #section1. So cy.get('.btn') finds only the button inside #section1.#section1 has text 'Click Me', so that is clicked. Buttons outside #section1 are ignored.cy.within(() => {
cy.get('.item').click()
})Cannot read property 'within' of undefined. What is the likely cause?within is called on undefined, meaning cy.within() is not a valid method.within()within() is a method on a selected element, like cy.get(...).within(). Calling cy.within() directly is invalid.within() -> Option Dwithin() on element, not on cy [OK]within() [OK]within() directly on cyform2. Which Cypress code correctly does this?form2 to avoid clicking the wrong button.cy.get('#form2').within() to scope commands, then finds and clicks the 'Submit' button inside. cy.contains('Submit').click() clicks the first 'Submit' button anywhere, which is unsafe. cy.get('button').contains('Submit').click() tries to chain contains incorrectly and is ambiguous. cy.within('#form2', () => { cy.get('button').click() }) uses invalid syntax for within().get then within to scope clicks [OK]within with selector argumentcontains chainingPass the test and this pattern moves unknown