Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Last active August 29, 2015 14:01
Show Gist options
  • Save ravinggenius/298aac91bd7989a81332 to your computer and use it in GitHub Desktop.
Save ravinggenius/298aac91bd7989a81332 to your computer and use it in GitHub Desktop.
Helpers for dealing with tables when integration testing with Capybara
module StepHelpers
def table_for(table_or_id)
if table_or_id.is_a?(Capybara::Node::Element)
table_or_id
else
find("##{table_or_id}")
end
end
def row_containing_text(table_or_id, text)
table_for(table_or_id).find(:xpath, "tbody//tr[(* | *//*)[contains(text(), '#{text}')]]")
end
def table_row_hash(table_or_id, text)
table = table_for(table_or_id)
row = row_containing_text(table, text)
table.all('thead th').zip(row.all('th, td')).inject({}) do |memo, (header, cell)|
memo.merge(header.text => cell.text)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment