Cypress assert label and checkbox child elements

Viewed 23

Issue I'm having is I have a list of items (all with the same selector)and I want to loop through these items and assert a text label on one child and a checkbox on another child of the same parent element match what I expect. I can easily solve this solution in Selenium, but struggling to find the answer in Cypress.

1 Answers

I found the answer to this myself, essentially using filters, which I hadn't used before, to find the parent with the child text I wanted, before checking the checkbox of another child. Ignore the If as it's not necessary for the solution

if (parentSelector) {
  cy.get(parentSelector).filter((_, element) => {
    return element.querySelector('h2').textContent === labelValue
  }).then( element => {
    cy.wrap(element).find(selector).should('not.be.checked')
  })
Related