Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send_keys support for Poltergeist?

Tags:

poltergeist

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)?

like image 710
Kevin Triplett Avatar asked Mar 05 '13 19:03

Kevin Triplett


3 Answers

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
like image 81
Matt Sanders Avatar answered Nov 05 '22 00:11

Matt Sanders


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.

like image 39
Eamon Nerbonne Avatar answered Nov 04 '22 23:11

Eamon Nerbonne


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.

like image 2
jonleighton Avatar answered Nov 04 '22 23:11

jonleighton