Cucumber step to match table rows with Capybara
These steps are now part of Spreewald.
This note describes a Cucumber step that lets you write this:
Then I should see a table with the following rows: | Bruce Wayne | Employee | 1972 | | Harleen Quinzel | HR | 1982 | | Alfred Pennyworth | Engineering | 1943 |
If there are additional columns or rows in the table that are not explicitely expected, the step won't complain. It does however expect the rows to be ordered as stated. If you don't like this, you can append "... in any order":
Then I should see a table with the following rows in any order: | Bruce Wayne | Employee | 1972 | | Harleen Quinzel | HR | 1982 | | Alfred Pennyworth | Engineering | 1943 |
You can find the step definition below.
If you do not want to match tables that have additional rows, you can use
Then I should see a table with exactly the following rows: ...
Testing tables with colspan or rowspan
Cucumber tables are required to have the same number of cells in every row. This is unfortunate when testing a table that uses
rowspan attributes. The attached step definition offers a workaround for this: Wrap the entire table in a multiline string:
Then I should see a table with the following rows in any order: """ | Date | Name | | Tue | 2011-02-08 | Bruce Wayne | | Wed | 2011-02-09 | Harleen Quinzel | """
If you don't care about (part of) an existing cell, you can use a wildcard, i.e
Then I should see a table with the following rows: | User | Born | | Bruce * | 1972 | | * Quinzel | 1982 | | * | 1943 |
Otherwise cells are matched exactly modulo surrounding whitespace.
The step looks at each row individually, so if you're skipping columns
| Name | Id | | Bruce Wayne | 35 |
| Name | Id | Age | | Bruce Wayne | 100 | 35 |
... with exactly the following rows
Better normalization (more consistent for expected/real input, gets rid of nbsps)
Better wildcard support (partial matches)
Better error output; "should not see" now fails if any of the rows is present; cells are now matched exactly, wildcard for values we don't care about
Allow to negate the expectation.
Tables can now be wrapped in multiline strings. This way we can test tables that use
rowspan, where every line does not have the same number of columns.
th now; Removing line breaks, tabs and multiple spaces inside cells to be able to check against such content