Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 411
Glend Maatita Avatar asked Jul 09 '13 07:07

Glend Maatita


2 Answers

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

fill_in "keyword", with: "London\n"
like image 178
eMgz Avatar answered Oct 17 '22 16:10

eMgz


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.

like image 5
siekfried Avatar answered Oct 17 '22 15:10

siekfried