Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to send key presses to Webkit using Capybara?

I need to send some key-presses to a web app in an integration test that uses Capybara and WebKit. Using Selenium (WebDriver and Firefox) I can achieve it like this:

find("#element_id").native.send_keys :tab 

but WebKit's native element node doesn't have a send_keys method. Actually native in WebKit returned a string containing a number. Is there another way to send keystrokes to WebKit? Maybe even some workaround using JavaScript/jQuery?

like image 379
pupeno Avatar asked Dec 12 '11 12:12

pupeno


2 Answers

I've been trying to implement Marc's answer without any success, but I found some help from a similar question: capybara: fill in form field value with terminating enter key. And apparently there was a pull request from capybara that seems to address this issue.

What worked for me was:

before { fill_in "some_field_id", with: "\t" } 

My example erases the text in the field and then presses Tab. To fill in a field with 'foobar', replace "\t" with "foobar\t". You can also use "\n" for the Enter key.

For your example, you could use:

find("#element_id").set("\t") 
like image 108
d_rail Avatar answered Sep 28 '22 02:09

d_rail


This worked for me with Poltergeist, to trigger the asterisk key:

find("body").native.send_key("*") 

I had no luck with the other solutions; not even Syn.

This was to trigger an angular-hotkeys event.

like image 24
Henrik N Avatar answered Sep 28 '22 01:09

Henrik N