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.