Test Overview
This test checks that a button inside a specific section can be clicked using cy.within() to scope the query. It verifies the button's text changes after the click.
This test checks that a button inside a specific section can be clicked using cy.within() to scope the query. It verifies the button's text changes after the click.
describe('Scoped queries with cy.within()', () => { it('Clicks button inside a specific section and verifies text change', () => { cy.visit('https://example.cypress.io/commands/actions') cy.get('#action-section').within(() => { cy.get('button#action-btn').should('have.text', 'Click Me').click() cy.get('button#action-btn').should('have.text', 'Clicked') }) }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io/commands/actions' | Page loaded with #action-section containing button#action-btn with text 'Click Me' | — | PASS |
| 3 | Find element with id 'action-section' using cy.get | Section element found | — | PASS |
| 4 | Scope queries inside #action-section using cy.within() | Scoped context set to #action-section | — | PASS |
| 5 | Find button with id 'action-btn' inside scoped section and check its text is 'Click Me' | Button found with text 'Click Me' | Button text equals 'Click Me' | PASS |
| 6 | Click the button inside scoped section | Button clicked, UI updates button text to 'Clicked' | — | PASS |
| 7 | Verify button text changed to 'Clicked' inside scoped section | Button text is now 'Clicked' | Button text equals 'Clicked' | PASS |
| 8 | Test ends | Test completed successfully | — | PASS |
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