How to verify number of records using capybara
I am using 开发者_开发知识库capybara along with cucumber on a Rails 2.3.9 project.
I have users index page and I have two records there. Using capybara how do I assert that there are only two records in the page.
HTML structure is like this
<div class='records'>
<li>record 1<li>
<li>record 2 </li>
</div>
This should do the trick for your Cucumber step definition:
page.has_css?("div.records li", :count => 2)
There's also page.has_xpath?
(but I don't understand xpath)
If you're using Rspec you can phrase it the Rspec way with:
page.should have_css("div.records li", :count => 2)
I had to solve a very similar problem just yesterday; here's the full step definition I ended up with.
Then /^I should see only (\d+) tasks$/ do |number_of_tasks|
page.should have_css("table tr.task", :count => number_of_tasks.to_i)
end
Updated code would look like:
page.should have_css('div.records li', count: 2)
and then you can check:
within('div.records li') do
expect(page).to have_text('record 1')
expect(page).to have_text('record 2')
end
精彩评论