开发者

How can I repeat steps in a Specflow Scenario Outline

In a nutshell, what I need is to create a Scenario Outline with a step that is repeatable without having to type it in using multiple AND's as I am currently doing below:

Scenario Outli开发者_JS百科ne: outline
    Given I am a user
    When I enter <x> as an amount
       And I enter <x2> as an amount
    Then the result should be <result>
Scenarios:
    |x|x2|result|
    |1|2 |3     |
    |1|0 |1     |

However, I would like to do something like the following:

Scenario Outline: outline 
    Given I am a user
    When I enter <Repeat: x> as an amount
    Then the result should be <result>

Scenarios:
    |x    |result|
    |1,2,3|6     |
    |1,2  |3     |

Basically, I want the "I enter as an amount" to run 3 and 2 times respectively.

The closest I have found to this question is How do I re-run a cucumber scenario outline with different parameters? , but I wanted to double check before giving up and using a StepArgumentTransformation with a comma separated list or something similar.

The final answer that I went with is something more like this:

Scenario Outline: outline 
    Given I am a user
    When I enter the following amounts
        | Amount1 | Amount 2| Amount3|
        | <Amt1>  | <Amt2>  | <Amt3> | 
    Then the result should be <result>

Scenarios:
    |Amt1 |Amt2 |Amt3 |result|
    |1    |2    |3    |6     |

There does not seem to be a good way to leave a value blank though, but this is pretty close to the solution I was looking for


The Examples keyword:

Scenario Outline: outline
    Given I am a user
    When I enter <x> as an amount
    Then the result should be <result>
    Examples:
        |x|result|
        |1|3     |
        |1|1     |

Is for when you want the entire test to take different parameters. It sounds like you want to feed repeating parameters at the When step.

The easier approach that won't require implementation of a StepArgumentTransformation is to simply use a table in the when:

When I enter the following amounts
|Amount|
|2     |
|3     |
|4     |

Then simply iterate all the rows in table to get your arguments. This avoids needing to have an interim method for the transformation.

Alternatives include using more steps or by parameter parsing, so as you say, use the StepArgumentTransformation.

Of course, if you need to test multiple repeating items, you could use both StepArgumentTransformation and Examples: feeding in a comma-list of numbers:

Examples:
|x    |result|
|1    |1     |
|1,2,3|6     |


You might be able to make use make use of an approach patrickmcgraw suggested on an answer to my question:

SpecFlow/Cucumber/Gherkin - Using tables in a scenario outline

So basically you could take the input x as a normal input string split it on a delimiter in this case ',' and then iterate over it performing your actions i.e. something like below (I haven't tested this code).

[When(@"I enter (.*) as an amount")]
public void IEnterAsAnAmount(string x)
{
   var amounts = x.Split(',');
    foreach (var amount in amounts)
    {
        // etc...
    }
}


This works for me and looks bit more readable too:

Scenario: common scenarios goes here one time followed by scenario outline.
   Given I am a user

Scenario Outline: To repeat a set of actions in the same page without logining in everytime.
   When I enter <x> as an amount
   Then the result should be <result>

Examples:
|x|result|
|1|2|
|2|4|
|4|8|
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜