7

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.

Les Nightingill
  • 5,662
  • 1
  • 29
  • 32

2 Answers2

8

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

Lou Kosak
  • 289
  • 6
  • 10
  • Not quite driver agnostic - depends how the driver implements form entry. For example, poltergeist only called a change event not too long ago, rather than keydown and the lot. ([See this ticket](https://github.com/jonleighton/poltergeist/issues/43)) For me, this worked in selenium and not poltergeist, I am checking keypress events for `e.keycode == 13` – willscripted Sep 01 '12 at 22:00
  • This didn't work for me in Poltergeist either. Debugging it, it looks like the dispatched keypress events always have `e.which == 0`, which is apparently due to a bug in WebKit that's been around forever and gets almost no attention https://bugs.webkit.org/show_bug.cgi?id=16735 – Louis Simoneau May 06 '14 at 03:55
4

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"
Jon Wingfield
  • 1,875
  • 2
  • 11
  • 8
socjopata
  • 5,028
  • 3
  • 30
  • 33
  • Thank you for your suggestion. I had already done this successfully in Selenium, although not as concisely as your suggestion. But I was trying to speed up my tests with capybara-webkit. – Les Nightingill Oct 26 '11 at 17:14
  • 1
    You can manage your scenarios and drivers using tags. For example you can configure your system, so any scenario tagged @selenium is being run using Selenium driver. This way you can get speed of webkit driver in most of your scenarios and use selenium for problematic ones. Good luck :) – socjopata Oct 26 '11 at 18:49