Cucumber Expressions: Using specific parameters
I've started looking into the possibility of upgrading to SpecFlow 4 by creating a branch, upgrading to 4 and seeing what breaks. Of course, all of the regular expressions that I use in many of my steps need to be either converted to Cucumber Expressions (which is the goal) or marked to be specifically read as a Regular expression.
While Cucumber Expressions are definitely easier to read, I've run into a situation where I end up having to use regular expressions instead. That situation is where I pass a value to a parameter that has a very finite number of possible values. Take the snippet below as an example.
Example 1 (Regular Expression):
[When(@"^(?:I\s)?login as (?:a|an) (admin|supportuser)$")]
public void LoginAsUser(string userType)
Here there are two possible types of users that I can log in with, a support user or an admin. Using a regular expression forces string userType to be only one of those two choices, because the step won't bind to a line in the feature file if you don't use one of those two options. When you're writing your gherkin, the tooltip shows up and you can see specifically what your options are.
In this other example, however, you don't get any insight as to your allowed options.
Example 2 (Cucumber Expression):
[When("(I )login as a/an {word}")]
public void LoginAsUser(string userType)
Here, the Cucumber Expression allows me to cleanup the regular expression, making things more readable. My first action in the method would be to throw an error if something other than admin or support user was passed to the step. But this seems clunky to me.
Another option is to just put in an optional string in the step binder.
Example 3 (Cucumber Expression w/ optional value):
[When("(I )login as a/an {word}( options: admin or supportuser)")]
Doing this, I can see the available options when I look at the IntelliSense when writing out the Gherkin. But again, this seems less than optimal.
What I'm looking for is the ability to do something like this using Cucumber Expressions.
Example 4 (My ideal):
[when("(I )login as a/an {admin}/{supportuser}"]
To me, this seems much more elegant. If someone has experience with doing something similar, I would love to hear what your solution was.
Thanks!
Please sign in to leave a comment.
Comments
0 comments