Features
- Doesn't need a Web server running
- You can issue a GET request and obtain a reference to the returned page. You are able to check the control properties etc.
- You can Mock external services or your own objects, set expectations etc. before requesting a page. The simplest possible way is to use TypeMock Isolator.
- You can set request headers and cookies, verify the response headers, cookies, status code and description, redirect url, raw HTML output.
- You can add event handlers for the page events. So, for example, you can set asserts during the page lifecycle, dynamically load controls or call other methods. You can test that a particular variable equals to "A" during Page_Init, and equals to "B" during Page_Prerender.
- You can issue postbacks, and you can modify the control data before the postback just like in a Winform application. For example, request a page, simulate entering "John" into the appropriate textbox, simulate clicking a button, get the page, verify that a cetrain label's text is "Hello John".
- A few control helpers to make life easier, including GridViewHelper and DetailsViewHelper.
- Possible to test any IHttpHandler -- not just a page.
Limitations and known issues
- Slow startup time for each fixture.
- Can't test autopostbacks and Value_Changed events (they are not fired).
- Cross page postbacks are not supported.
- AJAX and async processing is not supported (AJAX controls can appear in the tested page, but the AJAX functionality doesn't work).
- Web parts are not supported.
- Testing FileUpload controls is not implemented.
- Mocking the HttpContext class is not possible.
- Doesn't work correctly with pages that handle the OnError event, because it is handled in the framework code.
Sample test
/// <summary>
/// We enter the name into a textbox and verify that after the postback the HelloLabel's text is "Hello ..."
/// </summary>
[Test] public void HelloTest() {
TestSession session = new TestSession();
Page testPage = session.GetPage("Default.aspx"); //We first GET our page
TextBox NameTextbox = (TextBox) testPage.FindControl("NameTextbox"); //Find the textbox
NameTextbox.Text = "John"; //User enters some data
testPage = session.ProcessPostback("HelloButton"); // That's how we process postbacks
Label label = (Label) testPage.FindControl("HelloLabel");
Assert.AreEqual("Hello John", label.Text);
}