send_keys support for Poltergeist?
Asked Answered
F

4

17

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

Flyspeck answered 5/3, 2013 at 19:41 Comment(2)
Please, select the answer by @Matt Sanders as best answer, as it most accurately addresses your question as of today.Wimberly
Years later ... thanks to @Matt SandersFlyspeck
V
18

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
Vachell answered 13/2, 2014 at 22:42 Comment(0)
F
3

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.

Freakish answered 11/6, 2013 at 13:26 Comment(4)
could you post a link to the page in the documentation where you found this? If you just wanted to press one key what would the syntax be?Bibliographer
github.com/ariya/phantomjs/wiki/… - but note that this doesn't solve the problem by itself since although poltergeist happens not to pass that API through. You can probably hack around it either in poltergeist itself or by abusing some other exposed internals, but it's not a plug-n-play solution.Freakish
this solution seems ideal for testing github.com/jeresig/jquery.hotkeys. can you clarify how to pass sendEvent through rspec and poltergeist to phantomjs?Exterior
as John Leighton pointed out: I'm not sure this is possible without changing poltergeist. Poltergeist leaks some info into the JS containers it uses, so maybe you can hack around it, but the cleanest way would be to update poltergeist to explicitly support this (or or to support sending arbitrary commands to phantomjs).Freakish
F
2

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.

Flann answered 5/3, 2013 at 22:26 Comment(3)
This is not true; phantomjs does support sending keyboard, mouse and various other events to the browser (not just the more limited triggering available from within the browser sandbox): github.com/ariya/phantomjs/wiki/…Freakish
Yes, PhantomJS supports it (as I already stated), but the question was about Poltergeist. Poltergeist does not have a hook into this part of the PhantomJS API.Flann
Indeed - sorry! Would a client-side hack (ab)using __poltergeist perhaps be possible?Freakish
V
2

Starting from version 1.5.0, poltergeist supports basic send_keys.

https://github.com/jonleighton/poltergeist/blob/master/CHANGELOG.md#150

Vouchsafe answered 24/12, 2013 at 6:14 Comment(4)
I get this error when I try send_keys: NoMethodError: undefined method 'send_keys' for "1":String My setup is Capybara.driver=:webkit and Capybara.javascript_driver=:poltergeist with Poltergeist v1.7. What am I doing wrong?Mindszenty
@sameers, I think you should submit an issue on poltergeist github page. 1.7 was just released, so I haven't tried it yetVouchsafe
will do, I had assumed it was my fault, but maybe I shd dig into poltergeist itself...Mindszenty
find('input#some').send_keys('some text') should workNighthawk

© 2022 - 2024 — McMap. All rights reserved.