Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to click on a table row using capybara & rspec

I am writitng request spec for my rails app with capybara. In my code I have something like:

%table
  %tbody
     %tr{"on_click" => "location.href='some_link'"}
       %td="Some attribute"
       %td="Some attribute"
       %td="Some attribute"
       %td="Some attribute"

This way I make the entire row clickable. I want to write a request spec with capybara for this feature but I don't know how. Can anyone help me on this ?

Thank you

like image 872
u19964 Avatar asked May 25 '12 18:05

u19964


2 Answers

May be you should first get to know the testing in rails. Check this out! http://railscasts.com/episodes/275-how-i-test it is really helpful. You can give your tr a class (say) .tr and do

page.find(:css, ".tr").click()

I hope this works, worked in my case!

like image 188
Sadiksha Gautam Avatar answered Oct 12 '22 22:10

Sadiksha Gautam


I found this works without requiring a class:

page.find(:xpath, "//table/tbody/tr").click

I believe your row-click requires JavaScript, so you'll need to add :js => true to your test's header. Setting up testing with JavaScript is a challenge. I found these resources helpful:

  • ASCIIcast #257 Request Specs and Capybara - see info on database-cleaner
  • Capybara readme - see “Transactions and Database Setup"

Here is a more complete test example:

# Note that opening page by clicking on row requires JavaScript
describe "when user clicks on first row", :js => true do

  let(:first_account_listed) { Account.order(:name).first }

  before { page.find(:xpath, "//table/tbody/tr").click }

  it { should have_selector('title', text: 'Account Details') }

end
like image 39
Mark Berry Avatar answered Oct 12 '22 23:10

Mark Berry