error in replace one letter to null, then compare it with number in javaScript and cypress

Viewed 285

i tried to read price form webPage through cypress to convert $49.99 to 49.99 to be able to compare it with 40.00

with this code

  cy.get('.s-item').find('.s-item__price').invoke('text')
  .each(txt => txt.replace(/\$/g, '').then(parseFloat).should('be.gt', 40.00) // true
.and('be.lt',100.00 )    // true

and i get this error

      cy.each() can only operate on an array like subject. Your subject was: $49.99

then i tried to remove each

   cy.get('.s-item').find('.s-item__price').invoke('text')
  .then(txt => txt.replace(/\$/g, '').then(parseFloat).should('be.gt', 40.00) // true
.and('be.lt',100.00 )    // true

and i get this error

     txt.replace(...).then is not a function

how to solve it?

1 Answers

In first case it happens because invoke calls for jQuery text function, however each iterates through an array like structure (arrays or objects with a length property) - Cypress documentation.

I suggest you next solution:

 cy.get('.s-item').find('.s-item__price').each(($el) => {
    expect(parseFloat($el.text().replace(/\$/g, ''))).to.be.greaterThan(40)
        .but.to.be.lessThan(100);
 )}
Related