Testing with Selenium

November 23, 2010

This post covers creating your first Selenium test. We want to record a test in Firefox using Selenium IDE, save it as a python script, then run it back using Selenium RC. Ultimately we want to call this automatically from a Hudson job on a remote Debian machine, but we'll do that in a subsequent post.

Selenium IDE

The first step is to install Selenium IDE. Here we're going to use version 1.0.7.

I tried 1.0.8 but got an error on firefox 3.6.12 so I fell back to the previous version. Had to hack the download url for 1.0.8 to download it though - their previous releases page isn't up to date!

Once installed inside Firefox, Selenium IDE is started using Tools/Selenium IDE. Once open, it's recording so I then went to my website, at http://bbr.apophis, and ran down down the menu opening each top level page in turn.

The Selenium IDE window now looks like this:

InitialTest

From here, we can run the test again by clicking the green button just to the right of the Fast/Slow slider.

If you do this in a new tab, it will load your site, and then navigate to each url in turn as specified by the script. Once this is done, the Selenium IDE window will look like this:

TestRun

Use the menu in Selenium IDE to save this as a Python script, using File/Export Test Case As/Python. This file will look like this:

from selenium import selenium
import unittest, time, re

class bbr(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://change-this-to-the-site-you-are-testing/")
        self.selenium.start()

    def test_bbr(self):
        sel = self.selenium
        sel.open("/")
        sel.click("link=Bands")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Contests")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Test Pieces")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Conductors")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Adjudicators")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Regions")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Calendar")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Years")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Venues")
        sel.wait_for_page_to_load("30000")

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Change the line at the top that says

self.selenium = selenium("localhost", 4444, "*chrome", "http://change-this-to-the-site-you-are-testing/")

so that is references the site you are testing, in our case http://bbr.apophis/, like this:

self.selenium = selenium("localhost", 4444, "*chrome", "http://bbr.apophis/")

This script is designed to run within Selenium RC. We'll look at this next.

Selenium RC

First, we need to download this. I downloaded selenium-remote-control-1.0.3.zip and then unpacked it. This gave me seven directories:

selenium-perl-client-driver-1.0.1
selenium-remote-control-1.0.3.zip
selenium-dotnet-client-driver-1.0.1  
selenium-php-client-driver-1.0.1     
selenium-ruby-client-driver-1.0.1
selenium-java-client-driver-1.0.1    
selenium-python-client-driver-1.0.1  
selenium-server-1.0.3

In order to run our python test, we need the files from selenium-python-client-driver-1.0.1. Copy these into the same directory as you saved the .py file for now.

We also need to run the files in selenium-server-1.0.3. This requires java. To run it, use java -jar selenium-server.jar:

$ java -jar selenium-server.jar
16:12:58.914 INFO - Java: Sun Microsystems Inc. 17.0-b16
16:12:58.917 INFO - OS: Linux 2.6.35-22-generic amd64
16:12:58.925 INFO - v2.0 [a2], with Core v2.0 [a2]
16:12:59.023 INFO - RemoteWebDriver instances should connect to: http://192.168.1.10:4444/wd/hub
16:12:59.024 INFO - Version Jetty/5.1.x
16:12:59.025 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
16:12:59.026 INFO - Started HttpContext[/selenium-server,/selenium-server]
16:12:59.026 INFO - Started HttpContext[/,/]
16:12:59.042 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@329f671b
16:12:59.042 INFO - Started HttpContext[/wd,/wd]
16:12:59.047 INFO - Started SocketListener on 0.0.0.0:4444
16:12:59.047 INFO - Started org.openqa.jetty.jetty.Server@7cd0a5d9

Note that this is listening on port 4444, same as the python script connects to. Leave this running.

Next, navigate to the directory where you saved the .py file and run it:

$ python bbr.py

This should popup two windows - one control window, and one that shows your site as the test is played, proxied through localhost:4444.

In my case, I got an error dialog, showing You have chosen to open which is a: BIN file., like this:

ErrorBox

This apparently is shown because the site is gziped by the web server to save bandwidth.

The advice to fix this problem is to use a separate Firefox profile that doesn't accept gzip-ed content.

To do this, close down Firefox competely, get a command prompt, and run:

$ firefox -profilemanager

This will bring up the following dialog:

FirefoxProfileManage

Create a new profile, and call it Selenium. Select Selenium, and click Start Firefox.

Once in, type about:config in the url bar and press return. This will let you into the config - confirm that you know what you're doing.

Type Network.http.accept-encoding into the filter box, and change the value of this key to be empty string and quit from Firefox. There are some additional optimisations available that you can do here - see the references section at the bottom of this post.

At this point you will need to run firefox -profilemanager again, selecting the default profile, so that it picks this up next time as the default.

You need to restart selenium RC server, so stop it with Ctrl-C and then use the following command to start it:

$ java -jar selenium-server.jar -firefoxProfileTemplate /home/tjs/.mozilla/firefox/z59e5iwk.Selenium

Have a look in the .mozilla/firefox directory in your home area to find out your profile's exact directory name.

Once this is done, you can go back to running your tests:

$ python bbr.py
.
----------------------------------------------------------------------
Ran 1 test in 22.240s

OK

This will pop up the two windows mentioned earlier and run your tests.

References