There are a bunch of basic quality checks that should happen on every web page that goes live and they are tedious to do manually. I am working on a few helpers to integrate these checks into an xUnit test suite for projects:
[TestFixture]
public class ExampleTests
{
static readonly TimeSpan maxResponseTime = TimeSpan.FromSeconds(5);
const int maxHtmlSize = 51200;
const int maxViewState = 4096;
[RowTest]
[Row("http://www.microsoft.com/")]
[Row("http://www.asp.net/")]
[Row("http://www.myspace.com/")]
public void BasicWebPageQualityChecks(string url)
{
string html = null;
TimeSpan responseTime = TimeTakenToExecute(() =>
{
html = GetResponse(url);
});
Assert.That(responseTime, Is.LessThan(maxResponseTime));
Assert.That(html.Length, Is.LessThan(maxHtmlSize));
Assert.That(html, HasViewState.LessThan(maxViewState));
Assert.That(html, Validates.AsXHtml10Transitional);
}
}
I have used the RowTest extension for NUnit as an example of how to test a number of URLs with as little fuss as possible. The test is intended to sit alongside functional Watin style tests and be a basic check to stop developers handing rubbish off to QA. There are lots of ways to structure the test but keep the following in mind:
- TimeTakenToExecute takes an Action so that you can a larger block of code or another function to it. This makes it a nice generic timing helper for tests in general.
- GetResponse currently returns the response from a standard .Net WebRequest. Reading the HTML back from a Watin session may be interesting as well though less generic.
- The ViewState is read from the html using a Regex which makes it a little less fragile with invalid HTML. I need to figure out the exact set of characters that are legal in view state and tweak the pattern.
- The W3 validation constraint is actually calling the live W3 validator API and returning the errors it gives. It only validates HTML at the moment though there is a possibility of adding checks such as CSS, accessibility and mobility in future.
- As mentioned in their documentation I need to install my own instance of the W3 validator to avoid abusing it and being blocked (again).
I'm interested to hear from you if you have any feedback or suggestions for similar things you'd like to be able to check. I intend to push this out as a simple .dll you can link against where needed.
Labels: ASP.Net, TDD
15/08/2008 05:14 AM (UTC -07:00)