Introduction to Functional Web Testing With Twill & Selenium

Part 2 :: Selenium RC :: Fine Grained Control With Python

Synopsis

The Selenium IDE is a great prototyping tool, but it can be a little cumbersome to use when you want to run multiple Selenium tests. Additionally, you don't get any side benefits like conditionals. However, Selenium RC (remote control) allows you to access the Selenium API programatically. This section will take us through Selenium RC.

Selenium RC
 

Selenium RC is a package that contains a server and a set of interfaces that allow you to programatically interact with Selenium Core in your language of choice. For these examples, we'll compose nose-style tests using Python to talk to Selenium RC.

Out first step is to unpack the Selenium RC package. When you finish unpacking it, if you look at the directories you should see something like this:

              [terryp@tpmacbook] selenium-remote-control-1.0-beta-2 :: ls
              selenium-dotnet-client-driver-1.0-beta-2 selenium-python-client-driver-1.0-beta-2
              selenium-java-client-driver-1.0-beta-2   selenium-ruby-client-driver-1.0-beta-2
              selenium-perl-client-driver-1.0-beta-2   selenium-server-1.0-beta-2
              selenium-php-client-driver-1.0-beta-2
            

As you can see, there are Selenium drivers for .Net, Java, Perl, PHP, Ruby and Python. You'll also notice a selenium-server directory; this is a packaged Java Jetty server that allows the proxying of requests that enables browser control.

The first thing we need to do is start the Jetty server by issuing the 'java -jar selenium-server.jar' in the selenium-server directory:

              [terryp@tpmacbook] selenium-remote-control-1.0-beta-2 :: cd selenium-server-1.0-beta-2/
              [terryp@tpmacbook] selenium-server-1.0-beta-2 :: java -jar selenium-server.jar
              23:03:31.219 INFO - Java: Apple Inc. 1.5.0_16-133
              23:03:31.249 INFO - OS: Mac OS X 10.5.6 i386
              23:03:31.253 INFO - v1.0-beta-2 [2571], with Core v1.0-beta-2 [2330]
              23:03:31.492 INFO - Version Jetty/5.1.x
              23:03:31.493 INFO - Started HttpContext[/,/]
              23:03:31.494 INFO - Started HttpContext[/selenium-server,/selenium-server]
              23:03:31.494 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
              23:03:31.546 INFO - Started SocketListener on 0.0.0.0:4444
              23:03:31.546 INFO - Started org.mortbay.jetty.Server@bc887b
            

Okay, now we have the server running, so let's write a little nose-style Selenium RC test called 'test_sel_rc.py.'

We're going to have to write this test inside the 'selenium-python-client-drive' directory (we know; our colleague Kumar McMillan already has a ticket open with the Selenium guys to add a setup.py file). Once that's created, let's try to mimic what we did in the Selenium IDE test:

              from selenium import selenium

              def test_mimic_sel_ide():
                  s = selenium("localhost", 4444, "*firefox", "http://127.0.0.1")
                  s.start()

                  s.open("http://127.0.0.1")
                  assert s.is_text_present("Welcome")
                  s.stop()
            

OK, now let's try running it with nose and see what we get:

              [terryp@tpmacbook] selenium-python-client-driver-1.0-beta-2 :: nosetests test_sel_rc.py -vs
              test_sel_rc.test_mimic_sel_ide ... ok

              ----------------------------------------------------------------------
              Ran 1 test in 9.547s

              OK
              [terryp@tpmacbook] selenium-python-client-driver-1.0-beta-2 ::
            

Feel free to examine the terminal output in the Selenium Server for a little more information about what happened.

More information about selenium server options can be found here.

 

next

Fluid 960 Grid System, created by Stephen Bau, based on the 960 Grid System by Nathan Smith. Released under the GPL/ MIT Licenses.