0

Gretting all.In TestCafe,there are pressKey()anddispatchevent()to use on press keyboard .I'm now struggling on one thing:I can't press the Numpad by neither pressKey() or dispatchevent().Here's what I've tried:

.dispatchEvent(#textbox,'keydown',{code:'numpad1'})

This actually fired,but it didn't type in the text 1 by the numpad1.Same thing on Digit1.Want to know if TestCafe support the different pressKey by Numpad and Digit?Thanks for reply.

Edit: I tried to use dispatchevent() Here to fire Digit1 and numpad1,it works good,but if i tried on www.google.com,it fails.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
cPtz
  • 1
  • 2

1 Answers1

0

Typing text via dispatching keyboard events is not allowed due to security reasons. See: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Auto-repeat_handling_prior_to_Gecko_5.0. That's why the 'keydown' event is raised, but no value appears in the text box.

To simulate an input process with required key events, you can use the approach described here: https://stackoverflow.com/a/69917022/10684943.

Dmitry Ostashev
  • 2,305
  • 1
  • 5
  • 21
  • Thanks for reply!The apptoach you provided may not fit my request since it is use on develop side,but mine is on e2e-test side,on ```document```,the compiler gives error. – cPtz Jan 18 '23 at 02:42
  • I gave that link just for illustration. You can simulate a set of events (keydown, keypress, input, keyup) using the dispatchEvent method or the [ClientFunction](https://testcafe.io/documentation/402832/guides/basic-guides/client-functions). As you can see in that answer, the value of the textBox should be changed manually. – Dmitry Ostashev Jan 18 '23 at 08:12