Finding elements inside another using .within()

The purpose of the .within command is to focus Cypress to look for specific elements inside of another.

cy.get('div[class*="MuiPaper-elevation1 MuiPaper-rounded"]')
  .eq(6)
.within(() => {
cy.get('[name="refundReason"]', { timeout: 5000 })
.focus()
.type('Automation test', { timeout: 5000 })
.wait(1000)
.get('[data-testid="refund-confirm"]').click()
})

What this does is tell Cypress to look for the name ‘refundReason’ inside of the element defined as ('[class*=”MuiPaper-elevation1 MuiPaper-rounded”]').eq(6) (the seventh instance of this on the page as they are recognized by Cypress as 0-6 instead of 1-7)

Previous
Previous

Adding additional custom commands

Next
Next

Exact text matches