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.

Exact matches

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 colspan or rowspan attributes. The attached step definition offers a workaround for this: Wrap the entire table in a :

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 |
"""

Wildcards

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.

Caveat

The step looks at each row individually, so if you're skipping columns

| Name        | Id |
| Bruce Wayne | 35 |

will match

| Name        | Id  | Age |
| Bruce Wayne | 100 | 35  |

Changes

2013-02-20 (Tobias)

... with exactly the following rows

2012-06-20 (Henning)

Better normalization (more consistent for expected/real input, gets rid of nbsps)

2012-06-01 (Tobias)

Better wildcard support (partial matches)

2012-05-25 (Tobias)

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

2011-03-18 (Henning):

Allow to negate the expectation.

2011-03-16 (Henning):

Tables can now be wrapped in multiline strings. This way we can test tables that use colspan and rowspan, where every line does not have the same number of columns.

2010-10-06 (Arne):

Also handling th now; Removing line breaks, tabs and multiple spaces inside cells to be able to check against such content

Henning Koch over 11 years ago
This website uses short-lived cookies to improve usability.