Book Image

Cucumber Cookbook

By : Shankar Garg
Book Image

Cucumber Cookbook

By: Shankar Garg

Overview of this book

Table of Contents (13 chapters)
Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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