Saturday, 31 December 2016

C# Xamarin Studio - SpecFlow configuration, creating sample feature file, step defintion and execution.

Prerequisite:
Download and install latest version of Xamarin Studio from http://www.monodevelop.com/download/

Download SpecFlow addin for Xamarin Studio 6 from

Xamarin Studio addin for SpecFlow is downloaded.

Open the Xamarin Studio and 
select 'Tools' -> 'Add-ins'

Click 'Install from file' button.

Open the location of file downloaded(i.e. specflow addin file 'TechTalk.SpecFlow.TechTalk.SpecFlow_1.9.1.0.mpack')
   Click 'Open' button.

 Click 'Install' button.

Open the Xamarin Studio, Create the new solution

 New Project dialog is opened,
    select 'Other > Miscellaneous > General > SpecFlow NUnit Library Project' to select SpecFlow template for project.

Enter project name and click 'Create' button.
   ex- project name: FirstSpecFlowTest  
   
Feature file is created(i.e. default feature file is created)

Select 'View' -> 'Design' to view solution explorer.

Add package 'Xamarin.UITest' from Nuget package to the project.  
       Search for 'Xamarin.UITest', select checkbox for 'Xamarin.UITest' and click 'Add Package'.

'Xamarin.UITest' is installed and added to project package.

Select 'View' -> 'Test' to open 'Unit Test Window'

 If 'Unit Test' pad is not opened, then select 'View' -> 'Pads' -> 'Unit Tests' to view 'Unit Test' pad.

Two files are present(i.e. created)
     Test.feature -> Feature file
     TestSteps.cs -> Step definitions file

Build Project

Run Test in 'Unit Test'

Test Result

Add Console.Writeline(....) in step definition methods.
    Note: Console.Writeline(...) is added for only test demo purpose, in real time there will be some logic/implementation is added instead of Console.Writeline(...) in step definition methods.

Build the project and run the test again

Open the 'Application Output', by selecting 'View' -> 'Pads' -> 'Application Output'
    View the output of Console.Writeline(....) in 'Application Output'     

References:  

C# Visual Studio - SpecFlow Step Definition file

Prerequisite:
Feature file is already created.
Creating the feature file: https://automation-home.blogspot.com/2016/12/c-visual-studio-specflow-feature-file.html


Create the class for step defintions

 Enter the class name and click 'Add' button.
   ex: SampleFirstFeatureSteps.cs
       As a best practice, follow file name ends with 'Steps' <<FileName>>Steps.cs

Class file for step definitions is created.

Mouse right click on step in feature file and select the 'Generate Step Definitions'.

Click 'Preview' button.

Step definition skeleton preview is displayed, close the preview.

Copy the methods(i.e. step definitions methods)

Open the step definitions class file(i.e. 'SampleFirstFeatureSteps.cs'),
   paste the methods(i.e. step definitions) in class   

Since references are missing, shows compilation error for the methods.

Add the missing references by clicking (Ctrl+.)  

Open the feature file, right click on the step and select 'Go To Definition'.

Click 'No' button to close.
There is no binding between Feature file and Step Definitions.
Since there is no binding above dialog window is displayed.

Add the attribute [Binding] for class in Step Definition file.

Remove Unncessary Usings.

Observe the colour for steps in feature file, before and after adding [Binding] attribute for Step Definition class.
Open the Feature file, mouse over on the step right click and select 'Go To Definition'.
    This time when 'Go To Definition' is selected, Steps defintion file is opened(i.e Binding of feature file and step defintion file is successfull).

As a best practice place the feature file in 'Features' folder and step definitions file in 'Step_Definitions' folder.

<< Previous