Posted about 9 years ago. Visible to the public. Repeats.

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:

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

Copy
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

Copy
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 multiline string:

Copy
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

Copy
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

Copy
| Name | Id | | Bruce Wayne | 35 |

will match

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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 2 years ago
Keywords:
steps
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more