typescript - 将一个字母替换为 null 时出错,然后在 javaScript 和 cypress 中将其与数字比较
我尝试通过 Cypress 从网页读取价格,将 $49.99 转换为 49.99,以便与 40.00 进行比较
使用这段代码
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
然后我收到这个错误
cy.each() can only operate on an array like subject. Your subject was: $49.99
然后我尝试移除每一个
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
然后我收到这个错误
txt.replace(...).then is not a function
如何解决它?
答案#1
在第一种情况下,这是因为 invoke 调用了 jQuery 的 text 函数,而 each 则遍历类似数组的结构(具有 length 属性的数组或对象)——Cypress 文档。
我建议你的下一个解决方案:
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);
)}
