Watin and portable Firefox

I stumbled over this thread in the Watin archives and thought that it sounded like a great idea to use a portable bundle of Firefox for our acceptance tests.

However it took some more effort in getting this thing to work than just changing the FireFox.PathToExe property.
Changing just the property will just make Watin start the portable apps launcher process.
And just because the launcher process is started it does not mean that Firefox has been started yet so here we have a race condition.
Depending on your setup you could get it to work sometimes but must of my tests always failed.

I began by playing around with the portable launcher configuration (FirefoxPortable.ini) for a while but it seemed impossible to get it to work that way so I gave up and downloaded the source for Watin.

I followed the stack trace back to the broken method.

System.NullReferenceException : Object reference not set to an instance of an object.
at WatiN.Core.Native.Mozilla.FireFoxClientPort.CreateNewFireFoxInstance(String url)
at WatiN.Core.Native.Mozilla.FireFoxClientPort.Connect(String url, Boolean createNewFireFoxInstance)
at WatiN.Core.FireFox.CreateFireFoxInstance(String url)

And eventually found the problem in the Firefox.CreateProcess(string arguments, bool waitForMainWindow) method.
After it had created the new process it used this piece of code to halt until the process had been started.

var result = action.Try(() =>
{
    ffProcess.Refresh();
    if(!ffProcess.HasExited && ffProcess.MainWindowHandle != IntPtr.Zero)
    {
        Logger.LogAction("Waited for FireFox, main window handle found.");
        return true;
    }
    return false;
});

But that piece of code just checks if new process has been started, not if it is the correct process.

Luckily while looking for the issue I notice this property:

internal static Process CurrentProcess {
    get {
        foreach (var process in Process.GetProcesses())  {
            if (process.ProcessName.Equals("firefox", StringComparison.OrdinalIgnoreCase))  {
                return process;
            }
        }
 
        return null;
    }
}

The CurrentProcess property will return null if there are no processes named firefox running so it could be used to tell whether or not it is the correct process that has been started.
So I fixed the issue by replacing the ffProcess.MainWindowHandle != IntPtr.Zero condition with a check if the current process is null or not:

var result = action.Try(() =>
{
    ffProcess.Refresh();
    if(!ffProcess.HasExited && CurrentProcess != null)
    {
        Logger.LogAction("Waited for FireFox, process found.");
        return true;
    }
    return false;
});

(If the Portable Firefox Launcher is started without a splash screen the ffProcess will never have a MainWindowHandle…)

, ,

2 Responses to Watin and portable Firefox

  1. Ola Herrdahl March 25, 2011 at 12:09 pm #

    And as a bonus, getting IE to work with Watin on Windows 7 / 2008 Server is described here http://stackoverflow.com/questions/1014815/how-do-you-get-watin-to-work-on-windows-server-2008-with-ie8

  2. Jeff December 13, 2011 at 1:46 am #

    I tried disabling the splash screen using the instructions at:
    http://www.howtogeek.com/howto/19571/disable-the-splash-screen-in-portable-firefox-and-other-portable-apps/

    After that Watin was able to start up the portable FF on my system.

Leave a Reply