Skip to content

Instantly share code, notes, and snippets.

@bogdanRada
Forked from chrisbloom7/example_table.html
Created February 20, 2018 18:43
Show Gist options
  • Save bogdanRada/480b56fccec38397fac8f86b0fb19bdb to your computer and use it in GitHub Desktop.
Save bogdanRada/480b56fccec38397fac8f86b0fb19bdb 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment