question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.get('button').type() only works if button has `tabindex` specified

See original GitHub issue

Current behavior:

When calling cy.type() on a <button> element, Cypress throws:

CypressError: cy.type() failed because it requires a valid typeable element.

However if the <button> has a tabindex attribute of any value specified, then calling cy.type() will succeed.

Desired behavior:

cy.type() should succeed in any case, since <buttons> are naturally focusable.

Steps to reproduce:

Call cy.type() on <button> (will throw). Call cy.type() on <button tabindex="-1"> (will succeed). Call cy.type() on <button tabindex="0"> (will succeed). Call cy.type() on <button tabindex="1"> (will succeed).

Versions

Cypress 3.0.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
jennifer-shehanecommented, Jul 18, 2018

Hi @kyleoliveiro, the error should have also given you information about what we consider typeable including:

Cypress considers the 'body', 'textarea', any 'element' with a 'tabindex' or 'contenteditable' attribute, or any 'input' with a 'type' attribute of 'text', 'password', 'email', 'number', 'date', 'week', 'month', 'time', 'datetime', 'datetime-local', 'search', 'url', or 'tel' to be valid typeable elements.

If you want to simulate a specific event on the button, I’d suggest using trigger to trigger that event like:

cy.get('button').trigger('keypress', {
      keyCode: 35,  // whatever code for the key you wanted to press
      which: 35 // whatever which for the key you wanted to press
}) 

Let me know if this works for you.

1reaction
kucebcommented, Aug 30, 2019

Re-opened this since we want to allow typing into any element that is focusable. This is fixed in #4870

Read more comments on GitHub >

github_iconTop Results From Across the Web

type - Cypress Documentation
Type into a DOM element. It is unsafe to chain further commands that rely on the subject after .type(). Syntax Usage Correct Usage...
Read more >
Cypress - Select button that contains text - Stack Overflow
I solved it using cy.contains('button', 'Edit Claim').should('be.disabled');.
Read more >
cy.type() failed because it targeted a disabled element
type() failed because it requires a valid typeable element. However if the <button> has a tabindex attribute of any value specified, then calling ......
Read more >
Cypress Cheat Sheet - LinkedIn
get is like findElement, identifies the element in the web page based on css ... cy.get('#alert').click() //which opens only ok button alert.
Read more >
Working with Dynamic Element Ids, Classes and Attributes
We run cy.get(“#someBox”) to get an element which has an ID of ... .type() or .then(()=>) on the custom command in the test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found