Checking if the values are greater than a certain number in cypress

Viewed 1097

I have this div and want to check if all the texts within this div are greater than 800. Is there any way to do this in cypress?

<div>
 <div>700</div>
<div>720</div>
<div>810</div>
<div>830</div>
<div>850</div>
</div>


 it("Show cars below 700 euro", () => {
   
    var prices=cy.get("div > div >").should()
   )}
  
2 Answers

First assign a className for your div, for example value1.

 <div className="value1">700</div>

Then:

cy.get(".value1").invoke('text').then(parseFloat).should('be.gte', 800)

Assuming that doing cy.get("div > div") will return you only the elements you want to check, you can simply iterate over the yielded element list, using .each()

cy.get("div > div").each((el) => {
  const text = el.text();
  expect(text).to.be.greaterThan(800);
})

If the div > div does not uniquely yield only the elements you want to check, you will need to find a different way to uniquely isolate these elements. Adding a className, like Ali suggests, is one way.

Related