Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of rows in the tables. (RSpec, Capybara)

I have this code:

<table class="table" id="charges_failed">
<tr>
  <th>Customer</th>
  <th>Charge Amount</th>
  <th>Date Failed</th>
  <th colspan="3"></th>
</tr>
<% @charges_failed.each do |charge| %>
  <tr bgcolor="#FF0000">
    <td><%= charge.formatted_customer_name %></td>
    <td><%= number_to_currency(charge.amount.to_s) %></td>
    <td><%= charge.created_at.strftime("%d/%m/%Y") %></td>
  </tr>
<% end %>

And I have this test:

it "check failed charges" do
  visit root_path
  expect(page).to have_selector('table#charges_failed tr', :count => 6)
end

Why he finds only the first row in the table. Total on page 6 of them. (error: expected to find css "table#charges_failed tr" 6 times, found 1 match: "Customer Charge Amount Date Failed")

like image 335
Vladimir Fomin Avatar asked Oct 16 '14 05:10

Vladimir Fomin


1 Answers

I find the easiest method is to use xpaths.

Would be something like:

within('table#charges_failed') do
    expect(page).to have_xpath(".//tr", :count => 6)
end

You could probably also drop the 'within' and define a full xpath like: ".//table[@id='charges_failed']//tr"

As far as I know have_selector just checks for the presence of something (css, xpath etc). I've never seen a count used with it before.

like image 199
Allan W Smith Avatar answered Oct 15 '22 13:10

Allan W Smith