无法断言柏树中的值

  • 本文关键字:断言 cypress assertion
  • 更新时间 :
  • 英文 :


我有一个输入字段,我在其中键入值1000,当我尝试断言该值时,断言失败:https://i.stack.imgur.com/4WVhK.png

我的代码cy.get('#insurance_cover_money').type(1000).should('have.value', '1 000')

HTML: https://i.stack.imgur.com/0KS7b.png

谢谢!

你可以试试这个

cy.get('#insurance_cover_money').type('1000').should('have.value', '1000')

如果上面的仍然不起作用,你可以做两件事:

  1. 记录值,然后复制粘贴到should断言中。
cy.get('#insurance_cover_money')
.type('1000')
.invoke('val')
.then((val) => {
cy.log(val)
})
  1. 提取第一个get中的值,然后将相同的值传递给should断言
cy.get('#insurance_cover_money')
.type('1000')
.should('not.have.value', '0')
.invoke('val')
.then((val) => {
cy.get('#insurance_cover_money').should('have.value', val)
})

你也可以尝试增加超时时间:

cy.get('#insurance_cover_money', {timeout: 8000})
.type('1000')
.should('have.value', '1000')
cy.get('#insurance_cover_money', {timeout: 8000})
.type('1000')
.should('have.value', '1 000')

如果你想删除空格和断言,你可以这样做:

cy.get('#insurance_cover_money')
.type('1000')
.invoke('val')
.then((val) => val.replace(/s/g, ''))
.should('eq', '1000')

所以我需要做的就是删除字符串中的空格,然后做断言:

cy.get('#insurance_cover_money').type(1000).invoke('val').then(value => {
const string = value.replace(/s/g, '') // removes the spaces inside a string
expect(string).to.equal('1000')
})

最新更新