Aligned with another jam session at Ann Arbor's Come Jam With Us is another installment of Learn to Code, this time providing an introduction to WatiN, or Web Application Testing in .NET. The jam session was held at the offices of SRT Solutions in Ann Arbor, Michigan, at 5:30p, Tuesday April 6th. Though thunderstorms were in the forecast, the predicted high was 72°F (22°C), so we weren't bothered by the same 8" of fluffy white snow that caused cancellations and delays during the my session on ASP.NET MVC 2. But for those that couldn't make the WatiN jam session, might I recommend the exercise below.
This coding exercise is designed to give you an introduction to browser-based testing using the WatiN framework, or Web Application Testing in .NET. The framework allows developers to create integration tests (using a unit testing framework like MbUnit, NUnit, or MSTest) to test and assert their application within a browser window. The framework interacts with the browser DOM much like and end-user, producing reliable results that mimic the real world. In this sample, we will write a few WatiN tests against the Google search engine.
To complete this exercise, you will need to meet or complete a few prerequisites. Please complete these prerequisites before moving on. The session is designed to be completed in about an hour, but setup and prerequisites are not included in that time.
WatiN is generally used within the context of a unit testing framework. For this exercise, we will be using a Visual Studio Test Project and MSTest to wrap our WatiN code.
In our first test, we will use the project we just created to test Google's home page. After accessing http://www.google.com, we will check a few properties of the browser and a few loaded elements to ensure that the expected page was returned. The first thing we will need is a new Unit Test class to start our testing.
using Microsoft.VisualStudio.TestTools.UnitTesting; namespace WatinSample { [TestClass] public class WhenViewingTheGoogleHomePage { } }
Now that we have a test class, we can start writing WatiN code. Each of our tests will first need a Browser object to test against. Using methods attributed with TestInitialize and TestCleanup, we can create a browser instance before the test starts and shut it down when the test is complete.
Creating an instance of a browser in WatiN is easy: simply create a new instance of the IE class, passing in a URL. We can assign this new class to a field of type Browser, which is a base class of all browser classes in WatiN. Currently, WatiN supports Internet Explorer and Firefox.
using Microsoft.VisualStudio.TestTools.UnitTesting; using WatiN.Core; namespace WatinSample { [TestClass] public class WhenViewingTheGoogleHomePage { Browser browserInstance; [TestInitialize] public void WithAnInstanceOfTheBrowser() { browserInstance = new IE("http://www.google.com"); } [TestCleanup] public void ShutdownBrowserWhenDone() { browserInstance.Close(); browserInstance = null; } } }
There are three prominent items on the Google home page: the Google logo, the search criteria text box, and the search button. Using WatiN, we can check for them all. The WatiN Browser object contains an Elements collection, which is a flattened collection of every element in the entire DOM. Like any collection, you can use Linq and lambda expressions to search for items within this collection. Alternately, you may also use the Element method, which accepts the same lambda expression that would be used within the Where extension method on the collection, and returns the first or default element. For more specific searches, WatiN's Browser object includes similar collections and methods for searching explicitly for Images (<IMG>), Paras (<P>), Text Fields (<INPUT type="text" />), and so on.
On each returned Element (or derived Para, Image, or Text Field, etc., all of which inherit from Element), WatiN supplies properties for accessing the CSS Class, Id, InnerHtml, Name, Tag, Text, Value, or many other attributes. The method GetAttributeValue(string attributeName) is provided for accessing other attributes that are not explicitly defined on the object (uncommon attributes and custom attributes). Finally, elements also contain a Style property, which not only gives access to the inline style attribute, but also any CSS properties associated with the element from Internal Style (in the Page Head) or External Style (in an external style sheet).
On to checking for the three elements within the Google home page: the logo, the criteria input, and the search button. First, check for the existence of the Google logo graphic. The image can be found by searching the DOM for an image with an Id of "logo". WatiN works very closely with lambda expressions, so we can use these to help us find out graphic.
[TestMethod] public void PageShouldContainGoogleLogo() { Image googleLogo; googleLogo = browserInstance.Image(img => img.Id == "logo"); Assert.IsTrue(googleLogo.Exists); Assert.AreEqual("Google", googleLogo.Alt); }
Next, check for the existence of the search criteria input box. WatiN refers to these elements as Text Fields, using the TextField type. Additionally, this form field is identified by its Name rather than its Id. In Google, the name given to the criteria input is "q".
[TestMethod] public void PageShouldContainSearchCriteriaInput() { TextField criteriaInput; criteriaInput = browserInstance.TextField(tf => tf.Name == "q"); Assert.IsTrue(criteriaInput.Exists); }
Finally, check for the existence of the search button using the Button method. In our lambda expression, it is not important to know if the field is identified by a Name property or an Id attribute, as WatiN supplies a IdOrName property to help us find the element. The value to identify the button is "btnG".
[TestMethod] public void PageShouldContainSearchButton() { Button searchButton; searchButton = browserInstance.Button(btn => btn.IdOrName == "btnG"); Assert.IsTrue(searchButton.Exists); Assert.AreEqual("Google Search", searchButton.Value); }
WatiN can access properties on the DOM beyond just Text values and Alt attributes. WatiN also has full access to the style that CSS has applied to an element. Let's check out a few CSS properties, both those explicitly defined by WatiN and those implicitly accessible through the WatiN framework.
For our first style check, we'll take a look at the default font family used on the Google Home Page. Font Family is one of the explicitly available style properties on a WatiN element. Some others, like Color, Display, and Height are also explicitly defined.
[TestMethod] public void BodyShouldUseArialFontFamily() { Assert.AreEqual("arial, sans-serif", browserInstance.Body.Style.FontFamily); }
For our second style check, we will look for an implicit style definition. At the top of the Google Home Page is a series of links to other areas of Google, such as Images, Videos, Maps, and News. At the end of this list is a More link, that when clicked, displays a hidden DIV tag containing even more links, such as Books, Finance, and Google Translate. Since we do not have any code in our test initialization that interacts with the browser, and thus nothing that is clicking the More link, that DIV should still have a hidden visibility. However, since Visibility isn't an explicitly defined style property within WatiN, we need to use the GetAttributeValue method to retrieve the current visibility setting.
[TestMethod] public void MoreItemsShouldNotBeVisibleOnPageLoad() { var googleBarMoreItems = browserInstance.Div(gbi => gbi.Id == "gbi"); Assert.AreEqual("hidden", googleBarMoreItems.Style.GetAttributeValue("visibility")); }
Browser Integration tests are more than just loading a page and checking a few element attributes. Our tests may also need to enter values into form fields, click links and buttons, or interact with browser navigation like the back button. WatiN fully supports all of these features in a very intuitive fashion.
Create a new test class, similar to what we did in Exercise 1, calling the new test class WhenViewingGoogleSearchResultsForComeJamWithUs. Also add in the TestInitialize and TestCleanup methods that open and close the browser. However, this time, after we load http://www.google.com, enter a value into the search criteria input and then click the Google Search button.
using Microsoft.VisualStudio.TestTools.UnitTesting; using WatiN.Core; namespace WatinSample { [TestClass] public class WhenViewingGoogleSearchResultsForComeJamWithUs { Browser browserInstance; [TestInitialize] public void WithAnInstanceOfTheBrowserSearchingGoogle() { browserInstance = new IE(@"http://www.google.com"); TextField criteria = browserInstance.TextField(tf => tf.Name == "q"); criteria.Value = "Come Jam With Us"; Button search = browserInstance.Button(btn => btn.IdOrName == "btnG"); search.Click(); } [TestCleanup] public void ShutdownBrowserWhenDone() { browserInstance.Close(); browserInstance = null; } } }
With this code, or initialized test will load the Google Home Page and will conduct a search for "Come Jam With Us".
For our first verification, let's check the URL for the browser window. The search result URL should contain the search criteria in the URL's query string; we can validate this using the URL property on our instance of the Browser object.
[TestMethod] public void BrowserUrlShouldContainSearchCriteria() { Assert.IsTrue(browserInstance.Url.Contains(@"q=Come+Jam+With+Us")); }
With WatiN, we are not just limited to searching for items directly from the Browser object. We can also search for child elements directly from their parent element or any ancestor element. Our search results should contain a search result item linking to the Come Jam With Us web site. The Google Results page contains a DIV identified as "res" that serves as a container for all search result information. Rather than checking that our Come Jam With Us link exists somewhere on the page, we should search for it directly within the results DIV.
[TestMethod] public void ResultsShouldContainLinkToComeJamWithUs() { Link comeJamWithUs; Div searchResults = browserInstance.Div(div => div.IdOrName == "res"); comeJamWithUs = searchResults.Link(link => link.Url == @"http://www.comejamwithus.org/"); Assert.IsTrue(comeJamWithUs.Exists); }
An element may contain many child elements. An anchor tag—<A href="#">—can contain text, and child elements may make portions of that text bold, italic, underlined, or even bright red. Through WatiN, we can access that inner content as straight text without the formatting, or as the InnerHtml including all of the child elements.
[TestMethod] public void ResultsLinkContainsComeJamWithUsText() { Link comeJamWithUs; Div searchResults = browserInstance.Div(div => div.IdOrName == "res"); comeJamWithUs = searchResults.Link(link => link.Url == @"http://www.comejamwithus.org/"); Assert.AreEqual(@"Come Jam with us (Software Development Study Group)", comeJamWithUs.Text); } [TestMethod] public void ResultsLinkContainsComeJamWithUsHtml() { Link comeJamWithUs; Div searchResults = browserInstance.Div(div => div.IdOrName == "res"); comeJamWithUs = searchResults.Link(link => link.Url == @"http://www.comejamwithus.org/"); Assert.AreEqual( @"<EM>Come Jam with us</EM> (Software Development Study Group)", comeJamWithUs.InnerHtml); }
As previously mentioned, we can also fully interact with the browser, itself. Our test initialization started from the Google Home Page and performed a search. Using functionality built in to WatiN, we can execute the browser's back navigation to return to the previous page.
For our next test, execute a back navigation and verify that the browser's URL matches http://www.google.com/.
[TestMethod] public void PageShouldHaveComeFromGoogleDotCom() { string previousUrl; browserInstance.Back(); previousUrl = browserInstance.Url; Assert.AreEqual(@"http://www.google.com/", previousUrl); }
Some interactions on a page cause element properties to change. An example of this is the More link from Exercise 1; when the end-user clicks the More link, the More Items DIV appears because the link's click event changes the Visibility style property of the DIV to visible. For our final test, we will use what we have learned to test this functionality.
[TestMethod] public void MoreItemsShouldBeVisibleOnMoreLinkClick() { var googleBar = browserInstance.Div(gbar => gbar.Id == "gbar"); var googleBarMoreItems = googleBar.Div(gbi => gbi.Id == "gbi"); Assert.AreEqual("hidden", googleBarMoreItems.Style.GetAttributeValue("visibility")); var googleBarMoreLink = googleBar.Link(link => link.ClassName.Contains("gb3")); googleBarMoreLink.Click(); Assert.AreEqual("visible", googleBarMoreItems.Style.GetAttributeValue("visibility")); }
Now that we have spent some time on basic properties, interactions, and style sheets within the WatiN framework, hopefully you can apply this to your own application and get started with your own browser-based integration tests. If you would like more information, I encourage you to check out the WatiN site at http://watin.sourceforge.net. And as always, if you have any questions, drop me a line.
Remember Me