Boolean parameters for SpecFlow steps

In SpecFlow, I want to check for the presence of a string in a step definition, and for the moment I'm doing awkward things like this contrived example:

[Given(@"Foo ( bar)?")] public void GivenFoo(string bar) { if (bar == " bar") { // do bar } } 

However, I would like to do something like this:

 [Given(@"Foo ( bar)?")] public void GivenFoo(bool bar) { if (bar) { // do bar } } 

But I canโ€™t understand how this is possible, and if so, how?

+7
source share
3 answers

Based on your question and comments on Jakub's answer, it looks like you are trying to write one step that can span multiple user journeys through your site. SpecFlow is not really intended for this, and this probably indicates that you should try and improve the structure of your scripts / functions.

To answer your question directly, I do not believe that there is a way to infer logical values โ€‹โ€‹based on the existence of certain lines in the step definition.

If you want to continue this route, then your original example is probably the best choice.

I would recommend you not to use this approach, but instead take a look at restructuring your step definitions so that you can combine them together and reuse them in different scenarios. I'm really trying to think about an approximate definition of a step that would fit your decision.

An example of a multi-step approach might look like this:

 Given I have logged in as an existing user //1 And I have started my 6-step registration process //2 And I have filled in valid address values on step 1 //3 And I have left the fields blank on step 2 //4 ... etc When I save my registration 

And your steps:

  • go to the login page, log in as a valid user
  • go to step 1
  • fill in the fields with valid input, click "next"
  • click 'next'

You just need to make sure that each step is independent of the others as possible, so you can replace one step with a completely different one (for the new scenario) without affecting the others.

With this approach, you can still encounter complex (and possibly quite verbose) scenarios, but I think this is a better solution than trying to be smart and pack into one definition. You will probably end up with scripts that are not readable, and probably the code will be painful to read and maintain.

+1
source

You are looking for:

 [Given(@"I do something (times) times")] public void GivenIDoSomething(int times) { } 

Otherwise it seems sufficient

 [Given(@"I do something twice")] public void GivenIDoSomethingTwice() { } 

EDIT

I think that instead of the if in the step, you really want to separate the steps.

0
source

You can definitely do such things using the StepArgumentTransformation method. You still have to write parser logic, but you can split it into a method that is only intended to perform this parsing.

Example function file:

 Feature: I win if I am Batman Scenario: Happy Given I am the Batman Then I defeat the Joker Scenario: Sad Given I am not the Batman Then I am defeated by the Joker 

Interaction with technology (C #):

 [Binding] public class IWinIfIAmBatmanFeature { private bool iAmBatman; [StepArgumentTransformation(@"(am ?.*)")] public bool AmToBool(string value) { return value == "am"; } [Given(@"I (.*) the Batman")] public void WhoAmI(bool amIBatman) { iAmBatman = amIBatman; } [StepArgumentTransformation(@"(defeat|am defeated by)")] public bool WinLoseToBool(string value) { return value == "defeat"; } [Then(@"I (.*) the Joker")] public void SuccessCondition(bool value) { Assert.AreEqual(iAmBatman, value); } } 

The key factor is that the regex matching in your Given clause matches the conversion of the step argument. Thus, in I (.*) the Batman , if the capture matches the regular expression in the argument for StepArgumentTransformation, as in the attribute for AmToBool , then this is the transformation that is used.

0
source

All Articles