How to simulate pressing Enter in Rspec

I have a test with RSpec as below

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 search_all button and change it with an event, for example, by pressing the enter key.

How to write code for this with RSpec?

Hi,

+6
ruby-on-rails rspec rspec-rails capybara
source share
2 answers

You can do this with capybara-webkit , which is the Capybara driver allowing you to test Javascript. Just read the document 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.

+4
source share

Just add '\ n' to the end of the query line:

 fill_in "keyword", with: "London\n" 
+6
source share

All Articles