Tuesday, March 30, 2010

Warming up Sites Before Running Selenium Tests with NUnit’s SetUpFixture

I’ve been running in to a number of problems where my Selenium tests are timing out when they first run in our automated build environment. Our Selenium test build runs three times a day and sets up the environment from scratch using a distribution package created in another automated build process. [Note: This meets my goal of always testing what you release, not just what you can build.]

The issue with starting from scratch every time is that ASP.NET has to compile the site the first time a Selenium instance spins up. This takes long enough that several tests can fail due to timeouts. My local repave process (hacked together from several PowerShell scripts) hits the site before launching my tests; however, I needed some way to do this within my test process.

NUnit’s SetUpFixture attribute to the rescue!

This handy attribute lets you mark a class to run a one-time setup or teardown for a namespace or for an entire assembly. If you qualify the class with a namespace, then setup/teardown will run once for that namespace only. If you don’t qualify the class with a namespace it will run once for the entire assembly it resides in.

This is awesome.

Using this approach, I can fire up a WebClient instance and hit URLs which will force compilation of the target sites. Here’s how I do it:

using System;
using System.Net;
using NUnit.Framework;
using Telligent.Evolution.Tests.Selenium.Common;


[SetUpFixture]
public class AssemblySetupFixture
{
    [SetUp]
    public void WarmUpSiteAndControlPanelBeforeTesting()
    {
        try
        {
            var webClient = new WebClient();
            webClient.Proxy = null;
            var requestUrl = http://MyHost/SomeUrl;
            var result = webClient.DownloadString(requestUrl);
            requestUrl += "/ADeeperUrl/";
            result = webClient.DownloadString(requestUrl);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

Note there’s no namespace declared, so this snippet runs once for the assembly it runs in. Yes, the catch block is ugly, but this is my first pass at this and I’ll figure out something smarter to do with Exceptions later.

There are a number of ways I could have skinned this cat, but frankly I’d been wanting to try out this NUnit feature for some time and this gave me a nail to use that hammer on. (Because you can’t get enough bad metaphors in one sentence…)

Thursday, March 18, 2010

Selenium Exception: Unable to Delete File (parent.lock)

Problem: When using FireFox for Selenium tests, Selenium RC is throwing exceptions with the error message “Selenium.SeleniumException: Failed to start new browser session: Unable to delete file” with a reference to a “parent.lock” file in your temp folder. Cleaning up FireFox profiles doesn’t help.

Solution: You may be using an older version of the selenium-server.jar. Go grab the latest download of Selenium RC. At least that’s what fixed it for us…

Friday, March 12, 2010

Getting an Assembly From the GAC

Windows Explorer lets you browse the Global Assembly Cache (GAC) and drop strongly-named assemblies in if you’ve the proper permissions. What you can’t do is pull things out of the GAC using Explorer. Explorer hides some abstractions of the GAC from you, preventing you from pulling assemblies out.

I had the need to validate that an assembly in the GAC was indeed the version I expected; however, once a file’s in the GAC you can’t just drag it out using Explorer. (I couldn’t rely on the Version number shown in Explorer – it’s a DLL related to some SharePoint work and the pain of dealing with versioned assemblies in SharePoint is a rant I’ll save you from. Today.)

You can get around this with a quick bit of command-line fu, though. Actually, it’s just copy, so it’s not even fu.

Using an admin account, open a command prompt and change to the %SYSTEMROOT%\assembly folder. There are a number of folders under here:

C:\Windows\assembly>dir /b
GAC
GAC_32
GAC_64
GAC_MSIL
NativeImages_v2.0.50727_32
NativeImages_v2.0.50727_64
temp
tmp

Use dir /s to find the assembly you’re looking for. (Line breaks inserted)

C:\Windows\assembly>dir TelligentEvolution.Wss.Global.dll /s /b
C:\Windows\assembly\GAC_MSIL\TelligentEvolution.Wss.Global
       \4.1.31029.3047__454c7a96e9526647\TelligentEvolution.Wss.Global.dll

Now you’ve got the full path to the assembly. Use copy to get it where you want it.

C:\Windows\assembly>copy GAC_MSIL\TelligentEvolution.Wss.Global
   \4.1.31029.3047__454c7a96e9526647\TelligentEvolution.Wss.Global.dll 
   d:\temp
        1 file(s) copied.

Poof. Now I can get at the file with Reflector for a bit of inspection.

Subscribe (RSS)

The Leadership Journey