有没有办法使用 Capybara 将按键发送到 Webkit?
我需要在使用 Capybara 和 WebKit 的集成测试中将一些按键发送到 Web 应用程序。使用 Selenium(WebDriver 和 Firefox)我可以这样实现:
find("#element_id").native.send_keys :tab
但是 WebKit 的本机元素节点没有 send_keys 方法。实际上 WebKit 中原生返回了一个包含数字的字符串。还有其他方法将击键发送到 WebKit 吗?甚至可能有一些使用 JavaScript/jQuery 的解决方法?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我一直在尝试实现马克的答案,但没有成功,但我从类似的问题中找到了一些帮助: 水豚:用终止输入键填写表单字段值。显然有一个 来自水豚的拉取请求似乎解决了这个问题。
对我有用的是:
我的示例删除了字段中的文本,然后按 Tab。要使用
'foobar'
填充字段,请将"\t"
替换为"foobar\t"
。您还可以使用"\n"
作为 Enter 键。对于您的示例,您可以使用:
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 request from capybara that seems to address this issue.
What worked for me was:
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 Enter key.For your example, you could use:
这对我来说对 Poltergeist 有用,触发星号键:
我对其他解决方案没有运气;甚至没有同步。
这是为了触发 angular-hotkeys 事件。
This worked for me with Poltergeist, to trigger the asterisk key:
I had no luck with the other solutions; not even Syn.
This was to trigger an angular-hotkeys event.
你可以这样做:
You can do it like that:
现在,从 Capybara-webkit 1.9.0 开始,您可以使用 send_keys 发送 Enter 等按键:
来源:https://github.com/thoughtbot/capybara-webkit/issues/191#issuecomment-228758761
Capybara API 文档:http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FElement%3Asend_keys
Now since Capybara-webkit 1.9.0 you can send key presses like enter and others using send_keys:
Source: 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
我最终做了以下事情:
我发现在 Chrome 中测试我的 JavaScript,实际上使用
keyCode
或charCode
创建一个$.Event
,然后在我的输入字段上触发它并没有将字符放入输入中。我正在测试自动完成功能,它需要在输入字段中输入一些字符,并且它将在keydown
上启动自动完成功能。因此,我使用val
手动设置输入值,然后触发keydown
来启动自动完成脚本。I ended up doing the following:
I discovered in Chrome, testing my JavaScript, that actually creating an
$.Event
withkeyCode
orcharCode
and 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 onkeydown
. So I set the input value manually withval
, then triggerkeydown
to cause the autocompletion script to start.对于简单的情况,在 JS 中触发
keypress
事件将起作用:要获得更通用和可靠的答案,请使用此 伟大的库,它经历了触发正确事件的麻烦(即
keydown
,然后keypress
,最后keyup
)。可以在此处找到更复杂的示例
For simple cases, triggering a
keypress
event in JS will work:For a more general and robust answer, use this great library that goes through the trouble of triggering the right events (i.e.
keydown
, thenkeypress
and finallykeyup
).A more complex example can be found here
这是我的解决方案,适用于水豚
2.1.0
:请注意,在新的水豚中,您必须使用
page.evaluate_script
。Here is my solution, which works with capybara
2.1.0
:Please, note, that in new capybara you have to use
page.evaluate_script
.对于 Capybara Webkit,这是我使用的解决方案:
然后我在测试中干净地使用它,例如:
For Capybara Webkit, this is the solution I used:
Then I use it cleanly in my test like: