I'm trying to use Capybara to test that a list contains the correct items. For example:
<table id="rodents">
<tr><td class="rodent_id">1</td><td class="rodent_name">Hamster</td></tr>
<tr><td class="rodent_id">2</td><td class="rodent_name">Gerbil</td></tr>
</table>
This list should contain ids 1 and 2, but should not include 3.
What I'd like is something like:
ids = ? # get the contents of each row's first cell
ids.should include(1)
ids.should include(2)
ids.should_not include(3)
How might I do something like that?
I'm answering with a couple of unsatisfactory solutions I've found, but I'd love to see a better one.
Here is a slightly simplified expression:
rodent_ids = page.all('table#rodents td.rodent_id').map(&:text)
From there, you can do your comparisons.
rodent_ids.should include(1)
rodent_ids.should include(2)
rodent_ids.should_not include(3)
A bad solution:
within ('table#rodents tr:nth-child(1) td:nth-child(1)') do
page.should have_content @rodent1.id
end
within ('table#rodents tr:nth-child(2) td:nth-child(1)') do
page.should have_content @rodent1.id
end
page.should_not have_selector('table#rodents tr:nth-child(3)')
This is verbose and ugly, and doesn't really say that id 3 shouldn't be in the table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With