Running the Lightweight Test Automation Framework for ASP.NET from a separate application

Posted by Matthew Osborn on December 2, 2008

In some instances it might be best if your tests did not run in the same application as the website being tested.  This is where the “IApplicationPathFinder” interface comes into play.  If you implement this interface you will see that it only has one method “GetApplicationpath.”  What this does is allow you to specify a prefix of sorts for the navigate method.  When you call navigate on a HTMLPage it uses a ApplicationPathFinder to get the location it should look for the website at.  For example, the framework will implement this interface and simply return “http://localhost” by default.  Creating your own ApplicationPathFinder class allows you to specify where to look for the website, there is nothing stopping you from telling it to look for “http://foo.com.”  The fallowing code shows a simple implementation of the IApplicationPathFinder interface, that points to a second test application. This code is place in the DrivePage that is located in the “Test” folder.

<script runat="server">
    public class TestApplicationPathFinder : Microsoft.Web.Testing.Light.IApplicationPathFinder
    {
        public string GetApplicationPath()
        {
              return "http://localhost/TestApp2";
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        Microsoft.Web.Testing.Light.ServiceLocator.ApplicationPathFinder = new TestApplicationPathFinder();
    }
</script>

Okay, lets take a look at what is really going on here in the code.  First I choose to include the  implementation of the IApplicationPathFinder interface in the DriverPage, however, this could easily be moved to its own file/assembly.  Other than that the code is pretty straight forward, set the ApplicationPathFinder property on the ServiceLocator to an instance of your implementation.

There are numerous reason you might want to run the the test application separately from the site you are testing.  Here is a simple implementation that allows you to store your test application in a central location and pass the location of the site to be test to it.  You could use a URL like “http://localhost/Test?tag=localhost2&filter=true&path=http%3A%2F%2Flocalhost2&run=true” to the navigate and have the test page look for the site at “http://localhost2,” show only the test with the tag name “localhost2,” and run them once navigation is complete.  You could see how you could use this to set up a regression test bed, by storing all your tests in one location and pointing them to the proper servers for the pages.

<script runat="server">
    public class TestApplicationPathFinder : Microsoft.Web.Testing.Light.IApplicationPathFinder
    {
        public string Path { get; set; }

        public TestApplicationPathFinder(string path)
        {
            Path = path;
        }

        public string GetApplicationPath()
        {
            return Path;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        string path = this.Request.Params["path"];
        if(!string.IsNullOrEmpty(path))
            Microsoft.Web.Testing.Light.ServiceLocator.ApplicationPathFinder = new TestApplicationPathFinder(path);
    }
</script>

Hopefully this gives you some insight into the power that the Lightweight Test Automation Framework for ASP.NET has and how you can leverage that power.  You can download the framework here. Please let me know if you have any questions.