【问题标题】:What's the right way to check if an input has a value less than x in Cypress?在赛普拉斯中检查输入的值是否小于 x 的正确方法是什么?
【发布时间】:2020-06-18 18:37:31
【问题描述】:

我正在尝试检查 an 的值是否小于 x。在 Cypress 中进行测试的最佳方法是什么?

示例代码(不起作用):

cy.get('.number-input').type('200').should('have.value.lt', '201')

我知道我可以通过回调来做到这一点,但这似乎有点混乱,特别是考虑到测试输入是否 - 确切 - 东西是多么简洁:

cy.get('.number-input').type('200').should('have.value', '200')

【问题讨论】:

    标签: javascript html testing cypress


    【解决方案1】:

    Chai lt 是有效的(参见Chai.js cheatsheet),但它需要数值,而<input /> 值始终是字符串,因此您需要将其转换为数字。

    另外,Cypress .should('have.value.lt', '201') 命令是 jQuery 和 chai 运算符的组合,从错误消息来看显然是非法的(should 参数的语法有点不透明,你只需要尝试一下)。

    所以,这行得通

    cy.get('.number-input').type('200')
      .invoke('val')                         // call the val() method to extract the value
      .then(val => +val)                     // convert it to a number
      .then(val => console.log(typeof val))  // just to check the type
      .should('be.lt', 201)                  // also compare it to a number
    

    【讨论】:

    • 漂亮,谢谢!现在这一切都变得更有意义了。那么最短的方法是cy.get('.number-input').type('200').invoke('val').then(val => +val).should('be.lt', 201)
    • 正确,我只是在 console.log 中添加了显示如何检查命令链中的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-06-15
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多