Problem
I want to develop automation tests using BDD framework SpecFlow
Pre-condition
1) Visual Studio is installed.
2) Solution with basic test automation framework is set-up in Visual Studio.
Solution
SpecFlow installation
1. Go to Tools -> Extensions and Updates
2. Open Tab Online, type SpecFlow to search pane, click Download for first result, and then click Install. After Installation restart VS.
3. As SpecFlow is integrated with NUnit, in order to see tests in Test Explorer of VS, you need to install NUnit Test Adapter too.
4. Add SpecFlow NuGet package for your solution. Right-click on Solution, click Manage NuGet Packages for Solution
5. Open Tab Online, type SpecFlow to search pane, click Install on found result, select test project for SpecFlow installation
First test creation
The tests in BDD are written in a human-manner language, that is called Gherkin language. Files that contain this tests are called Features in SpecFlow. Let's create a feature file with a test.
Let's say, I want to visit site Flipkart.com and navigate to Search page, perform search with a keyword and check search results are displayed.
1. In Test Project, make folder Features.
2. Add SpecFlow Feature file to test solution. Right-click on folder Features, click Add -> New Item
3. Here you can see the files that have been added during SpecFlow installation. Choose SpecFlow Feature File, name it and click Add.
4. Copy below code to created feature file
- Feature - feature name that is tested. Can also be considered as the test class name.
- Scenario - can also be considered as test name.
- Given - a pre-condition action for the test
- When - actual actions of the test follow after that keyword
- Then - actual action step. It is usually used as an assertion step.
- And - copies last used keyword, e.g. in this test it's the same as Then . It's named so to make scenario more human-readable.
Ok, the BDD scenario is set-up and even non-IT person can understand what this test should do. But the computer does not understand what this words mean.
To make computer understand this scenario, you need to make a Definition class. It contains bindings for the computer to transform this human-readable text into actual actions on the page.
5. Add folder Definitions to test project. Right-click on it and click Add -> New Item. Choose SpecFlow Step Definition, name it, and click Add.
6. SpecFlow will produce a template Definition class. Replace it with below code. I will use my test framework methods, but I'm sure you can replace it with yours .
Ok, let's talk about what we see here:
- Class with [Binding]. It means that it contains "linking methods" for the computer to understand your scenario.
- Methods marked with attributes [Given], [When], [Then]. Code in this methods will be executed when the SpecFlow interpretator hits the line in the test with defined a) keyword b) text that is standing after that keyword. Example: SpecFlow walks through scenario See Top Tracks, sees line 'Given I navigate to application'. Then it goes to Definition class and sees method marked with keyword Given and string value for it - I navigate to application. The line in scenario and definition for it are matching -> method is set for execution.
- You can pass strings as the parameters into corresponding method. In Scenario you can see line - When I click 'Music' on Navigation Bar. Word 'Music' is marked with deep grey in Feature file.
In Definition file you can see method marked with [When("I click '(.*)' on Navigation Bar")]. (.*) is a regex representing all text, and the text in scenario standing for this regex will be passed into the method as parameter. So the Scenario line - When I click 'Music' on Navigation Bar - will be equal to call of Definition class method NavigationBarItemClick with parameter "Music".
Placing Scenario here so you could relate Defintion methods to Scenario lines.
7. Now Rebuild your Solution. With NUnit Test Adapter installed, you can see Feature with Scenario displayed in Test Explorer.
Data-Driven approach with SpecFlow
You may also implement Data-Driven testing using SpecFlow to do this:
- Replace Scenario keyword with Scenario Outline
- Define table of data with keyword Examples
- Define column name and test data under it
- Pass test data into test with placeholder <ColumnName>
End-state
One can write tests in BDD style using SpecFlow
Reference
SpecFlow is a powerful BDD framework that has many features. You can start from below links if you want to learn more:
https://github.com/techtalk/SpecFlow/wiki - section Pages
0 comments:
Post a Comment