how to traverse and get the text value of the sibling of a parent in Cypress

236 Views Asked by At

Is there a way to get the text of the sibling of a parent in Cypress?

enter image description here

I tried this but cypress can't find the element of the siblings

cy.get('[role="switch"]')
  .parentsUntil('[class="v-list-item__icon px-3"]')
  .siblings('[class="v-list-item__title px-3"]')
1

There are 1 best solutions below

1
Lola Ichingbola On

parentsUntil() gives you all parents, excluding the one you specified in the selector, and they are in reverse order.

So you want the .last() of the elements selected by .parentsUntil('[class="v-list-item__icon px-3"]'), and you want it's .parent() - which is actually [class="v-list-item__icon px-3"].

cy.get('[role="switch"]')
  .parentsUntil('[class="v-list-item__icon px-3"]')
  .last()                               // child of class="v-list-item__icon px-3"
  .parent()                             // div with class="v-list-item__icon px-3"
  .siblings('[class="v-list-item__title px-3"]')
  .should('have.text', 'Type')