Defining String transformations for better conversions

Think about a Scenario where you want to convert some Strings in test Steps to some specific Strings in your code. For example, the PO has mentioned "29-12-1986" in a Step and you want Cucumber to understand this text as a date. Further, in some countries this could be in the DD-MM-YYYY format, while in others, it could be in the MM-DD-YYYY format. So how do we standardize this conversion? Let's find out how we can do this in Cucumber.

Getting ready

Consider the following test Step for this problem:

Given My Birthday is on "29-12-1986"

Now, we can use the @Format String transformer to convert text to date.

How to do it…

Import java.util.Date into your Step Definitions file. This is how our Step ...

Get Cucumber Cookbook 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.