Passing arrays of variable in specflow

18.9k Views Asked by At

Is there a way to pass an array of parameters instead of passing each parameter individually?

For example I have the following scenarios:

When i login to a site
then <firstname>, <lastname>, <middleName>, <Desingation>, <Street>, <Apartmentno> are valid

The list can go on above. Instead can I pass all the above variables in an array?

3

There are 3 best solutions below

2
On BEST ANSWER

You can pass a comma separated string and then transform it into a list:

When i login to a site
then 'Joe,Bloggs,Peter,Mr,Some street,15' are valid

[Then("'(.*)' are valid")]
public void ValuesAreValid(List<String> values)
{
}

[StepArgumentTransformation]
public List<String> TransformToListOfString(string commaSeparatedList)
{
    return commaSeparatedList.Split(",").ToList();
}

if you want the values to come from examples then you could do this instead:

When I login to a site
then '<values>' are valid
Examples
| values                            |
| Joe,Bloggs,Peter,Mr,Some street,15|
| Joe,Bloggs,Peter,Mr,Some street,16,SomethingElse,Blah|

If you want to use a table then you could do this instead:

When I login to a site
then the following values are valid
    | FirstName | LastName | MiddleName | Greeting| Etc    | Etc     |
    | Joe       | Bloggs   | Peter      | Mr      | you get| The Idea|

(you could omit the headers if you want and just use the row values I think)

you can also use examples with this:

When I login to a site
then the following values are valid
    | FirstName | LastName  | MiddleName  | Greeting  | Etc    | Etc     |
    | <name>    | <lastName>| <middleName>| <greeting>| <etc>  | <etc>   |
0
On

This might be of help: https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions

Add the following code snippet to your Common Step Definition File:

[StepArgumentTransformation]
public string[] TransformToArrayOfStrings(string commaSeparatedStepArgumentValues)
{
  string sourceString = commaSeparatedStepArgumentValues;
  string[] stringSeparators = new string[] { "," };
  return sourceString.Split(stringSeparators, StringSplitOptions.None);
}

SpecFlow will then automatically convert all comma-separated values in the SpecFlow Steps data table into an array of strings.

Then in your individual step binding function, change the type of the input parameter as string[] as in snippet below:

[Then(@"the expected value is '(.*)'")]
public void ThenTheExpectedValueIs(string[] p0)
{
    //ScenarioContext.Current.Pending();
    Assert.AreEqual(25, Convert.ToInt32(p0[0]));
    Assert.AreEqual(36, Convert.ToInt32(p0[1]));
    Assert.AreEqual(79, Convert.ToInt32(p0[2]));
}

Then, based on your expected value for a test step, you may want to apply the appropriate type conversion.

0
On

Just transfer the data as a string Example:

Then LEDS 0, 1, 7 are on

[Then(@"LEDS (.*) are on(.*)]
    public void ThenLEDAreOn(string p0)
    {
        int count = p0.Split(',').Length - 1;
        string[] Leds_on = p0.Split(',');
        foreach (string s in LEDs_on)
        {
            int.TryParse(s, out LEDS[index]);
            index++;
        }
    }

Then you have your values as integers in an array