Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arrays of variable in specflow

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?

like image 405
AutomateFr33k Avatar asked Jun 25 '15 23:06

AutomateFr33k


2 Answers

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>   |
like image 109
Sam Holder Avatar answered Nov 15 '22 21:11

Sam Holder


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.

like image 2
Abhijith Avatar answered Nov 15 '22 22:11

Abhijith