O'Reilly logo

The Cucumber Book, 2nd Edition by Steve Tooke, Aslak Hellesoy, Matt Wynne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Capturing Arguments

You’ll notice that in the step we’ve been using as an example, we’ve talked about the sum of $100 the whole time. What if we had another scenario where we needed to deposit a different amount of money into the account? Would we need another step definition, like this?

 Given(​/I have deposited \$100 in my Account/​) ​do
 # TODO: code goes here
 end
 
 Given(​/I have deposited \$250 in my Account/​) ​do
 # TODO: code goes here
 end

Happily, we don’t. This is where the flexibility of regular expressions comes into play. We can use two of regular expressions’ most useful features here to capture any dollar amount as an argument to the step definition. Those features are capture groups and wildcards.

Capture Groups ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required