Behaviour Driven Development using Specflow with Webdriver and Nunit – Part 3
Setting up Specflow
Now I am already assuming that Selenium Webdriver and Nunit is already set up in your Visual Studio project. If not you can learn how to do this at https://trickydefects.com/index.php/webdriver-essentials-c.html
Get the latest Nuget Package for specflow and then get the visual studio integration from Visual Studio Gallery. You can go to https://visualstudiogallery.msdn.microsoft.com/9915524d-7fb0-43c3-bb3c-a8a14fbd40ee and install it. You should have the Nunit Nuget package and the Specflow.Nunit Nuget package making your config file look something like this
After completing the install, when you try to ‘Add New Items’ in Visual Studio you will notice some new additions including ‘Add new feature file’. Select this option and you will see a file added to your project with the extenstion .feature.
Give your feature file a suitable name for the user story for example we have a login feature file underneath and the BDD tests are listed with a scenario name with the Gherkin syntax describing the tests.
What is now required is for the gherkin statements to be ‘binded’ to our Webdriver code in C#. What Specflow will do is create hooks for your framework. The way it does this is the feature file has a code behind .cs file that has been created by SpecFlow . Don’t worry too much about this as it is used to run the Scenarios described in the feature file and implemented in the binding step definitions. The Step definitions are where we actually create the code that run the Nunit tests. These step definitions will have a [binding] attribute. Simply right click on your BDD Test scenario which you have yet generate any step definitions for and select the menu option ‘Generate step definitions’.
This should bring up a dialogue box that will allow you to copy the method stubs as above.
And then paste them into your code. The method stubs will look like this when you paste them:
We now simply need to enter the Webdriver code into our bindings. Once you have written your webdriver code into these stub methods, rebuild our project and simply open up the automated tests dll using the Nunit Test Runner. We should see our feature file BDD tests in our Nunit runner ready to run and we can simply run them as per below:
……..and that is how we run Specflow with Selenium Webdriver and Nunit