I want to switch from Selenium to Poltergeist but I need to simulate a barcode scanner that looks like keyboard entry to the <body>
tag. I use this code with Selenium:
native.send_keys(send_key)
Is there a way with Poltergeist to send a string of keys to an arbitrary element (ie, not an input)?
Poltergeist now has send_keys support:
element = find('input#id')
# send a simple string
element.native.send_key('String')
# send a series of keystrokes
element.native.send_keys('H', 'elo', :Left, 'l') # => 'Hello'
# symbol for special keys
element.native.send_key(:Enter) # triggers Enter key
Since PhantomJS 1.7 (released 2012-09-22), you can send keyboard events to the headless browser using page.sendEvent
.
The documentation includes an example simulating shift-A:
page.sendEvent('keypress', page.event.key.A,
null, null, 0x02000000 | 0x08000000 );
How exactly that input is handled by the page (i.e. what's targeted) will depend on the state of the page, such as where the focus is.
No, there is no way to do this at present. PhantomJS does provide an API for this, so it could be added in the future, but it's not currently supported.
I'd suggest trying to generate the DOM keyboard events in Javascript. Or just keep those specs using Selenium and use Poltergeist for the rest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With