Extreme ASP.NET Makeover: Testing - Acceptance Tests
- Posted: Jun 04, 2009 at 11:58 PM
- 10,738 Views
Right click “Save as…”
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.
· Eliminating Repetition from Tests
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?