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

Kevin Triplett
  • 582
  • 5
  • 11

4 Answers4

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
Matt Sanders
  • 8,023
  • 3
  • 37
  • 49
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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
  • 1
    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? – Max Rose-Collins Sep 23 '13 at 10:56
  • https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#wiki-webpage-sendEvent - 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. – Eamon Nerbonne Oct 10 '13 at 08:36
  • this solution seems ideal for testing https://github.com/jeresig/jquery.hotkeys. can you clarify how to pass `sendEvent` through rspec and poltergeist to phantomjs? – Chris Beck Apr 05 '14 at 14:40
  • 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). – Eamon Nerbonne Apr 06 '14 at 21:58
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.

jonleighton
  • 1,041
  • 8
  • 12
  • 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): https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#wiki-webpage-sendEvent – Eamon Nerbonne Jun 11 '13 at 13:22
  • 1
    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. – jonleighton Jun 11 '13 at 17:02
  • Indeed - sorry! Would a client-side hack (ab)using `__poltergeist` perhaps be possible? – Eamon Nerbonne Jun 11 '13 at 20:48
2

Starting from version 1.5.0, poltergeist supports basic send_keys.

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

Valentin V
  • 24,971
  • 33
  • 103
  • 152
  • 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? – sameers Oct 02 '15 at 17:19
  • @sameers, I think you should submit an issue on poltergeist github page. 1.7 was just released, so I haven't tried it yet – Valentin V Oct 02 '15 at 17:28
  • will do, I had assumed it was my fault, but maybe I shd dig into poltergeist itself... – sameers Oct 03 '15 at 18:02
  • find('input#some').send_keys('some text') should work – hisa_py Nov 27 '15 at 13:55