有没有办法使用 Capybara 将按键发送到 Webkit?

发布于 2024-12-20 13:21:09 字数 290 浏览 4 评论 0原文

我需要在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吃不饱 2024-12-27 13:21:09

我一直在尝试实现马克的答案,但没有成功,但我从类似的问题中找到了一些帮助: 水豚:用终止输入键填写表单字段值。显然有一个 来自水豚的拉取请求似乎解决了这个问题。

对我有用的是:

before { fill_in "some_field_id", with: "\t" }

我的示例删除了字段中的文本,然后按 Tab。要使用 'foobar' 填充字段,请将 "\t" 替换为 "foobar\t"。您还可以使用 "\n" 作为 Enter 键。

对于您的示例,您可以使用:

find("#element_id").set("\t")

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:

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 Enter key.

For your example, you could use:

find("#element_id").set("\t")
手心的海 2024-12-27 13:21:09

这对我来说对 Poltergeist 有用,触发星号键:

find("body").native.send_key("*")

我对其他解决方案没有运气;甚至没有同步。

这是为了触发 angular-hotkeys 事件。

This worked for me with Poltergeist, to trigger the asterisk key:

find("body").native.send_key("*")

I had no luck with the other solutions; not even Syn.

This was to trigger an angular-hotkeys event.

長街聽風 2024-12-27 13:21:09

你可以这样做:

keypress_script = "var e = $.Event('keydown', { keyCode: #{keycode} }); $('body').trigger(e);"
page.driver.browser.execute_script(keypress_script)

You can do it like that:

keypress_script = "var e = $.Event('keydown', { keyCode: #{keycode} }); $('body').trigger(e);"
page.driver.browser.execute_script(keypress_script)
青巷忧颜 2024-12-27 13:21:09

现在,从 Capybara-webkit 1.9.0 开始,您可以使用 send_keys 发送 Enter 等按键:

find("textarea#comment").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:

find("textarea#comment").send_keys(:enter)

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

盛夏已如深秋| 2024-12-27 13:21:09

我最终做了以下事情:

Capybara.current_driver = Capybara.javascript_driver
keypress_script = "$('input#my_field').val('some string').keydown();"
page.driver.browser.execute_script(keypress_script)

我发现在 Chrome 中测试我的 JavaScript,实际上使用 keyCodecharCode 创建一个 $.Event ,然后在我的输入字段上触发它并没有将字符放入输入中。我正在测试自动完成功能,它需要在输入字段中输入一些字符,并且它将在 keydown 上启动自动完成功能。因此,我使用 val 手动设置输入值,然后触发 keydown 来启动自动完成脚本。

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 $.Event with keyCode or charCode 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 on keydown. So I set the input value manually with val, then trigger keydown to cause the autocompletion script to start.

伪装你 2024-12-27 13:21:09

对于简单的情况,在 JS 中触发 keypress 事件将起作用:

def press(code)
  page.execute_script("$('#my-input').trigger($.Event('keypress', {keyCode: #{code}}))")
end

要获得更通用和可靠的答案,请使用此 伟大的库,它经历了触发正确事件的麻烦(即keydown,然后keypress,最后keyup )。

def type(string)
  page.execute_script("Syn.click({}, 'my-input').wait().type(#{string.to_json})")
end

可以在此处找到更复杂的示例

For simple cases, triggering a keypress event in JS will work:

def press(code)
  page.execute_script("$('#my-input').trigger($.Event('keypress', {keyCode: #{code}}))")
end

For a more general and robust answer, use this great library that goes through the trouble of triggering the right events (i.e. keydown, then keypress and 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

别在捏我脸啦 2024-12-27 13:21:09

这是我的解决方案,适用于水豚 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

请注意,在新的水豚中,您必须使用 page.evaluate_script

Here is my solution, which works with capybara 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.

骄兵必败 2024-12-27 13:21:09

对于 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

然后我在测试中干净地使用它,例如:

press_enter("textarea#comment")

For Capybara Webkit, this is the solution I used:

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文