Tuesday 27 May 2014

SpecFlow with CodedUI - BDD framework

Here mainly we are not discussing in deep, instead just a brief about how exactly the BDD framework can be developed by using CodedUI and its integration with the SpecFlow


Final look of the Solution Explorer


This is how the Solution explorer looks for the sample test written for testing a Addition test scenario for the Calculator.
Pre Setup to be done:
·         Add the following references

·         TechTalk.Specflow.dll is to be downloaded from SpecFlow_v1.9.0_bin.zip and added in the reference.
·         Download and install SpecFlow 1.9 IDE for integrating it with the Visual studio 2012 premium.
                once it is installed, right click on the project and select “Add an new item” and add the SpecflowFeature1.feature and then a StepDefinition.cs file
·         Add the App.Config file with the following data
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
  </configSections>
  <specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
  <unitTestProvider name="MsTest" generatorProvider="Specflow.CodedUI.MsTestCodedUiGeneratorProvider, Specflow.CodedUI" runtimeProvider="TechTalk.SpecFlow.UnitTestProvider.MsTest2010RuntimeProvider, TechTalk.SpecFlow" /></specFlow>
</configuration>


This makes the initial setup done for the project.

Coding Part

Now comes the coding part.
To make things understandable, or make things easy, we defined 3sets of UIMaps
GeneralUIMap – Consists of general functions likes launching Calculator, closing Calculator
StandardUIMap – Consists of all the functions related to the Standard Calculator
ScientificUIMap – Consists of all the functions related to the Scientific Calculator

These UIMaps file can be generated separately in a CodedUI Project and then added into the UIMap folder.

UIMapLoader folder consists of the the cs file where we define a class and the instances of all the three UIMap class.
using Microsoft.VisualStudio.TestTools.UITest.Common.UIMap;
using SpecflowAndCodedUI.UI.GeneralUIMapClasses;
using SpecflowAndCodedUI.UI.ScientificUIMapClasses;
using SpecflowAndCodedUI.UI.StandardUIMapClasses;

namespace SpecflowAndCodedUI.UI.UIMapLoader
{
    public static class Calculator
    {
        public static ScientificUIMap Scientific
        {
            get { return _scientific ?? (_scientific = new ScientificUIMap()); }
        }

        private static ScientificUIMap _scientific;

        public static StandardUIMap Standard
        {
            get { return _standard ?? (_standard = new StandardUIMap()); }
        }

        private static StandardUIMap _standard;

      
 public static GeneralUIMap General
        {
            get { return _general ?? (_general = new GeneralUIMap()); }
        }

        private static GeneralUIMap _general;
    }
}



The sample scenario which can be specified in the SpecflowFeature1.feature file

Feature: SpecFlowFeature1
                In order to avoid silly mistakes
                As a math idiot
                I want to be told the sum of two numbers

@mytag
Scenario: Add two numbers
                Given I have entered 50 into the calculator
                Given I press add
                And I have entered 70 into the calculator
                When I press equals
                Then the result should be 120 on the screen

// Below scenario is for the Data Driven Approach
@mytag
Scenario Outline: Add two numbers for different scenarios
                Given I have entered "<Input1>" into the calculator
                Given I press add
                And I have entered "<Input2>" into the calculator
                When I press equals
                Then the result should be <Result> on the screen

Examples:
| Input1 | Input2 | Result |
| 20     | 70     | 90     |
| 50     | 100    | 150    |



The StepDefinitions.cs file looks like this

namespace SpecflowAndCodedUI.StepDefinitions
{
    using SpecflowAndCodedUI.UI.UIMapLoader;
    using System;
    using TechTalk.SpecFlow;

    [Binding]
    public class StepDefinitions
    {
        [BeforeScenario()]
        public static void PrepareForTest()
        {
            Calculator.General.LaunchCalculator();
        }

        [AfterScenario]
        public void CleanUpTest()
        {
            Calculator.General.CloseCalculator();
        }

        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(string input)
        {
            Calculator.Standard.TypeAValue(input);
        }

        [Given(@"I press add")]
        public void GivenIPressAdd()
        {
            Calculator.Standard.ClickAddButton();
        }

        [When(@"I press equals")]
        public void WhenIPressEquals()
        {
            Calculator.Standard.ClickEqualsButton();
        }

        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBe120OnTheScreen(string expectedResult)
        {
            //Is is the alternative to modifying the Coded UI Test Method to accept a parameter as we did for the TypeAValue method
            //Instead you can use the Coded UI Tests Params properties to pass the value.
            Calculator.Standard.AssertTheResultExpectedValues.InputLableValueDisplayText = expectedResult;
            Calculator.Standard.AssertTheResult();
        }
    }
}

The coding part is done
Now to execute the scenarios, right click on the SpecFlowFeature.feature and select “Run SpecFlow Scenarios
Happy coding.

Let me know if anyone needs any more details information or sample project to be shared

No comments:

Post a Comment