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 ProjectInstalling 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.
- Feature: SampleSpecflowFeature
-
- The main menu is a central of essencial links of page.
-
- All link must be working.
-
- @mytag
-
- Scenario: Links of main menu
-
- Given Menu have four links
-
- When I click in some link
-
- 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.
- [Binding]
-
- public class SampleSpecflowFeatureSteps
-
- {
-
- IE browser = new IE("http://localhost/WatinSpecflowSample/Watin", true);
-
- List < string > listOfPages = new List < string > ();
-
- [Given(@
- "Menu have four links")]
-
- public void GivenMenuHaveFourLinks()
-
- {
-
- int count = 0;
-
- IElementContainer elems = (IElementContainer) browser.Element(Find.ById("mainMenu"));
-
- foreach(Element element in elems.Links)
-
- {
-
- listOfPages.Add(element.InnerHtml);
-
- count++;
-
- }
-
- Assert.AreEqual(4, count);
-
- }
-
- [When(@
- "I click in "
- "(.*)"
- " link")]
-
- public void WhenIClickInLink(string p0)
-
- {
-
-
-
- browser.Link(p0.ToLower()).Click();
-
-
-
- browser.WaitForComplete();
-
- }
-
- [Then(@
- "the result should be a new page with title "
- "(.*)"
- "")]
-
- public void ThenTheResultShouldBeANewPageWithTitle(string p0)
-
- {
-
-
-
- string title = browser.ElementWithTag("h2", Find.ById("title"), null).Text;
-
-
-
- Assert.AreEqual(title, p0);
-
-
-
- browser.Back();
-
- browser.WaitForComplete();
-
- }
-
- [AfterScenario]
-
- public void Cleanup()
-
- {
-
-
-
- browser.Close();
-
- }
-
- }
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!