Specflow - Create First Test

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
FlipkartDemo.feature
Feature: Search
     
Scenario: Verify Search functionality
Given I navigate to "http://www.flipkart.com" web site
When I perform search with "Apple iphone 5s"
Then The result should be displayed



Let's see the keywords that SpecFlow uses:
  • 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 (smile).
Definiton.cs
using System;
using System.Linq;
using TechTalk.SpecFlow;
using TestFramework.Core.BrowserUtils;
using TestFramework.Utilities.Pages;
namespace TestFramework.Tests.Definitions
{
    [Binding]
    public sealed class SomeDefinition
    {
        Actions.Actions actions = new Actions.Actions();
        ResultsPage resultPage = new ResultsPage();
        HomePage homePage = new HomePage();

        Library.Lib lib = new Library.Lib();
        [Given(@"I launch Browser")]
        public void GivenILaunchBrowser()
        {
            Assert.IsNotNull(Library.Lib.Driver);
        }

        [Given(@"I navigate to ""(.*)"" web site")]
        public void GivenINavigateToWebSite(string url)
        {
            Library.Lib.Driver.Navigate().GoToUrl(new Uri(url));
        }

        [When(@"I perform search with ""(.*)""")]
        public void WhenIPerformSearchWith(string searchString)
        {
            homePage.PerformSearch(searchString);
        }

        [Then(@"The result should be displayed")]
        public void ThenTheResultShouldBeDisplayed()
        {
            Assert.IsNotNull(Library.Lib.Driver.FindElement(By.Id("searchCount")));
        }
    }
}
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:
  1. Replace Scenario keyword with Scenario Outline
  2. Define table of data with keyword Examples
  3. Define column name and test data under it
  4. Pass test data into test with placeholder <ColumnName>
The Scenario code will look like this:

DDT Scenario
Scenario Outline: Verify Search operation iphone
Given I navigate to "http://www.flipkart.com" web site
When I perform search with "<SearchString>"
Then The result should be displayed
Examples: 
| SearchString    |
| Apple iphone 5s |
| Apple iphone 4  |
| Apple iphone 6s |

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:

0 comments:

Post a Comment