In the following post I will show a quick tip on now you can write a custom transformer in order to parse decimal value from your step
Intellij IDEA 2016.2 CE is used as IDE
Target audience: QA Automation engineers / developers
DISCLAIMER: It is assumed that reader has some experience with Behavior Driven Development approach and a Cucumber test tool. For full please refer to official Cucumber website.
A little bit of theory
By default Cucumber recognizes only two possible parameters which come from your steps: strings (single and double-quoted) and integers.
If Cucumber parser see strings:
Then it will propose to the user the generated code with sample parameters of type string:
The same is for integer values in scenario step:
And as a result:
NOTE: Pay attention on how Cucumber generates regex for strings and integers by default.
What if you want something more complex?
There are a lot of situations when complex data should be used as a parameter in scenario steps.
It can be for example decimal value or your custom data type.
In Cucumber for Java book can be found a well described example of getting Money object as a step parameter. Example is written in Java and can be found here.
The main point is to write your custom class which extends Cucumber’s Transformer and then use it step implementations as annotation for specific parameter.
Okay, but can we write it on Scala?
In Scala language we can write our custom transformer as well. It is a little bit trickier, but pretty easy.
For example we need to parse decimal value and use it in our step
When you run scenario, logs will show that the value 1.2 is transformed by Cucumber as two integer parameters with a point delimeter.
Here are the steps for implementing your custom transformer for BigDecimal value
1. Create your custom trnasformer class for BidDecimal values
NOTE: Cucumber for Scala does not recognize native scala.math.BigDecimal values (and in fact throws an exception that custom transformer class should be implemented) - as a result we should implement a basic wrapper for desired type (or your own custom type) with XStreamConverter annotation.
2. Use custom transformer in Step Definitions
NOTE: Pay attention on regular expression for single decimal value “([\d.]*)” and also for usage your custom MyBigDecimal wrapper with Transform annotation.
For now you can write transformer for many custom data types in your Cucumber steps.