7
votes

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.

2

2 Answers

8
votes

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

4
votes

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"