KoffeeKoder



  • When trying to execute WatiN unit tests you will need to start your WebServer by either manually running the command line tool or by running the Web Application Project. But, sometimes you will want the unit test project to dynamically start a web server. Let’s check it out how this can be done.

    First, if you want to start the ASP.NET internal server (WebDev.WebServer) then you can use command line to start it. The syntax is shown below:

    WebDev.WebServer.exe /port:1950 /path: “C:\Projects\MyWebApplication”

    You will need to be in the same directory where the WebDev.WebServer exists. By default it is located at:

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe

    Now, let’s use this information to start the server using unit tests. First, here is the required configuration saved in the configuration file (App.config).

    <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>

    The BaseTestPage class will use this information to start the server and all the test classes will derive from the BaseTestPage class to use this functionality.

    Here is the complete code for the BaseTestPage class:

    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;
            }

          
        }

    We used a static constructor to make sure that the process is not running. If it is not running we make a new process and start it else we use the old process. The GetWebServerArguments(), GetPort() and GetWebApplicationPath() are just helper methods to improve the readability.

    Finally, you will derive all your unit test classes from the BaseTestPage class as shown below:

    public class TestAgreementPage : BaseTestPage

    Now, if you run your unit test project will start the WebServer and then run all the tests.