Reputation: 93
I'm having long Scenario with Specflow and I need to add 3 users, all the steps are the same. How can I repeat just couples of steps in the flow?
Given Go to page
#Login
And Click on sign in button
When Enter email '[email protected]'
And Enter password '123456'
And Click on Submit button
And click on Events list button
And Open Event
And Go to new opened page
And Click on Register to event button
When Click on Person title '<title>'
And Select ticket package
And Select ticket quantity '15'
And Click on submit registration button
And Enter mail <email> to register to event
And Enter first name <first name> to register to event
And Enter last name <last name> to register to event
And Click on submit registration button
And Click on submit registration button
Examples:
| title | email | first name | last name |
| Person 1 | [email protected] | Person1 | Person1 |
| Person 2 | [email protected] | Person2 | Person2 |
| Person 3 | [email protected] | Person3 | Person3 |
When click on Continue button Then 3 user should be in the list
Upvotes: 2
Views: 3168
Reputation: 93
what I did and seems working fine for me
When Register multiple users '3' to event click on 'Person'
[When(@"Register multiple users '(.*)' to event click on '(.*)'")]
public void WhenRegisterMultipleUsersToEventClickOn(int userQty, string title)
{
for (int i = 1; i <= userQty; i++)
{
WhenClickOnPersonTitle(title + " " + i);
WhenSelectTicketPackage();
WhenSelectTicketQuantity(10);
WhenClickOnSubmitRegistrationButton();
WhenEnterMailToRegisterToEvent("mail" + i + "@mai.com");
WhenEnterFirstNameToRegisterToEvent("Name" + i);
WhenEnterLastNameToRegisterToEvent("Lastname" + i);
WhenClickOnSubmitRegistrationButton();
WhenClickOnSubmitRegistrationButton();
}
}
Upvotes: 5
Reputation: 1607
You can call steps from within another step. Within your steps class inherit the TechTalk.SpecFlow.Steps class like so;
public sealed class RegisterMultipleUsersSteps : Steps
{
...
}
Ok so why is this helpful? well you can now call the 5 steps to create a single user into one step so do something like below
Scenario Outline: Register With Multiple Users
Given Go to page
...
When i create user 'Person1' 'Last name 1' with email '[email protected]'
When i create user 'Person2' 'Last name 2' with email '[email protected]'
When i create user 'Person3' 'Last name 1' with email '[email protected]'
...
Then Some condition
I'll assume you already have the steps to create the user with the individual credentials already created since you listed them in your question.
In you steps class for this feature, where you previously inherited the spec flow steps class do something like the following
[When(@"I create user (.*) (.*) with email (.*)")]
public void WhenICreateUserTitleNameSurnameWithEmailAddress(var firstName, var lastName, var emailAdd)
{
// Now because you inherited the specflow steps class
// you can call other steps that you already have
When(string.Format("Enter mail {0} to register to event", emailAdd));
When(string.Format("Enter first name {0} to register to event", firstName));
When(string.Format("last name {0} to register to event", lastName));
When("Click on submit registration button");
}
This allows you to more easily create a user without cluttering your scenarios too much. You could probably merge some more of your steps as well.
A side note, i think the generally accepted standard for writing in gerkhin language is to keep features as simple as possible normally no more than 5 steps, this is documented somewhere on specflows website and loads of other places. The idea being that if you need more than that you should split up your tests or some functionality can be merged.
I hope the above helps.
Upvotes: 0