The idea of the framework is as the following. A web application root is created
in a temp directory. All currently referenced and loaded assemblies are copied
to the /bin folder. Starting from this point, HttpRuntime.ProcessRequest() is
able to process any requests, compiling aspx, themes, master pages, etc. if
necessary.
Few words about the API.
WebTest is the central class of the framework. It's instances are typically
created in Nunit testcases. WebTest instances carry the information from the
testcase appdomain into the web application appdomain, and back. The most
important properties of WebTest are Request and Invoker. The request carries all
the information, necessary to create an HttpWorkerRequest in the web appdomain.
The invoker carries all the callbacks which have to be invoked to perform the
tests in the web appdomain. Here is an example of using the WebTest, Request,
and Invoker.
[Test]
public void TestCase1 ()
{
WebTest t = new WebTest ();
t.Invoker = PageInvoker.CreateOnLoad (TestCase1OnLoad));
t.Request.Url = "MyPage.aspx";
string htmlRes = t.Run();
//HtmlDiff on htmlRes ...
}
static public void TestCase1OnLoad (Page p) //invoked in the web appdomain
{
Assert.AreEqual ("White", p.StyleSheetTheme);
}
There is a support for postback. The flow goes like this: you make a first
request like:
WebTest t = new WebTest ("SomePage.aspx");
t.Run ();
Then you use the response to create a FormRequest:
FormRequest f = new FormRequest (t.Response);
This will parse the response, and use action URL, GET/POST method and all
hidden fields (VIEWSTATE, etc.) You might add more query parameters, like
f.Controls.Add (new BaseControl ("button1", ""));
f.Controls.Add (new BaseControl ("textbox1", "some text");
and run the second request:
t.Request = f;
t.Run ();
It can be useful to install some callbacks for the second request, before
calling to t.Run(), like the following:
t.Invoker = PageInvoker.CreateOnLoad (MyDelegate);
the delegate for the above test might look like:
t.Invoker = PageInvoker.CreateOnLoad (MyDelegate);
static public void MyDelegate (Page p)
{
Assert.IsTrue (p.IsPostBack);
Assert.AreEqual ("some text", ((TextControl)p.FindControl ("textbox1")).Text);
}