I hope you enjoyed Friday’s post on Getting Started with Selenium. Today I spent some time messing about with Selenium RC, which I’d really like to use because it allows me to write the tests in Python.
My real need here is to add a smidge of programming to my test. When I test our forms, I have to use a unique email address every time, otherwise we’ll cause an “address already in use” error. I want to add a short random number to the email address to avoid this. I would prefer to do this in Python although I know there are other ways.
Selenium RC uses a Java-based server to control your browser by setting up a proxy server running on port 4444. When you run your test, it connects through this local proxy and drives your browser, executing the steps of the test. You can see a graphic illustrating this on the Selenium site.
Getting Started
The first thing I learned is that you must launch the Selenium Server and leave it running in order to run your tests. Adam Goucher offers an Ant script to automate opening Selenium Server, running your tests, and shutting down. I’m woefully ignorant of Ant, but it might help some of you.
For the Ant-naive like myself, let’s skip this step and just see it work. First, download Selenium RC and upack it on your machine. In the archive, you should see a directory called “selenium-server-1.0-beta-1″. Using a Terminal go into this directory. Now you can launch the Selenium Server using Java. Obviously you have to have Java installed locally to run the server. To test if you have Java, type:
java -version
You should get something along these lines:
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)
Now, to start the Selenium Server, type:
java -jar selenium-server.jar
The server should spit out a bunch of text and in it somewhere should be the line:
15:10:06.062 INFO - Started SocketListener on 0.0.0.0:4444
This means the Selenium Server is now listening on port 4444. Now to run a sample test. Open a new Terminal and move into the archive directory and then into the “selenium-python-client-driver-1.0-beta-1″ directory. A quick look of the directory listing will show a few sample tests. We’re going to run the Google test. Type:
python test_google.py
Now, here’s where it gets weird for me. I’m running Firefox 3 on Mac OS X (Leopard) and here’s what happens. A new instance of Firefox launches, but nothing happens. If I quit this new Firefox instance, Firefox relaunches and the test runs successfully.
.
----------------------------------------------------------------------
Ran 1 test in 8.078s
OK
I don’t know why this happens, so I’m going to ask around and see what I can come up with. You can close both of your Terminals at this point.
Selenium IDE + Selenium RC
My end goal here is to be able to generate at least the user input portions of the tests using Selenium IDE’s record mode, then export them (File -> Export Test Case As…) to Python, do a little custom coding, and be able to run these tests via Selenium RC. If you do this and export your tests into a test directory then try to run them, you should get an import error on selenium.py. You can rectify this in three different ways:
- Copy “selenium.py” into the directory where your Python-based tests live
- Put “selenium.py” into your Python apth
- Install Selenium via PyPI or other similar mechanism: easy_install selenium
Basically, Python needs to be able to execute the command “from selenium import selenium”. I hope this helps you get an understanding of Selenium RC, although it still leaves some questions unanswered. Another interesting side-effect that came up, is that Firefox 3 tries to connect to Google’s phishing and blacklist database upon launch, which lives at sb-ssl.google.com. This throws a certificate error. I tried to notify someone at Google about the certificate issuer for this has expired or is invalid. <shrug>.
UPDATE:
Apparently the 1.0b1 release of Selenium RC does not launch Firefox 3 properly, but the nightly build does. Check it out, I am!
~Spanky
December 23rd, 2008 - 21:03
Great post! I have been a power user of selenium RC (java) and ran into certificate error issues (sb-ssl.google.com) as stated in your post while running my tests on FF3/mac. The interesting part is i see this error only when Selenium invokes the firefox. When i visit the same websites using FF3 certificate error does not appear. Do you find any workaround or got any reply from google?
Once again great post, you have covered great details in this post and the previous one - getting started.
~ Ajit
December 30th, 2008 - 13:00
I’ve written a new post all about this: http://kapanka.com/2008/12/selenium-rc-firefox-and-the-self-signed-ssl-cert/