Ruby-on-rails 有没有办法使用 Capybara 将按键发送到 Webkit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8474103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to send key presses to Webkit using Capybara?
提问by pupeno
I need to send some key-presses to a web app in an integration test that uses Capybara and WebKit. Using Selenium (WebDriver and Firefox) I can achieve it like this:
我需要在使用 Capybara 和 WebKit 的集成测试中向 Web 应用程序发送一些按键。使用 Selenium(WebDriver 和 Firefox)我可以这样实现:
find("#element_id").native.send_keys :tab
but WebKit's native element node doesn't have a send_keys method. Actually native in WebKit returned a string containing a number. Is there another way to send keystrokes to WebKit? Maybe even some workaround using JavaScript/jQuery?
但是 WebKit 的原生元素节点没有 send_keys 方法。实际上,WebKit 中的原生返回了一个包含数字的字符串。是否有另一种方式将击键发送到 WebKit?甚至可能是使用 JavaScript/jQuery 的一些解决方法?
回答by d_rail
I've been trying to implement Marc's answer without any success, but I found some help from a similar question: capybara: fill in form field value with terminating enter key. And apparently there was a pull requestfrom capybara that seems to address this issue.
我一直在尝试实施 Marc 的答案,但没有成功,但我从一个类似的问题中找到了一些帮助:capybara: fill in form field value with terminating enter key。显然有一个来自水豚的拉取请求似乎解决了这个问题。
What worked for me was:
对我有用的是:
before { fill_in "some_field_id", with: "\t" }
My example erases the text in the field and then presses Tab. To fill in a field with 'foobar', replace "\t"with "foobar\t". You can also use "\n"for the Enterkey.
我的示例擦除字段中的文本,然后按Tab。要使用 填充字段'foobar',请替换"\t"为"foobar\t"。您也可以"\n"用于Enter密钥。
For your example, you could use:
对于您的示例,您可以使用:
find("#element_id").set("\t")
回答by Marc Lainez
You can do it like that:
你可以这样做:
keypress_script = "var e = $.Event('keydown', { keyCode: #{keycode} }); $('body').trigger(e);"
page.driver.browser.execute_script(keypress_script)
回答by Henrik N
This worked for me with Poltergeist, to trigger the asterisk key:
这对我与 Poltergeist 一起工作,以触发星号键:
find("body").native.send_key("*")
I had no luck with the other solutions; not even Syn.
我对其他解决方案不走运;甚至没有同步。
This was to trigger an angular-hotkeysevent.
这是为了触发angular-hotkeys事件。
回答by Osmond
Now since Capybara-webkit 1.9.0 you can send key presses like enter and others using send_keys:
现在,从 Capybara-webkit 1.9.0 开始,您可以使用 send_keys 发送像 Enter 和其他按键这样的按键:
find("textarea#comment").send_keys(:enter)
Source: https://github.com/thoughtbot/capybara-webkit/issues/191#issuecomment-228758761
来源:https: //github.com/thoughtbot/capybara-webkit/issues/191#issuecomment-228758761
Capybara API Docs: http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FElement%3Asend_keys
Capybara API 文档:http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FElement%3Asend_keys
回答by Sarah Vessels
I ended up doing the following:
我最终做了以下事情:
Capybara.current_driver = Capybara.javascript_driver
keypress_script = "$('input#my_field').val('some string').keydown();"
page.driver.browser.execute_script(keypress_script)
I discovered in Chrome, testing my JavaScript, that actually creating an $.Eventwith keyCodeor charCodeand then triggering that on my input field didn't put the characters in the input. I was testing autocompletion which required a few characters be in the input field, and it would start the autocompletion on keydown. So I set the input value manually with val, then trigger keydownto cause the autocompletion script to start.
我在 Chrome 中发现,测试我的 JavaScript,实际上创建了一个$.EventwithkeyCode或charCode然后在我的输入字段上触发它并没有将字符放入输入中。我正在测试自动完成,它需要在输入字段中输入几个字符,它会在keydown. 因此,我使用 手动设置输入值val,然后触发keydown以启动自动完成脚本。
回答by Marc-André Lafortune
For simple cases, triggering a keypressevent in JS will work:
对于简单的情况,keypress在 JS 中触发一个事件会起作用:
def press(code)
page.execute_script("$('#my-input').trigger($.Event('keypress', {keyCode: #{code}}))")
end
For a more general and robust answer, use this great librarythat goes through the trouble of triggering the right events (i.e. keydown, then keypressand finally keyup).
对于更通用和更可靠的答案,请使用这个伟大的库,它经历了触发正确事件的麻烦(即keydown, thenkeypress和 finally keyup)。
def type(string)
page.execute_script("Syn.click({}, 'my-input').wait().type(#{string.to_json})")
end
A more complex example can be found here
一个更复杂的例子可以在这里找到
回答by Greg Blass
For Capybara Webkit, this is the solution I used:
对于 Capybara Webkit,这是我使用的解决方案:
def press_enter(input)
script = "var e = jQuery.Event('keypress');"
script += "e.which = 13;"
script += "$('#{input}').trigger(e);"
page.execute_script(script);
end
Then I use it cleanly in my test like:
然后我在我的测试中干净地使用它,例如:
press_enter("textarea#comment")
回答by ExiRe
Here is my solution, which works with capybara 2.1.0:
这是我的解决方案,适用于水豚2.1.0:
fill_in('token-input-machine_tag_list', :with => 'new tag name')
page.evaluate_script("var e = $.Event('keydown', { keyCode: 13 }); $('#token-input-machine_tag_list').trigger(e);") # Press enter
Please, note, that in new capybara you have to use page.evaluate_script.
请注意,在新的水豚中,您必须使用page.evaluate_script.

