Skip to content

Instantly share code, notes, and snippets.

@odlp
Last active August 13, 2020 14:37
Show Gist options
  • Save odlp/3d654bb5a3acc2ff4c79ce8c2d7a6859 to your computer and use it in GitHub Desktop.
Save odlp/3d654bb5a3acc2ff4c79ce8c2d7a6859 to your computer and use it in GitHub Desktop.
Capybara table helper
module TableSupport
def column_names(table_selector = "table")
find(table_selector).all("th").map(&:text)
end
def values_for_column(column_name, table_selector = "table")
table = find(table_selector)
index = table.all("th").map(&:text).find_index(column_name)
expect(index).to be_present, "Unable to find column named: #{column_name}"
css_index = index + 1
table.all("tbody tr td:nth-of-type(#{css_index})").map(&:text)
end
def values_for_row(index, table_selector = "table")
table = find(table_selector)
css_index = index + 1
values = table.all("tbody tr:nth-of-type(#{css_index}) td").map(&:text)
headings = table.all("th").map(&:text)
headings.zip(values).to_h
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment