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

Get The Cucumber Book, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.