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)
2
Answers
An
HTMLElement
could be an<input>
which has avalue
, but it could be a<div>
which does not.You need to narrow the type. You can do that by testing for the existence of the
value
property:jQuery objects don’t have
.value
property, but they do have.val()
method that you could useor use
.invoke()
in the chain (ref invoke)or you can use the
.should('have.value', ...)
chainer (ref should)or use the
.value
property of the unwrapped element as you have shown