I am newbie on cypress.
I need write a test with F3 send key to page.
I wrote this (114 = charcode key for F3):
cy.get('body').type('{114}') // (not ok)
cy.get('body').type('{{114}') // (not ok)
Pease help
The only special characters built in to Cypress are listed in the docs here. There is not a special character for {114}
.
You should be able to trigger the keydown
event with the character code needed for F3 using .trigger()
like this:
cy.get('body').trigger('keydown', { keyCode: 114, which: 114 })
Let me know if this works.
Perfect !!!
Many many thanks.
I recently ran into a similar issue and came across this thread. In my case I had to trigger a Backspace. Unfortunately @jennifer-shehane 's suggestion didn't work. I later learned that keyCode is deprecated in KeyboardEvent, but using key worked for me.
Here is my code:
cy.get('body').trigger("keydown", { key: "Backspace" });
With the current version 3.4.1, to simulate a function key press I had to do
cy.get('body').trigger('keydown', { key: "F8", code: "F8", which: 119 })
(using https://keycode.info/ as a handy way to find the relevant values for any given key).
(In my case, I needed all three: key, code, which - your milage may vary.)
For anyone else finding this thread, I had to add the full keydown
, keypress
and keyup
events as specified here: https://stackoverflow.com/a/56854185
For typing special characters built into Cypress without focus into some specific input one can do, for example, like this cy.get('body').type('{esc}', { force: true })
.
For typing special characters built into Cypress without focus into some specific input one can do, for example, like this
cy.get('body').type('{esc}', { force: true })
.
It work for me. I use cypress version 3.8.2 but have to loop to work.
for (let i = 0; i < 2; i++) {
cy.get('modal-container').type('{esc}', { force: true});
}
Most helpful comment
The only special characters built in to Cypress are listed in the docs here. There is not a special character for
{114}
.You should be able to trigger the
keydown
event with the character code needed for F3 using.trigger()
like this:Let me know if this works.