capybara: fill in form field value with terminating enter key

橙三吉。 提交于 2020-01-02 03:31:07

问题


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.


回答1:


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




回答2:


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"


来源:https://stackoverflow.com/questions/7866469/capybara-fill-in-form-field-value-with-terminating-enter-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!