Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara: fill in form field value with terminating enter key

Tags:

capybara

I'm testing a barcode reader input... it behaves like the keyboard but terminates each barcode with the enter key character. My javascript detects the enter key and responds (backbone.js application).

How can I "fill in" a form field with a string that has a terminating enter key value?

My test stack is cucumber/capybara/capybara-webkit.

like image 461
Les Nightingill Avatar asked Oct 23 '11 13:10

Les Nightingill


2 Answers

Actually, it seems that you can just send a \n to capybara's native set method and achieve the same effect (in a more flexible, driver agnostic way).

So in my code, this is currently working to trigger a form submit event (handled by JS):

  field = find("form input[type=text]")
  field.set "my comment\n"

(Note that, as the author of the pull request explains here, this only works if you're binding to the specific keydown event, not if you're binding to the form submit that should result from it.)

like image 102
Lou Kosak Avatar answered Oct 04 '22 23:10

Lou Kosak


Consider using Capybara::Driver::Selenium as your page driver for that particular scenario. If you do then you can do things like this:

place = page.find_by_id('tinymce').native
place.send_keys("I rule!")
place.send_key "\xEE\x80\x83"
like image 21
socjopata Avatar answered Oct 05 '22 00:10

socjopata