如何断言在柏树中分开的数字

发布于 2025-02-11 14:52:48 字数 332 浏览 2 评论 0原文

在应用程序上编写测试时,我遇到了一个问题。

我需要从DOM的跨度内获取一个数字,然后断言数字是否在特定范围之间。 我可以通过使用

cy.get('#my_selector').invoke('text').should('be.gt',lower_bound).and('be.lt',upper_bound)

,但问题是逗号分隔的数字像5,000一样分开。我会遇到一个错误,因为“预期的5,000'为数字或日期”

是否有任何简单的简短方法将其转换为纯数字

While writing tests on an application I have came across a problem.

I need to fetch a number inside a span from the DOM and then assert if the number is between a specific range.
I can do it by using

cy.get('#my_selector').invoke('text').should('be.gt',lower_bound).and('be.lt',upper_bound)

But the issue is the number is comma separated like 5,000. and I'm getting an error as "expected '5,000' to be a number or a date"

Is there any simple short way to convert it into pure numeric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2025-02-18 14:52:48

您可以使用string.replaceall()删除“”,然后parseint()

cy.get('#my_selector')
  .invoke('text')
  .then(str => parseInt(str.replaceAll(',', '')))
  .should('be.within', lower_bound, upper_bound)

You can use string.replaceAll() to remove the "," then parseInt()

cy.get('#my_selector')
  .invoke('text')
  .then(str => parseInt(str.replaceAll(',', '')))
  .should('be.within', lower_bound, upper_bound)
长亭外,古道边 2025-02-18 14:52:48

您可以使用javascript 替换方法来删除逗号,然后添加+将其转换为数字,例如:

cy.get('#my_selector')
  .invoke('text')
  .then((num) => {
    cy.wrap(+num.replace(/,/g, ''))
      .should('be.gt', lower_bound)
      .and('be.lt', upper_bound)
  })

You can use the javascript replace method to remove the comma and then add a + to convert it into a number, like:

cy.get('#my_selector')
  .invoke('text')
  .then((num) => {
    cy.wrap(+num.replace(/,/g, ''))
      .should('be.gt', lower_bound)
      .and('be.lt', upper_bound)
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文