What is SpecFlow

Specflow is a Behavior data-driven (BDD) automation framework. In simple words “Specflow is cucumber of .net”.

Gherkin language is used to write testcases in Sepcflow. Let’s have a look at the Gherkin language tutorial.

If you are a beginner for BDD automation. I will recommend checking the gherkin tutorial before starting this.

Important terms used in specflow.

Let’s discuss some terminologies used in sepecflow like Feature files, Step Definition, Scenarios, Scenario outlines ETC. then will move to installation and practical practice.

  • Feature file
  • Step definition
  • Scenario
  • Scenario outlines

What is Scenario in specflow?

A scenario is a collection of test steps to perform an activity. for example “login in the application”.

Example of a scenario:

Scenario: Login as Admin
Given I Navigate to the login page
And I Enter username and password
When I click to login button 
Then I can see admin is able to login

What is Scenario outlines in specflow?

Suppose, We need to write a test scenario where we need to execute the same scenario multiple times with different data in each execution.

Let’s understand with an example of login in the application.

In the below code Observe the “example” section that has two columns or variables name “Username” and “Password”. to call this variables specflow use <> syntax.

Observe the line And I Enter <username> and <password>. This scenario will execute 4 times that is a number of rows in the example. And pass the new value in every execution.

Scenario Outline: Login as Admin
Given I Navigate to the login page
And I Enter <username> and <password>
When I click to login button 
Then I can see admin is able to login
Examples:
| Username | Password |
| Asd@gmail.com | Pass@123 |
| Admin@gmail.com | Pass@123 |
| User@gmail.com | Pass@123 |
| Div@gmail.com | Pass@123 |

What is a Feature file in Specflow?

A file that contains scenarios of the requirements. A feature file can contain multiple scenarios it depends on the requirement or the module. Gherkin language will use to write the scenarios in the feature files.

Gherkin a simple language which is easily understood by the human. You can see the one example of gherkin language.

Sample Testcases in feature file(Login.feature)

Scenario: Login as Admin
Given I Navigate to the login page
And I Enter username and password
When I click to login button 
Then I can see admin is able to login 

Scenario: Login as admin with an invalid password
Given I have login with the invalid credential as an "Invalid_Password"
Then I can see the error message


Scenario: Login as Invalid UserName and Invalid Password
Given I have login with the invalid credential as an "Invalid_User_Password"
Then I can see the error message

This is the simple example of a test scenario that is about to add two numbers you can see the steps it’s easily understandable for normal people(Stack holders/Business analyst).

What is Step definition is Specflow

Step definition is a line of the code that binds with specflow steps. That helps to perform the activity according to the step. Specflow provides auto-generated code for every step just we need to add out code in the skeleton.

How to generate step definition is the specflow?

Below are the steps to generate the step definition is specflow.

1) Right-click on the steps in your feature file and click to “Generate the step definition”.
Or

Click to “Go to definition”.

2) Copy the auto-generated code and paste in respected file of a step definition.

Sample code for the step definition with respect to the above example.

[Given(@"I Navigate to the login page")]
public void GivenINavigateToTheLoginPage()
{
//Enter your action code here
ScenarioContext.Current.Pending();
}

[Given(@"I Enter username and password")]
public void GivenIEnterUsernameAndPassword()
{
ScenarioContext.Current.Pending();
}

[When(@"I click to login button")]
public void WhenIClickToLoginButton()
{
ScenarioContext.Current.Pending();
}

[Then(@"I can see admin is able to login")]
public void ThenICanSeeAdminIsAbleToLogin()
{
ScenarioContext.Current.Pending();
}