0
0
Cypresstesting~10 mins

cy.within() for scoped queries in Cypress - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Cypress
Cypress
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')
    })
  })
})
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opens and navigates to 'https://example.cypress.io/commands/actions'Page loaded with #action-section containing button#action-btn with text 'Click Me'-PASS
3Find element with id 'action-section' using cy.getSection element found-PASS
4Scope queries inside #action-section using cy.within()Scoped context set to #action-section-PASS
5Find 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
6Click the button inside scoped sectionButton clicked, UI updates button text to 'Clicked'-PASS
7Verify button text changed to 'Clicked' inside scoped sectionButton text is now 'Clicked'Button text equals 'Clicked'PASS
8Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: Button with id 'action-btn' is not found inside #action-section or text does not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.within() do in this test?
AClicks all buttons on the page
BLimits all queries to inside the #action-section element
CNavigates to a new page
DWaits for the button to appear
Key Result
Using cy.within() scopes queries to a specific part of the page, making tests more reliable and easier to read by avoiding searching the whole page.