Positive number

Imagine a page component that initially shows zero, but then loads and shows a number. We want to confirm that number is positive.

<div id="app">
  <div id="points">0</div>
  <p>Total points</p>
#points {
  font-weight: bold;
  setTimeout(() => {
    const app = document.getElementById('app')
    app.innerHTML = `
      <div id="points">200</div>
      <p>Total points</p>
  }, 1000)

My initial code will show a wrong solution. We will get the text of the element, convert it to a number, and then check if the number is greater than zero. Unfortunately, the cy.then(parseInt) stops the retries, plus the entire element #points is replaced anyway when the application overwrites it.

  .should('be.greaterThan', 0)

Instead, let's wait for the number to appear by checking the #points element using the cy.containsopen in new window command. When the text matches a digit other than zero, we know the number has been loaded. We can then get the text, convert it to a number, and check if that number is above zero.

cy.contains('#points', /[1-9]/)
  .should('be.greaterThan', 0)

See also