kind-mug
kind-mug

Reputation: 97

How to filter with multiple conditions based on elements text in Cypress?

Given this HTML, I want to yield the .row element based on code = 002 and ref = 001

<div class="row">
    <span class="code">001</span>
    <span class="ref">002</span>
    <label>Some Label</label>   
</div>
<div class="row">
    <span class="code">002</span>
    <span class="ref">002</span>
    <label>Another label</label>    
</div>
<div class="row">
    <span class="code">002</span>
    <span class="ref">001</span>
    <label>Something</label>    
</div>

I would have thought the following should work but it doesnt filter anything

cy.get('.row').filter(`span.code:contains("002")span.ref:contains("001")`)

Even with one condition it seems to not work. Any idea what Im doing wrong?

Thanks for the help

Upvotes: 2

Views: 1214

Answers (1)

kind-mug
kind-mug

Reputation: 97

As someone suggested in the comment I used a function that retrieve elements with jQuery. This is working

cy.get('.row').filter((i, $el) => {
        return Cypress.$($el).find('span.code:contains("002")').length > 0
          && Cypress.$($el).find('span.ref:contains("001")').length > 0;
      })

Upvotes: 4

Related Questions