Site Loader
Auckland, New Zealand
In the last post of specflow, we discussed about scoped bindings, in this post we will discuss how to work with custom types of .Net as parameters for step definitions in specflow.

Argument conversion

In Specflow, argument transformations are done effortlessly by itself from a feature to step definition, If we create an step with integer value, then specflow will deal with it automatically Similarly specflow deals with .Net types automatically
  • Tables
  • Strings
  • Booleans etc
So, how to deal with custom arguments which specflow does not support out of the box ?

Step Argument transformation

There comes, Step argument transformation feature of specflow, which can be used to apply a custom conversion step for the arguments of the step definitions. The step argument transformation is a method that provides a conversion from text (specified by a regular expression) or from a Table instance to an arbitrary .NET type.

To achieve step argument transformation

To achieve step argument transformation following rules needs to be followed
  • the return type of the transformation is the same as the parameter type
  • the regular expression (if specified) is matching to the original (string) argument
  • if there are multiple matching transformation available, a warning is provided in the trace and the first transformation is used
This is how a step argument transformation looks like in the code which we will discuss in the video below Here is the complete video of the above discussion

Thanks for reading the post and watching the video!!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “Step argument transformation in Specflow”

  1. Is it possible to do argument transformation for a field in a table using a Regular Expression?

    For example,
    I have a table like
    | PromptDate | PromptCount | LastUpdatedTimeStamp |
    | Today | 2 | 2 hours ago |

    PromptDate and LastUpdatedTimeStamp are both Datetime types in my object.

    I would want to convert ‘Today’ to Current Date, which is DateTime.Today and transform 2 hours ago to DateTime.Now.AddHours(-2).

    How can this be done in Specflow ?

Leave a Reply to Divya Cancel reply

Your email address will not be published. Required fields are marked *