Glend Maatita
Glend Maatita

Reputation: 633

How to simulate pressing Enter in Rspec

I have a test with RSpec like 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 button 'search_all' and change it with event like pressing enter by keyboard.

How do I write code for that with RSpec ?

Regards,

Upvotes: 11

Views: 4516

Answers (2)

eMgz
eMgz

Reputation: 708

Just add a '\n' to the end of your query string:

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

Upvotes: 13

siekfried
siekfried

Reputation: 2964

You can do it using capybara-webkit, which is a Capybara driver allowing you to test 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.

Upvotes: 5

Related Questions