Friday, June 26, 2015

Write your 1st Test with Specflow and Selenium WebDriver

Introduction

Recently I've been asked by a blogger how can I use SpecFlow to write a test in Visual Studio. 

"I am a beginner in test automation and I try Cucumber -> Specflow -> Selenium Webdriver . If you have experience with such tools, an article would be useful. It is not clear for me how "generic operations" (picking up a certain browser, opening / closing a browser session etc) can be embedded into a Specflow feature file (hooks maybe?)"

In this article I'm going to explain how you can create your first test with SpecFlow and Selenium WebDriver, by using TDD and Page Object Model.

Install IDE integration

For using SpecFlow it is recommended to install the IDE integration. I use Visual Studio 2013, so I downloaded this IDE from this location: https://visualstudiogallery.msdn.microsoft.com/90ac3587-7466-4155-b591-2cd4cc4401bc.
If you are using different versions of Visual Studio, visit SpecFlow page about IDE installation.

Setup First SpecFlow Project

Before starting, I'd like to start with recommending to have:
- one project with all SpecFlow tests, let's call it SpecFlowTests 
- another project that will contain SpecFlow page objects, let's call it SpecFlowPages, that will contain the methods that will access the browser objects by using Selenium WebDriver
I'm not going to talk about Page Objects and Selenium in this article, but there is a very useful presentation on PluralSight that you can watch it: http://www.pluralsight.com/courses/automated-testing-framework-selenium.

Setup Project using NuGet

For this demonstration I'm going to use MStest. Ready? Let's start!

1. Open Visual Studio and create a new TestProject-->Unit Test Project and name it SpecFlowTests.



2. Remove UnitTest1.cs from Solution Explorer, cause you are not going to need it.



3. In Solution Explorer --> SpecFlowTests project --> References right-click and select Manage NuGet Packages. Make sure you selected Online option from the left part and then in the search area enter SpecFlow.



4. You will notice that an App.config file was generated in the structure of the project. Because we chosen to use MSTest to execute out tests, we need to do a configuration in this file.



5. Now we can build our solution and we are ready to add our first test. The test that I'll perform is this:
- Open http://www.specflow.org/ page
- Hover over SpecFlow+ link text
- Click SpecFlow+ Excel on link text
- Check Get Started Now with SpecFlow+ Excel! if the button is available 

6. Create a new SpecFlow feature by right-clicking on the project name --> Add --> New Item --> Visual C# Items --> SpecFlow Feature File. Name the feature --> NavigateSpecFlowExcel.



7. In the feature description we should enter the feature that we have to test and the scenario in Gherkin language. These features, represents in fact acceptance criteria of the application (use cases, user stories). For more information about this language, please access this page. For our example, please enter this feature:



8. Now that we defined the scenario we should Generate the step definitions. For this, right-click somewhere on the feature file and select Generate Step Definitions



9. You will see a dialog for generating the Class for all steps. Generate it




10. Choose the location where to store this class. It should be in the same project.  A new .cs file will be generated with a default content.



11. We need now to implement the methods for the scenarios. We can TDD this and start writing the methods like this.



12. Some remarks about this
- if you will build now the solution, it will normally fail, because there are some classes that should be implemented
- [BeforeScenario] and [AfterScenario] are two attributes, used to say that those actions should be run before and after a scenario/test
- we should implement Driver class that is used for setting the browser to work with via selenium, navigation to the page we want to use, etc
ConstantsUtils class, is a class where we store constants used in the application like, the url page, strings etc.
NavigateToSpecFlowExcel class, is the class used to navigate to the desired page, identifying objects with Selenium etc.

All these classes should be separated from the tests itself, so let's create a new project SpecFlowPages, with these 3 new classes.
- right-click the solution and Add --> New Project -->SpecFlowPages
- remove UnitTest.cs, cause we don't need it
- add 3 classes, which should be public classes: Driver, ConstantsUtils and NavigateToSpecFlowExcel 
- in the SpecFlowTests project, add in References --> right-click --> Add References --> Projects --> select project SpecFlowPages. Now, the classes will be referenced in our test and your binding should look like this:



- in the SpecFlowPages project, add Selenium WebDriver references with NuGet: right-click  References --> Manage NuGet Packages --> Online --> search for selenium and install Selenium WebDriver and Selenium WebDriver Support Classes



- we need to implement the classes with they methods. To do this you can go to NavigateSpecfFlowExcelSteps.cs and click on the method that is not implemented (red) and click ALT+Enter. This will add a new method on the correct class. You should do this for every method, property that is not implemented


- The structure of the project should be like in the image below:



13. Finally, the Driver class should look like this:



14. Finally, the ConstantUtils class should look like this:



15. Finally, the NavigateToSpecFlowExcelclass should look like this:



16. One more thing you have to do is to add the reference Selenium WebDriver under SpecFlowTests project, because this is used in [BeforeScenario]/[AfterScenario] attributes. Also, if you want to use ChromeDriver, instead FirefoxDriver to run your test, you need to add ChromeDriver into your SpecFlowPages project. To do this go to Referecens --> right click --> Manage NuGet Packages --> Online --> search for ChromeDriver




Run Tests

Now we are ready to run our test. There are different ways to do this
1. Open NavigateSpecfFlowExcel.feature file, right-click somewhere and select Run SpecFlow Scenarios



2. Run the test from Test Explorer



Some Refactoring

There is still a better way to organize our solution.

1. In order to avoid writing for each test/scenario [BeforeScenario] and [AfterScenario] actions that should be done before/after each test, we can extract the information into a different class, called Start, that will inherit Driver class. Also, all binding tests, will inherit Start class.



2. We can even organize the structure of our files better like below:




... and the Source files

For a better understanding of this example, I've uploaded the project under GitHub, so you can download it from here: https://github.com/suditur/SpecFlow_Selenium_TDD_PageObject_Example.git





I hope you enjoyed this article!
Want to lean more about BDD? Have a look on this book: BDD in Action: Behavior-driven development for the whole software lifecycle

Happy testing and... make it green, becomes a dream :).

Popular Posts