英文:
Property 'value' does not exist on type 'JQuery<HTMLElement>' when asserting using Cypress
问题
I'm trying to assert the value of an input field:
cy.get('[data-vv-name="item price"]')
.then(response => {
expect(response.value).to.be.equal('100')
})
But I'm getting an error:
Property 'value' does not exist on type 'JQuery
But when I try the code to the console:
$('[data-vv-name="item price"]').value
I'm able to get the correct value of '100'.
What should I add/update to make my assertion correct? Am I missing anything here?
EDIT:
Using the following, I was able to do my assertion
expect((
英文:
I'm trying to assert the value of an input field:
cy.get('[data-vv-name="item price"]')
.then(response => {
expect(response.value).to.be.equal('100')
})
But I'm getting an error:
Property 'value' does not exist on type 'JQuery<HTMLElement>'
But when I try the code to the console:
$('[data-vv-name="item price"]').value
I'm able to get the correct value of '100'.
What should I add/update to make my assertion correct? Am I missing anything here?
EDIT:
Using the following, I was able to do my assertion
expect((<HTMLInputElement>response[0]).value).to.be.equal(10)
答案1
得分: 4
jQuery对象没有.value
属性,但它们有.val()
方法,您可以使用它
cy.get('[data-vv-name="item price"]')
.then($el => {
// $el是约定命名jQuery对象
expect($el.val()).to.be.equal('100')
})
或者在链式操作中使用.invoke()
(参考invoke)
cy.get('[data-vv-name="item price"]')
.invoke('val')
.then(value => {
expect(value).to.be.equal('100')
})
.should('eq', '100') // 与上面的断言相同的替代方式
或者您可以使用.should('have.value', ...)
连接器(参考should)
cy.get('[data-vv-name="item price"]')
.should('have.value', '100')
或者像您所示,使用未包装的元素的.value
属性。
英文:
jQuery objects don't have .value
property, but they do have .val()
method that you could use
cy.get('[data-vv-name="item price"]')
.then($el => {
// $el is convention for naming jQuery objects
expect($el.val()).to.be.equal('100')
})
or use .invoke()
in the chain (ref invoke)
cy.get('[data-vv-name="item price"]')
.invoke('val')
.then(value => {
expect(value).to.be.equal('100')
})
.should('eq', '100') // alternative to above assertion
or you can use the .should('have.value', ...)
chainer (ref should)
cy.get('[data-vv-name="item price"]')
.should('have.value', '100')
or use the .value
property of the unwrapped element as you have shown
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论