简体   繁体   English

如何模拟在Rspec中按Enter键

[英]How to simulate pressing Enter in Rspec

I have a test with RSpec like below 我有一个RSpec测试,如下所示

describe "visitor do search" do      
  before do
      fill_in "keyword", with: "London"
      click_button "search_all"
    end

    it "should visit search result path" do
      page.should have_selector('title', :text => "Search Result")
    end
end

I want to remove the button 'search_all' and change it with event like pressing enter by keyboard. 我想删除按钮'search_all'并将其更改为类似按键盘输入的事件。

How do I write code for that with RSpec ? 如何使用RSpec编写代码?

Regards, 问候,

只需在查询字符串的末尾添加'\\ n':

fill_in "keyword", with: "London\n"

You can do it using capybara-webkit , which is a Capybara driver allowing you to test Javascript. 你可以使用capybara-webkit ,它是一个Capybara驱动程序,允许你测试Javascript。 Simply read the doc to install it and make it work in your project, then you should be able to simulate a click on the keyboard using this piece of code: 只需阅读文档以安装它并使其在您的项目中工作,然后您应该能够使用以下代码模拟键盘上的单击:

keypress = "var e = $.Event('keydown', { keyCode: 13 }); $('body').trigger(e);"
page.driver.execute_script(keypress)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM