javascript - Unable to Type in "number" input field in Chrome 93 Cypress 8.4.1 - Stack Overflow

I am using chrome version 96 and cypress version 8.4.1. Whenever I try to type in an input field with t

I am using chrome version 96 and cypress version 8.4.1. Whenever I try to type in an input field with type="number" cypress immediately fails with the error:

InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection.

HTML Code: <input type="number" name="phone_lead" id="phone_lead" placeholder="+92 301 2345678" class="required" autoplete="off">

-Input Phone number:

cy.get('#phone_lead').click({force:true}).type('16777')

Any solution/suggestion how to resolve this issue?

I am using chrome version 96 and cypress version 8.4.1. Whenever I try to type in an input field with type="number" cypress immediately fails with the error:

InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection.

HTML Code: <input type="number" name="phone_lead" id="phone_lead" placeholder="+92 301 2345678" class="required" autoplete="off">

-Input Phone number:

cy.get('#phone_lead').click({force:true}).type('16777')

Any solution/suggestion how to resolve this issue?

Share Improve this question edited May 19, 2023 at 7:37 H.Bouvy 1526 bronze badges asked Sep 27, 2021 at 8:55 RITBRITB 131 silver badge6 bronze badges 2
  • 1 Can you add your html to the question. – Alapan Das Commented Sep 27, 2021 at 9:57
  • <input type="number" name="phone_lead" id="phone_lead" placeholder="+92 301 2345678" class="required" autoplete="off"> – RITB Commented Sep 27, 2021 at 12:08
Add a ment  | 

4 Answers 4

Reset to default 4

The problem isn't reproducible, either with Chrome 93 or 96 (you mention both versions). The test passes when using that HTML in isolation.

Technically that error es because of the type="number", from HTMLInputElement.setSelectionRange()

Note that according to the WHATWG forms spec selectionStart, selectionEnd properties and setSelectionRange method apply only to inputs of types text, search, URL, tel and password. Chrome, starting from version 33, throws an exception while accessing those properties and method on the rest of input types. For example, on input of type number: "Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection".

But Cypress has internal checks that avoid selectionStart for inputs of type number,

const canSetSelectionRangeElementRe = /^(text|search|URL|tel|password)$/

Since you are dealing with a phone number, the input type should (theoretically) be changed to type="tel".

<input type="tel" 
  name="phone_lead" id="phone_lead" 
  placeholder="+92 301 2345678" 
  class="required" 
  autoplete="off">

In my React project (other libs might experiment the same issue, or not), the only way I got input[type="number"] fields value writing with Cypress has been doing it like this:

cy.get('input').type('{selectall}').type('123');

It first does a full selection of the extisting value, and later it types the value you want.

Error message says what you are doing wrong. You shouldn't use click function. It is not a button and it is not a dropdown. You need just to type in it.

Try without click:

cy.get('#phone_lead').type('16777')

Also try to use data-cy instead of #phone_lead

further reading https://docs.cypress.io/guides/references/best-practices

Try this

cy.get('input[name="phone_lead"]').type('12345678')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745184744a4615583.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信