Passing arrays of variable in specflow

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.


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>   |