Acceptance Tests
Acceptance tests using WatiN are simply longer versions of smoke tests. The following shows an acceptance test to verify that we can create a new page in our wiki.
[Test]
public void CanCreateNewWikiPage() {
string pageName = string.Format("TestPageName-{0}", Guid.NewGuid());
const string pageTitle = "TestPageTitle";
using(var browser = new IE()) {
browser.GoTo(PageUrl.Default);
browser.LoginAsAdmin();
browser.CreateWikiPage(pageName, pageTitle);
Assert.That(browser.WikiPageTitle(), Is.EqualTo(pageTitle));
Assert.That(browser.Url, Text.Contains(pageName));
}
}
I created an extension method BrowserExtensions.CreateWikiPage to encapsulate the logic of creating a new page.
public static void CreateWikiPage(this Browser browser, string pageName, string pageTitle) {
browser.Link(Find.ByTitle("Create a new Page")).Click();
browser.TextField(Find.ByTitle("Type here the name of the page")).TypeText(pageName);
browser.TextField(Find.ByTitle("Type here the title of the page")).TypeText(pageTitle);
browser.Button(Find.ByValue("Save")).Click();
}
Another extension method simplifies finding a wiki page's title; not the contents of the <title/> tag, but the contents of the <h1 class="pagetitle"/> tag.
public static string WikiPageTitle(this Browser browser) {
return browser.ElementWithTag("h1", Find.ByClass("pagetitle")).Text.Trim();
}
As we write more acceptance tests, the BrowserExtensions class may become unwieldy in size. If this happens, I can easily refactor the code into BrowserLookupExtensions (for finding information in the page) and BrowserScriptingExtensions (for manipulating the page). I could separate extension methods by public and admin, particular function, or any other means to make the code more tractable.
Other videos from this article
· Of Tightropes and Tests
· Using WatiN
· Eliminating Repetition from Tests
· Acceptance Tests