Tutorial: Installing and running Selenium-RC in Perl

Used in conjunction with Selenium's online tutorials, this tip will provide additional guidance on how to start out with Selenium RC in Perl regardless of your platform or server. Once you have Selenium set up, this tip explains how to create and run your first test.

This install guide is designed to be used in conjunction with a tutorial on selenium. Here are the absolute nitty-gritty basics of how to get up and running with selenium RC and perl for for Windows, Macintosh, and most Linux systems. You'll need a computer with administrative rights and access to the internet. Depending on your technical skills, it will likely take something between ten minutes to an hour or so to get browser-driving tests running at your desk.

Downloading, installing and running the Selenium Server

The server is the process that actually controls the browser. It sits and waits on a specific network port for a request, then 'services' that request by obeying commands. This gives it the name RC - "remote control." We can write 'client' programs in any of a half-dozen possible languages -- the example below is in Perl.

Before you can run Selenium RC you'll need to have a Java compiler on your machine. It should come standard on Mac and Linux, and appear in c:\program files\Java on a PC. If you don't have Java installed, you can download it from Sun for free.

First, download the latest build of selenium RC and unzip it. Within that zip file, look for selenium-server.jar. That .jar file is a program written in Java that sits around, waiting for a request to start up a browser. On demand, it'll drive the browser and pass results back. We'll want to start the server with a double click, so write either a MS-DOS batch file or on Linux/Macintosh an executable that looks like this:

java -jar selenium-server.jar -port 2222

Put it in the same directory as selenium-server.jar and double-click to run. For ease of use, I put mine on the desktop.

Now that the server is running, we'll need to create a program to drive the browser. Let's make an example program in Perl. Perl is included with Linux, Mac OS 10, and you can download windows-based Activestate perl for free. We'll also need to install the perl modules to talk to selenium, which on Linux or a Macintosh is a console command.

To intall Test::WWW::Selenium on Mac/Linux, go to a console window and type:

 sudo cpan Test::WWW::Selenium

And answer yes and defaults to most questions, along with your local location and which servers to install from and the software will download automatically from the internet.

On Windows, to install Test::WWW::Selenium, you'll need to run the Perl Package Manager (ppm). Just go to the command line and type ppm (enter). When the package manager comes up, click on the grey box at upper-left to see the list of possible packages. Type in Test-WWW to see the list of test packages, click on Test-WWW-Selenium. Now go to the action menu and select Test-WWW-Selenium to be installed. Finally, select file->run marked actions, and whew, we've got Test::WWW::Selenium installed.

If you don't see Test-WWW-Selenium, you likely don't have that grey box selected. It's right below the 'File' menu; mouse over it and you will see "View all packages." The default is to view installed packages -- of course, Test-WWW-Selenium is not installed yet.

Finally, you should have Firefox 3 installed on your computer. (I'll put directions for Internet Explorer below as well.)

Creating our first test

Now we're ready to create our first test. Download the following file - amazon.pl - and save to your hard drive. You can read it in a text editor if you'd like; it's a simple program designed to search Amazon.com. (If you have Internet Explorer, you can edit the file and change *chrome to *iexplore in the text.)

Now go to the command-line, go to the directory you saved the file to, and type:

 perl searchamazon.pl

Away we go! You'll see the browser pop-open, and text appear on the screen in TAP format. A human being can read smaller tests, but for larger tests (and suites of tests) you'll want a tool that consumes TAP output and parses it, creating a report. There are several free or open-source TAP consumers.

This is a simple program designed to test search in Amazon, written in Perl. $sel is the selenium object; we basically just create the object and call commands on it. Notice the browser is *chrome; that actually means FireFox 3 in high permissions mode. That will allow us to upload files and interact with the local computer. Other browser options include *firefox, *iexplore, *safari, and *googlechrome. If we change the program to take browser as a command-line parameter, we could run the same script for many different browsers in a trivial step. But first, let's just plain run it.

And that's it! For more on the why's and wherefores of Selenium RC, you'll want to read the tutorial.

Related Content:

  • Podcast: Using Selenium for application testing
    Matt Heusser explains how to use Selenium an open source testing tool that supports Java scripting within Web browsers.
  • How to use Selenesse, a combination tool from FitNesse and Selenium
    Selenesse is an open source project that lets testers drive browsers with Selenium, but manage and run their tests in the testing framework Fitnesse. Learn details about how to use this new tool.

Dig Deeper on Topics Archive