How to Press Tabs in Cypress

How to Press Tabs in Cypress

Ferenc Almasi β€’ 2021 October 15 β€’ Read time 2 min read
  • twitter
  • facebook

Currently, there is no official support for trigger the tab key on your keyboard in Cypress. However, as the official documentation suggests, we can us the cypress-plugin-tab package. After running:

npm install -D cypress-plugin-tab

You need to include the package in your support file (by default, located at cypress/support/index.js):

Copied to clipboard!
require('cypress-plugin-tab');

Then in your test cases, you can use it by calling the tab command by chaining off from cy.get, or using on the currently focused element:

Copied to clipboard!
cy.get('body').tab(); // Tab into the first tabbable element on the page
cy.get('input').type('1').tab().type('2'); // type 1, then press tab, then type 2
cy.focused().tab(); // Tab into the currently focused element

You can also keep calling tab to keep tabbing into further elements. Apart from the tab, a couple of special keys are also supported in Cypress out of the box. Some of the most commonly used ones can be:

Copied to clipboard!
cy.get('input').type('{enter}');     // Hit the enter
cy.get('input').type('{backspace}'); // Hit the backspace
cy.get('input').type('{del}');       // Hit the delete key
cy.get('input').type('{selectall}'); // Select everything in the input
See resources below for the full list of supported special sequences

Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:

Learn Cypress with Educative
How to type tab keys in Cypress
If you would like to see more webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.