James Gardner: Home > Blog > 2009 > Getting Started with Selenium and Python

Getting Started with Selenium and Python

Posted:2009-02-18 15:32
Tags:Pylons, Python, Web

I thought I'd have a look at Selenium today. Here I my notes from my first outing.


Selenium is a funcitonal testing framework which automates the browser to perform certain operations which in turn test the underlying actions of your code. It comes in a number of different versions:

Available Components

Selenium Core

The core HTML and JavaScript files which you can install on your server to program Selenium directly. No-one really uses these.

Selenium IDE

A firefox plugin which embeds Selenium Core into Firefox so that you don't need to install it on your server. It also provides a nice GUI for recording and re-playing tests as well as exporting them as Python code (and other formats) so that they can be used with Selenium RC

Selenium RC (Remote Control)

This is a server which can be controlled (via HTTP as it happens) to send Seleium commands to the browser. This effectively allows you to control a browser remotely from your ordinary Python test suite. The RC server also bundles Selenium Core, and automatically loads it into the browser.

Selenium Grid

This allows you to run your Selenium tests on multiple different browsers in parallel.

We will use Selenium IDE with Firefox and Selenium RC.

Selenium IDE

Now would be a great time to watch the introductory video at http://seleniumhq.org to understand how Selenium IDE works. It is only a minute or two long and exaplins how everything works with a demo much better than I could with words.

Now you've seen the video its time to create and run your own test. Here's what you need to do:

  1. Download and install Selenium IDE from http://seleniumhq.org/download/ It is a .xpi file which will be installed as a Friefox plugin.
  2. Start the IDE by clicking Tools -> Selenium IDE from the menu.
  3. Recording will have started automatically so perform some actions. For this first example, try searching for something in Google, follow a link and then perform some more actions on the site you arrive at. Then press the record button to stop.
  4. Press the play all tests button to see the actions re-performed
  5. Choose Options->Format->Python to see a Selenium RC test suite written in Python which will re-perform those actions. Save the Test Case as First Test and save the Python as first_test.py

Although the Selenium IDE you've just used to record your tests only works in Firefox, the tests it produces can be used with Selenium RC to run tests in most modern JavaScript browsers.

Selenium RC

The Selenium RC server is written in Java even though it can be controlled from many languages. To run it you will need a recent version of Java installed. Here's how to install it on Debian/Ubuntu:

sudo apt-get install openjdk-6-jdk

Now download the latest version from http://seleniumhq.org/download/ and run these commands to install it:

unzip selenium-remote-control-1.0-beta-2-dist.zip
cd selenium-remote-control-1.0-beta-2
cd selenium-server-1.0-beta-2
java -jar selenium-server.jar

The options for the server are documented at: http://seleniumhq.org/documentation/remote-control/options.html

If you visit http://localhost:4444/selenium-server/tests/html/test_click_page1.html you'll see some of the tests. You can run these automatically with these commnads with the server still running:

cd selenium-remote-control-1.0-beta-2
cd selenium-python-client-driver-1.0-beta-2
python test_default_server.py

This will take a few seconds to load some browser windows and then output the following to show that the test passed:

Using selenium server at localhost:4444
Ran 1 test in 18.695s


There will be plenty of output on the Selenium RC server's console explaining what it is up to.

Now try running the script you saved earlier. You'll need to copy the selenium.py file from selenium-remote-control-1.0-beta-2/selenium-python-client-driver-1.0-beta-2 into the same directory as first_test.py and then run this:

python first_test.py

You'll see the following error output after 3 seconds:

ERROR: test_new (__main__.NewTest)
Traceback (most recent call last):
  File "first_test.py", line 12, in test_new
  File "/home/james/Desktop/GRP/code/trunk/GRP/test/selenium.py", line 764, in open
    self.do_command("open", [url,])
  File "/home/james/Desktop/GRP/code/trunk/GRP/test/selenium.py", line 215, in do_command
    raise Exception, data
Exception: Timed out after 30000ms

Ran 1 test in 44.308s

FAILED (errors=1)

This is because you need to specify the URL where Selenium should start its testing. Ordinarily Selenium can't test across multiple domains so the domain you start at is the only one which can be testes. Selenium is able to work around this limitation in certain browsers such as Firefox and IE as you can read about at http://seleniumhq.org/documentation/remote-control/experimental.html

Since we are using Firefox Selenium RC can test multiple domains at once but it still needs to be given the URL to start at. Since the test you performed in the Selenium IDE section started at http://google.com you should change the URL http://change-this-to-the-site-you-are-testing/ to http://google.com in the code. If you run the test again you should see Selenium repeat all the actions and this time confirm the output was a success:

Ran 1 test in 37.098s


If you want to examine the windows which pop-up during the tests, just add these lines to the end of the test_new() method to make the test wait for 10 seconds after it has been run:

import time

You can now remote control the browser from Python which means you can perform other tasks in-between the tests you are running. For example, you might want to remove all data from your test database before the tests are run.

To test your code in a different browser you would change this line:

self.selenium = selenium("localhost", 4444, "*chrome", "http://google.com/")

To something like this:

self.selenium = selenium(
    "c:\\program files\\internet explorer\\iexplore.exe",

Happy testing.

Selenium Website:
Python Documentation:

(view source)

James Gardner: Home > Blog > 2009 > Getting Started with Selenium and Python