Working on webpage test automation with Cypress atm. Ran into a problem.

  • I need to simulate a pressing of the ESCAPE key from a user.

Cypress has its own type() method, but it is required to chain off a DOM element, e.g. it has to be implemented this way:


I want to simulate a simple key press of ESCAPE without involving any DOM element. Does anyone know how to do it?

I have tried


which does not work.


I tried

cy.get('body').trigger('keydown', { keyCode: 27, which: 27 });//27 is the keycode for the escape key

Did not work either. I guess there is a mis-alignment between my expectation and Cypress specification.

  • What I want to do is a simple key press from keyboard while Cypress has its type() to type into an element, they could be two different things.

Found an answer:

  cy.get('body').trigger('keydown', { keyCode: 27});
  cy.get('body').trigger('keyup', { keyCode: 27});

The amount of time cy.wait() waits is arbitrary. It depends on the application under test; but 200 mS seems to be working for me too.


The recommended command is:


I'm using this to disclose pop-ups and where it makes sense to use ESCape as a User.

Cypress version: 8.3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.