Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Cucumber Cookbook

You're reading from   Cucumber Cookbook Over 35 hands-on recipes to efficiently master the art of behaviour-driven development using Cucumber-JVM

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781785286001
Length 162 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Shankar Garg Shankar Garg
Author Profile Icon Shankar Garg
Shankar Garg
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Writing Feature Files FREE CHAPTER 2. Creating Step Definitions 3. Enabling Fixtures 4. Configuring Cucumber 5. Running Cucumber 6. Building Cucumber Frameworks 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images