“Property ‘value’ does not exist on type ‘JQuery<HTMLElement>’ when asserting using Cypress.”

huangapple go评论68阅读模式
英文:

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((response[0]).value).to.be.equal(10)

英文:

I'm trying to assert the value of an input field:

cy.get(&#39;[data-vv-name=&quot;item price&quot;]&#39;)
    .then(response =&gt; {
        expect(response.value).to.be.equal(&#39;100&#39;)
    })

But I'm getting an error:

Property &#39;value&#39; does not exist on type &#39;JQuery&lt;HTMLElement&gt;&#39;

But when I try the code to the console:

$(&#39;[data-vv-name=&quot;item price&quot;]&#39;).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((&lt;HTMLInputElement&gt;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(&#39;[data-vv-name=&quot;item price&quot;]&#39;)
  .then($el =&gt; {
    // $el is convention for naming jQuery objects
    expect($el.val()).to.be.equal(&#39;100&#39;)
  })

or use .invoke() in the chain (ref invoke)

cy.get(&#39;[data-vv-name=&quot;item price&quot;]&#39;)
  .invoke(&#39;val&#39;)
  .then(value =&gt; {
    expect(value).to.be.equal(&#39;100&#39;)
  })
  .should(&#39;eq&#39;, &#39;100&#39;)    // alternative to above assertion

or you can use the .should(&#39;have.value&#39;, ...) chainer (ref should)

cy.get(&#39;[data-vv-name=&quot;item price&quot;]&#39;)
  .should(&#39;have.value&#39;, &#39;100&#39;)

or use the .value property of the unwrapped element as you have shown

huangapple
  • 本文由 发表于 2023年6月29日 18:01:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定