ASP.NET View Testing (MVC)
Posted On: January 1st, 2009There comes a time in our lives where front-end testing is a must for our web applications…
I have recently been introduced to a company by the name of ArtOfTest a Microsoft Gold Certified Partner and for a good reason. The company has two products: “Automation Design Canvas” and the “WebAii” Framework, both incredibly useful tools.
The one I am going to write and sample today is the WebAii Framework, available as a free download from: ArtOfTest WebAii.
Overview:
This light weight testing framework is perfect for writing tests in NUnit or Visual Studio Team Test because it seamlessly integrates with your current project and really doesn’t take any setup due to the provided Visual Studio Templates that come with the installer. All you need to do is write the tests.
So let’s dive into some quick code snippets from the website:
Ajax JavaScript Automation Testing:
[TestMethod] //Define your test method (VS Test)
public void AjaxJsAutomation()
{
// Wait for a specific element with id 'containing' foo to exist
Element e = new Element(new FindParam("id=~foo"), ActiveBrowser);
e.Wait.ForExists();
// Convert the generic element to an HtmlDiv
HtmlDiv myDiv = e.As<HtmlDiv>();
// Now invoke a javascript function on my page directly.
Actions.InvokeScript("myJsFunction('param')");
// Wait for a color "computed" style to become red
myDiv.Wait.ForStyles(true, "color=red");
// Invoke OnFocus event
myDiv.InvokeEvent(ScriptEventType.OnFocus);
// Wait for the div to be invisible.
myDiv.Wait.ForVisibleNot();
// Move the mouse over the div
myDiv.MouseHover();
//Write assertion code here.
}
This is probably one of the cooler features of WebAii. What this code is doing is pulling an element with an id of “~foo” as soon as it exists. Then after the item exists this code is invoking a javascript function and actually waiting for the javascript to change data on your page.
Asp.NET Testing:
[TestMethod] //Define your test method (VS Test)
public void AspNetTesting()
{
// Set the local server to be Asp.Net local server (aka cassini)
Manager.Settings.LocalWebServer = LocalWebServerType.AspNetDevelopmentServer;
Manager.Settings.WebAppPhysicalPath = @"C:\myAspNetApp\Test";
// Launch a new browser
Manager.LaunchNewBrowser();
// Navigate to a page in your app.
ActiveBrowser.NavigateTo("/main.aspx");
// Get the grid using partial id.
Element grid = Find.ById("~dataGrid1");
//Write assertion code here.
}
As you can see testing asp.NET applications is as easy as setting up the local web server and web application physical path. The rest as usual, launch the web browser via the Manager, get/set your elements and then run your assertions.
So I should probably get back to writing my tests at work but there is a lot more goodness in WebAii and I encourage you to take a look at it when you get a chance and the best place to start is the documentation: Web Aii Docs


[...] to VoteASP.NET View Testing (MVC) (2/25/2009)Wednesday, February 25, 2009 from Andrew KharlamovThere comes a time in our lives where front-end [...]
ASP.NET MVC Archived Blog Posts, Page 1 (February 27th, 2009)
Very nice!
John865 (May 29th, 2009)
Privet Andrei (just being cheeky here),
Thanks for this write-up! It’s exactly what I’ve been looking for to separate my test environment from my development environment. I knew it had to be simple but I couldn’t get it figured out.
Best Regards,
Erik
Erik (March 16th, 2010)
[...] “Automation Design Canvas” and the “WebAii” Framework, both incredibly useful tools…. [full post] Andrew Kharlamov CowFarm.NET development 0 0 0 0 0 [...]
ASP.NET View Testing (MVC) (December 27th, 2010)