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 :).

14 comments:

  1. Thanks for your post; selenium is most trusted automation tool to validate web application and browser. This tool provides precise and complete information about a software application or environment. Selenium Training in Chennai

    ReplyDelete
  2. Our editorial teams are helps to students for making and editing the assignments.The cover letter essays, application essay, resume essaysthesis writing serviceto be possible for graduates.The study related materials and tools are bought from here. The online thesis is helps to the global students for taking their writing documents.

    ReplyDelete
  3. This is a really nicely written tutorial, many thanks for making such an effort to share your knowledge. I'm moving from Ruby/Cucumber to Specflow and found this very helpful.

    ReplyDelete
  4. Thank u for your information. I read your shared information on selnium Topic.
    Selenium Online Training

    ReplyDelete
  5. Thank you very much. Exactly the one i was looking for and appreciate the detailed explanation.

    ReplyDelete
  6. Very Impressive article Raluca, crisp and clear . Appreciate your enlightenment for the software world.

    ReplyDelete
  7. Great and easy to follow the steps!

    ReplyDelete
  8. Very helpful materials, need help from you! How I can set priority of the two test case with different feature file.

    ReplyDelete
    Replies
    1. Hi, thank you.
      Here is a comment from http://www.ontestautomation.com/running-your-tests-in-a-specific-order/

      At the beginning of this post, I mentioned that in my current project, we use SpecFlow to specify our regression tests. We then execute our SpecFlow scenarios using NUnit as a test runner, so we can leverage this alphabetical test order ‘trick’ by naming our SpecFlow scenarios alphabetically inside a specific feature. This gives us a way to control the order in which our scenarios are executed:

      ?
      Scenario: 01 Create test data
      Given ...
      When ...
      Then ...

      Scenario: 02 Modify data
      Given ...
      When ...
      Then ...

      Scenario: 03 Remove modified data
      Given ...
      When ...
      Then ...
      Again, it is always best to create your tests in such a way that they can be run independently. However, sometimes this just isn’t possible or practical. In those cases, you can employ one of the strategies listed in this post to control your test order execution.

      Delete
  9. We provide real time training for all trainees. So join our institute and get more knowledge about android training. Android Training in Chennai |
    Android Training in Chennai |
    Android Training in Chennai | Android Training in Chennai |

    ReplyDelete

Popular Posts