Click here to Skip to main content
15,886,512 members
Articles / Web Development / ASP.NET

Running UI Tests on Local ASP.NET Website with Development Server

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Jun 2012CPOL2 min read 8K   2  
How to run UI tests on local ASP.NET website with development server

These past couple of days, I started writing a utility program that will invoke some UI tests against a certain website of my choice. Obviously, I needed to write tests for the functionality of this utility, so I will be sure that it works. I wanted the tests to be as real as possible, and so I decided to run them against a real site, like Google (could've been any other site, this doesn't really matter for the sake of the story).

The tests worked great, but then I realized that they could easily be broken, with no connection to my work - No one promises me that Google's page structure (or anything I want to test) will always stay the same, and I don't even know which tests I'll want to add to it in the future, and if I'll have all the resources on the site of my choice.

I decided that I'll create a custom ASP.NET MVC site for the sake of my tests. At first, I configured the site on IIS locally and this was great, but then I started thinking another step forward, and realized I have another problem - I want someone else to be able to download the source code, and run the tests immediately without having to create a local website and going through all the proper configurations.

I realized that I could use the ASP.NET development server for this.

First, I needed to give the site a static port to run on in the development server:

(In the project properties page, go to the 'Web' tab, and mark 'Use Visual Studio Development Server' -> 'Specific Port')

Then, I created a class called 'DevelopmentServer' responsible for loading the ASP.NET development server, and shutting it down at the end:

C#
public class DevelopmentServer : IDisposable
{
    private readonly Process _devServer;

    public static int Port = 1212;
    private string _devServerExe = 
      @"C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE";
    private string _testSitePath = @"C:\Dev\MySitePath\";

    public DevelopmentServer()
    {
        _devServer = new Process {
            StartInfo = { 
                FileName = _devServerExe,
                Arguments = string.Format("/port:{0} /path:{1}", Port, _testSitePath)
            }
        };

        try
        {
            _devServer.Start();
        }
        catch
        {
            Console.WriteLine("Unable to start development server");
            throw;
        }
    }

    public void Dispose()
    {
        _devServer.Kill();
        _devServer.Dispose();
    }
}

The port number and the library paths are just hard coded for the sake of the example. It makes more sense to put them in some configuration file of your choice. (Note: You might have to change the path to the file WebDev.WebServer40.exe which is the binary of the development server, according to the version you have. The earlier version of this file is called WebDev.WebServer20.exe)

Finally, I just created an instance of this class upon FixtureSetUp, and disposed of it on FixtureTearDown.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
-- There are no messages in this forum --