Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Created June 11, 2012 18:52
Show Gist options
  • Save chrisbloom7/2911908 to your computer and use it in GitHub Desktop.
Save chrisbloom7/2911908 to your computer and use it in GitHub Desktop.
A cucumber/capybara step to test the order of data in a table, either with or without headers
<html>
<body>
<section class="left">
<article id="customers">
<h2>New Customers</h2>
<table class="data">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Middle Initial</th>
<th>Last Name</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Bobby</td>
<td>H</td>
<td>Raccicot</td>
<td><a href="#">Edit</a></td>
</tr>
<tr>
<td>3</td>
<td>Tim</td>
<td>I</td>
<td>Minchkin</td>
<td><a href="#">Edit</a></td>
</tr>
</tbody>
</table>
</article>
</section>
<section class="right">
<article id="sessions">
<h2>Active Sessions</h2>
<table class="data">
<tbody>
<tr>
<td>Minchkin, Tim</td>
<td><a href="#">View Trace</a></td>
</tr>
<tr>
<td>Jobs, Steven</td>
<td><a href="#">View Trace</a></td>
</tr>
</tbody>
</table>
</article>
</section>
</body>
</html>
Scenario: Testing Table Steps
Given I am on the example_table.html page
Then I should see the following report in the "New Customers" table:
| First Name | Last Name |
| Bobby | Raccicot |
| Tim | Minchkin |
And I should see the following report in the "Active Sessions" table:
| Minchkin, Tim |
| Jobs, Steven |
def check_simple_table_data(table_selector, table_data, options = {})
options.reverse_merge!(:headers => true)
page.should have_selector(table_selector)
within(table_selector) do
if !options[:headers]
header_map = (0...table_data.rows.first.length).to_a
row_count = table_data.raw.length
table_rows = table_data.raw
else
header_map = []
row_count = table_data.raw.length - 1
within("thead tr:first") do
columns = all("th").collect{ |column| column.text.downcase.strip }
columns.size.should >= table_data.headers.size
table_data.headers.each_with_index do |header, index|
column = columns.index(header.downcase.strip)
column.should_not be_nil
header_map << column
end
end
table_rows = table_data.raw[1...table_data.raw.length]
end
within("tbody") do
all("tr").size.should == row_count
xpath_base = './/tr[%i]/td[%i]';
table_rows.each_with_index do |row, index|
row.each_with_index do |value, column|
find(:xpath, xpath_base % [index + 1, header_map[column] + 1]).should have_content(value)
end
end
end
end
end
Then /^I should see the following report in the "New Customers" table:$/ do |table|
check_simple_table_data("#customers table", table)
end
Then /^I should see the following report in the "Active Sessions" table:$/ do |table|
check_simple_table_data("#sessions table", table, :headers => false)
end
@maxmckenzie
Copy link

model may be the wrong term, but would this be any better for the Then statements that fire that function?

Then /^I should see the following report in the "(.*?)" table:$/ do |table, model|
  check_simple_table_data(model, table)
end

Then its global and you can just pass all tables to one step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment