如何使用capybara& amp;点击表格行rspec的

时间:2012-05-25 18:06:29

标签: ruby-on-rails-3 html5 capybara rspec-rails

我正在使用capybara写我的rails应用程序的请求规范。在我的代码中,我有类似的东西:

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

这样我就可以整行整行。我想用capybara为这个功能写一个请求规范,但我不知道怎么做。谁可以帮我这个事 ?

谢谢

2 个答案:

答案 0 :(得分:6)

可能你应该首先了解rails中的测试。看一下这个! http://railscasts.com/episodes/275-how-i-test这真的很有帮助。您可以为tr一个班级(比方说).tr提供

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

我希望这有效,在我的情况下工作!

答案 1 :(得分:5)

我发现这可以不需要课程:

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

我相信您的行点击需要JavaScript,因此您需要将:js => true添加到测试的标题中。使用JavaScript设置测试是一项挑战。我发现这些资源很有帮助:

这是一个更完整的测试示例:

# 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