0

What I am working with:

<ul class="abc">
 <li class="xyz">
  <a href="www.something.com">
   <div>
    <h2>
     <div>
      <div class="theText">
       <div>Get this text</DIV>
      </div>
     </div>
    </h2>
   </div>
  </a> 
  <button aria-label="remove">...</button>
 </li>
 <li class="xyz">...Same stuff here as above li...</li>
 <li class="xyz">...Same stuff here as above li...</li>
 <li class="xyz">...Same stuff here as above li...</li>
</ul>

The button here has two states for the aria-label attribute which is remove (for when the button is clicked) and add (for when the button is not yet clicked).

What I want to achieve:
I want to get the value within the <a> tag, which in this case is "Get this text", BUT only if the button within its same <li> tag is set to aria-label="remove". I will also be storing the values that I get within an array to later on compare with another array.

What I have tried:

let myArray: any = []

cy.get('li[class="xyz"]').each(($element) => {
    cy.get('li[class="xyz"]').within(($element) => {
        cy.wrap($element)
            .find('button[aria-label="remove"]')
            .find('div[class="theText"]')
            .invoke('text').then(text => {
                myArray.push(text)
            })
    })
}).then(() => {
    cy.wrap(myArray).as('myArray')
})

With the above code, I am getting this Assertion Error from Cypress.

krisc
  • 95
  • 1
  • 10
  • Haven't used cypress. However if I understand but first why don't you do cy.get('li.xyz')? Also You are querying all the specific li items and for each one you are trying to find another `.xyz` element? however based on the provided markup no other .xyz exist – Manos Kounelakis Oct 14 '22 at 04:15
  • @ManosKounelakis - All the other `
  • ` tags have the same `class="xyz"`. Let me update the post, thanks for that.
  • – krisc Oct 14 '22 at 06:53
  • 2
    You can remove the `cy.get('li[class="xyz"]').within()` line since you are wrapping `$element` and using `.find()` - within and find do pretty much the same thing. – Roberta D Oct 14 '22 at 07:57