Capybara: fill in form field value with terminating enter key

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.

Best Answer

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

Related Topic