0

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:

cy.get(element).type('{esc}');

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

cy.get('body').type('{esc}');

which does not work.

Update:

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.
0

Found an answer:

  cy.get('body').trigger('keydown', { keyCode: 27});
  cy.wait(500);
  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.

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.