Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: type tab key

I want to test that my forms are accessible and that I can tab between my input elements. I found this github issue asking for the feature here: https://github.com/cypress-io/cypress/issues/299

Currently I try to do .type('{tab}') and I get the following error:

CypressError: {tab} isn't a supported character sequence. You'll want to use the command cy.tab(), which is not ready yet, but when it is done that's what you'll use.

Is there a current workaround for the lack of tab support?

like image 523
bkucera Avatar asked Mar 05 '19 18:03

bkucera


People also ask

How do you handle a tab in Cypress?

Cypress does not have a specific command to work with tabs. It has a workaround method in jQuery through which it handles the tabs. In the html code, a link or button opens to a new tab, because of the attribute target. If the target attribute has value blank, it opens to a new tab.

How do you enter a key in Cypress?

Cypress automatically matches the spec and browser behavior for pressing the {enter} key when the input belongs to a <form> . This behavior is defined here: Form Implicit Submission. For instance the following will submit the form.

How do you press Enter after type in Cypress?

As you can see in the first test ( searches by typing and pressing ENTER ), when I call the type() method, in addition to passing the term I want to search for ( cypress.io ), I pass the text enter wrapped in curly braces ( {enter} ). This way, Cypress will type the text and simulate the ENTER key is pressed.


1 Answers

The Cypress team is currently working on implementing tab support along with other keyboard keys as part of Native Events

In the meantime I've made a plugin that adds a .tab() command. cypress-plugin-tab:

This enables you to do:

cy.get('input').tab()
// and
cy.get('input').tab({shift: true})

However, the actual tab implementation will not be a separate command, so know if you use this plugin, you'll have to refactor your test code when Native Events lands

As the cypress documentation says:

In the meantime, you can use the experimental cypress-plugin-tab and can thumbs up this issue.

like image 87
bkucera Avatar answered Sep 21 '22 19:09

bkucera