Automated UI Test With WatiN and Specflow: Part 2

Before proceeding further have a look at my previous article:

 
SpecFlow

“SpecFlow aims at bridging the communication gap among domain experts and developers by binding business readable behavior specifications and examples to the underlying implementation.” – http://www.specflow.org/getting-started/.

In other words, we write text describing our scenario, how it should work then list the functionalities and the behavior. The specflow will generate a little class where we can put some code to test each step described in this text.

Create a new Project



Installing SpecFlow

In the project test open Nuget and find “SpecFlow”, install the packages “SpecFlow” and “SpecFlow.NUnit” (this one is if you use NUnit).



Download and install this extension https://visualstudiogallery.msdn.microsoft.com/90ac3587-7466-4155-b591-2cd4cc4401bc. It enables some file templates in the project.

Open app.config and change unitTestProvider to MsTest (I prefer) or NUnit if you use NUnit.





Testing

Now we will create a new test class like as in the following procedure.

Create a new SpecFlow feature file.





The template creates a very simple example that describes perfectly the main idea of the SpecFlow. It just specifies, by texting, how a simple add operation must work.

Adjust our feature to test our Links at the main menu.

 

  1. Feature: SampleSpecflowFeature  
  2.   
  3. The main menu is a central of essencial links of page.  
  4.   
  5. All link must be working.  
  6.   
  7. @mytag  
  8.   
  9. Scenario: Links of main menu  
  10.   
  11. Given Menu have four links  
  12.   
  13. When I click in some link  
  14.   
  15. Then the result should be a new page with title "named" as the link was clicked  

 

Note: “named”, here I define a parameter to this step.

To Generate SpecFlow step definitions, in other words our class to insert code test, right-click and “Generate Step Definitions”:







Now we can test our scenario using WatiN the same as we use in Part 1.

 

  1. [Binding]  
  2.   
  3. public class SampleSpecflowFeatureSteps  
  4.   
  5. {  
  6.   
  7.     IE browser = new IE("http://localhost/WatinSpecflowSample/Watin"true);  
  8.   
  9.     List < string > listOfPages = new List < string > ();  
  10.   
  11.     [Given(@  
  12.     "Menu have four links")]  
  13.   
  14.     public void GivenMenuHaveFourLinks()  
  15.   
  16.     {  
  17.   
  18.         int count = 0;  
  19.   
  20.         IElementContainer elems = (IElementContainer) browser.Element(Find.ById("mainMenu"));  
  21.   
  22.         foreach(Element element in elems.Links)  
  23.   
  24.         {  
  25.   
  26.             listOfPages.Add(element.InnerHtml);  
  27.   
  28.             count++;  
  29.   
  30.         }  
  31.   
  32.         Assert.AreEqual(4, count);  
  33.   
  34.     }  
  35.   
  36.     [When(@  
  37.     "I click in "  
  38.     "(.*)"  
  39.     " link")]  
  40.   
  41.     public void WhenIClickInLink(string p0)  
  42.   
  43.     {  
  44.   
  45.         //get link page1 and click  
  46.   
  47.         browser.Link(p0.ToLower()).Click();  
  48.   
  49.         //wait for page open  
  50.   
  51.         browser.WaitForComplete();  
  52.   
  53.     }  
  54.   
  55.     [Then(@  
  56.     "the result should be a new page with title "  
  57.     "(.*)"  
  58.     "")]  
  59.   
  60.     public void ThenTheResultShouldBeANewPageWithTitle(string p0)  
  61.   
  62.     {  
  63.   
  64.         // getting title of page using the element ID  
  65.   
  66.         string title = browser.ElementWithTag("h2", Find.ById("title"), null).Text;  
  67.   
  68.         //Checking if page have de title Index  
  69.   
  70.         Assert.AreEqual(title, p0);  
  71.   
  72.         //backing for index  
  73.   
  74.         browser.Back();  
  75.   
  76.         browser.WaitForComplete();  
  77.   
  78.     }  
  79.   
  80.     [AfterScenario]  
  81.   
  82.     public void Cleanup()  
  83.   
  84.     {  
  85.   
  86.         //Closing the browser  
  87.   
  88.         browser.Close();  
  89.   
  90.     }  
  91.   
  92. }  

 

To run our test go to the feature at Solution Explorer, right-click, Run SpecFlow Scenarios.



Debugging our Feature





Results of Test

We can see that the test failed because a Page3 doesn't exist in our solution, as expected.



Conclusion

It was possible to note that the SpecFlow makes the test more understandable and reduces the gap among the developer and business users. It uses a simple text that is easy to interpret that is commonly understandable by everyone. This type of test is commonly called Behavior Test and it's used in the software development process called behavior-driven development.

I hope I've helped you guys to understand a bit more about testing showing these two tools SpecFlow and WatiN. Send a comment for any feedback or questions.

The complete code you can get from https://github.com/mfandre/WatinSpecflowSample
Thanks!

Up Next
    Ebook Download
    View all
    Learn
    View all