Starting WebDev Server Programmatically to Unit Test ASP.NET Pages

Yesterday, I was using WatiN to test some of my ASP.NET pages. I started by writing unit tests for the already created pages. I tried to run the unit tests but they froze. This was because the WebDev server (ASP.NET internal web server) was not running. The easy way is to start the server manually by running some command line tools or by running the ASP.NET application. But, this was not a good solution. So, I decided to start the WebDev server programmatically from the unit test project.

I set up some configurations in the App.config file for my unit test project:

<configuration>

  <appSettings>
    <add key="WebServerExePath" value="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe"/>
    <add key="Port" value="1950"/>
    <add key="WebApplicationPath" value="c:\projects\demowatiN\demowatiN"/>
    <add key="DefaultPageUrl" value="http://localhost:1950/Default.aspx"/>
   
  </appSettings>
 
</configuration>

And here is the BaseTestPage that runs the WebDev server.

 public class BaseTestPage
    {
        static Process server = null;

        static BaseTestPage()
        {
            if (Process.GetProcessesByName("WebDev.WebServer").Count() == 0)
            {
                string webServerExePath = (string)ConfigurationManager.AppSettings["WebServerExePath"];
                server = new Process();
                Process.Start(webServerExePath, GetWebServerArguments());
            }
        }

        public static string GetWebServerArguments()
        {
            string args = String.Format("/port:{0} /path:\"{1}\"",GetPort(),GetWebApplicationPath());
            if (String.IsNullOrEmpty(args)) throw new ArgumentNullException("Arguments is not defined");
            return args;
        }

        public static string GetPort()
        {
            string port = ConfigurationManager.AppSettings["Port"] as String;
            if (String.IsNullOrEmpty(port)) throw new ArgumentNullException("Port is null or empty");

            return port;
        }

        public static string GetWebApplicationPath()
        {
            string webApplicationPath = ConfigurationManager.AppSettings["WebApplicationPath"] as String;
            if (String.IsNullOrEmpty(webApplicationPath)) throw new ArgumentNullException("WebApplicationPath is null or empty");

            return webApplicationPath;
        }

      
    }

First, I check if the WebDev.WebServer is not running. If not then I start the server. The configuration settings are read from the App.config file.

Finally, here is my test.

 [TestFixture(ApartmentState = ApartmentState.STA)]
    public class TestAgreementPage : BaseTestPage
    {
        [Test]
        public void test_can_accept_the_user_agreement()
        {             
            IE ie = new IE(ConfigurationManager.AppSettings["DefaultPageUrl"] as String);

            ie.TextField("txtName").TypeText("Mohammad Azam");

            ie.CheckBox("chkAgree").Checked = true;
           
            ie.Button("Btn_Agree").Click();

            Assert.AreEqual("Valid",ie.Span("lblMessage").Text);           
        }      

 }

Now, when I run the test it will automatically start the server.

If you need you can add more configuration settings in the App.config file.

Print | posted @ Monday, January 07, 2008 5:33 AM

Twitter