Skip to main content

Posts

Showing posts from September, 2013

Magic: strings and regular expressions in ruby Cucumber tables

I stumbled on an interesting trick that is worth documenting

Say you have a Cucumber Scenario Outline like so:

Scenario Outline: Foo and bar When I click < foo > Then < bar > should appear in the page Examples: | foo | bar | | X | =String or regex | | Y | ===String or regex | | Z | String or regex |
and we use it like so:

Then(/^(.+) should appear in the page$/) do |bar| on(FooPage).page_contents.should match Regexp.new(bar) end
but this won't work, because the case with "=" will pass by accident if page_contents erroneously contains "===" instead.  And case Z would always pass no matter how many '=' characters are in the page.  (That's clear, right?  Say something in the comments if that doesn't make sense to you.)

Also, I really need to check for a leading space for case Z, so what I think I need is not a string but a regular expression, so those a…