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.
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.
Import java.util.Date
into your Step Definitions file. This is how our Step Definitions will look:
@Given("^My Birthday is on \"(.*?)\"$")
public void my_Birthday_is_on(@Format("dd-MM-yyyy") Date bday) {
//prints the text converted to Java.util.Date
System.out.println(bday);
//prints the class of bday to confirm...