almosteffortless.com | djangoproject.com | python.org | linux.com
demongin.org - Batch Script to Start Selenium RC Server on Windows

Batch Script to Start Selenium RC Server on Windows

Notes on some work I've been doing...at work. Posted for their potential use to posterity.


Friday, 2009-10-30 | Programming, Testing

"toconnell@lana:~/demongin/blog$ fortune
The truth is what is; what should be is a dirty lie.
-- Lenny Bruce"

Basically, as part of a work project, I'm working on doing automated testing of a certain internal-use web-based application.


Now, I'm no developer--I am but a humble QA engineer--so I am not responsible for unit tests or anything like that. I am also not responsible for so-called "sapient testing" (i.e. "exploratory testing", "bug hunting" or, as I like to call it, "dicking around"). What I am responsible for is setting up checks to make sure that important parts of this application don't die after re-deployments and restarts.

Ultimately, what this means, is that I've got to run browser tests. A bunch of them. And so, since anything that I have to do more than once has to be scripted, I've devised a small piece of software that walks a directory for test scripts (generated by Selenium IDE) and then fires them all off and records their results to a.) a log file and b.) a dynamically generated HTML form that allows for quick review of the results.

But that piece of the project isn't what I'm going to describe right now. What I'm going to describe right now is the batch script that starts up the Selenium server on the Windows PC from which my automatic browser-based tests are executed.

Without further ado:

SET selenium="C:\Documents and Settings\tim.oconnell\My Documents\SharedDocuments\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar"

SET profile="C:\Documents and Settings\tim.oconnell\Application Data\Mozilla\Firefox\Profiles\pmqpikeu.selenium"


java -jar %selenium% -singlewindow -port 555 -firefoxProfileTemplate %profile% 

cmd \K
Now you're probably wondering why I'm posting this totally boring, procedural code. I'll answer that question directly: I'm posting this totally boring procedural code because it took fucking forever to get it written and working correctly.

Part of that is my fault: I know very little about Windows, batch scripts and using cmd windows for anything other than cocktail party punch-lines. A bigger part of that, however, is the fault of the inherent limitations of batch scripting: Microsoft's built-in scripting language is a.) not like posix at all, b.) inflexible and user-hostile as all get-out and c.) lacks the basic functionality you could probably find in the oldest stable release of ksh. Another important part of why the above code took so long to write has to do with the fact that almost everything up there is a work-around of some type (and therefore required troubleshooting, researching, trial-and-error, etc.).

That having been said, here's the line-by-line breakdown. I offer this in the hopes that it'll save some other Linux guy some of the headaches that working around Windows has caused me:
SET selenium="C:\Documents and Settings\tim.oconnell\My Documents\SharedDocuments\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar"
Fairly straightforward. This initializes a variable (later called as "%selenium%") that contains the path to selenium-server.jar and contains double-quotes. I have bolded the last sentence because I spent about 20 minutes debugging this thing a little while ago and then did an epic foreheadslap when I realized that the quotes were coming along with the rest of the string.

Basically, I'm Dorothy, this script is Toto and, seeing as how we're not in Kansas (i.e. posix) anymore, Toto isn't a dog: he's literally every fucking character that follows the equal sign (that isn't escaped with a carat).

SET profile="C:\Documents and Settings\tim.oconnell\Application Data\Mozilla\Firefox\Profiles\pmqpikeu.selenium"
Same deal, except this time I'm setting the path to my special Firefox profile that I've set up for selenium to use.

This is something that a lot of professional testers do, as it enables them to create a unique about:config and tweak other settings within a Firefox profile that make for a smoother automated browser testing experience.

What's remarkable here (other than the fact that the double quotes are included as part of the variable) is that the trailing slash is missing after "pmqpikeu.selenium". This is key. If you feed a selenium server that's supposed to run on a Windows machine a path with a trailing slash, the server will not start.

java -jar %selenium% -singlewindow -port 555 -firefoxProfileTemplate %profile% 
Here is where the magic happens. I execute the Java binary with the "-jar" argument, pass it the path to my selenium server Java Archive, tell it to run everything in a single browser window, listen on port 555 and feed it the path to my Firefox profile.
cmd \K
This is important for debugging purposes. If you put all of the above syntax into a file called "sel.bat" and run it, it'll run the server and display its debug output in a cmd window, but if it does for some reason (or hey, if it doesn't start for some reason), without the "cmd K", your cmd window will close, and you'll never know why (unless you happen to be looking at the output window when the server dies and get a glimpse of its last words before the window closes which, I barely need to mention, is extremely unlikely).

I may write more about my big selenium project at some later time/date, but for now, it's important for me to get that batch ugliness a.) off my chest, b.) out into the world and c.) into the dog-house where all batch scripts belong.

Thanks for listening.