繁体   English   中英

将一个字母替换为 null 时出错,然后将其与 javaScript 和 cypress 中的数字进行比较

[英]error in replace one letter to null, then compare it with number in javaScript and 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

如何解决?

在第一种情况下,它发生是因为invoke jQuery text function,但是each遍历类似数组的结构(具有长度属性的数组或对象)-赛普拉斯文档

我建议你下一个解决方案:

 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);
 )}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM