Running your first Selenium test without the IDE

Up to now I’ve only written here about using Selenium with the handy Firefox add-on, which provides the ability to just record-and-play. There are limitations to just using the Selenium IDE, and only a few of these are addressed by using plugins. There’s likely to become a time where you just need to graduate from Selenium IDE to something more powerful.

Not long ago, the only available next step from Selenium IDE was Selenium RC (Remote Control). This is a Java server that you can send commands to in a number of different client languages, which get translated and then used to manipulate the browser. There are two main advantages to this: you have the full power of a programming language, and you can run tests in more than just Firefox.

More recently, an alternative next step is WebDriver. This is included in Selenium 2, and although the same advantages as RC exist, there are a whole lot more. Two such advantages of WebDriver are: no longer a dependency on a Java server, and much improved browser integration. Rather than exclusively controlling the browser content via JavaScript, each browser now has its own driver. What’s even better is that several browser vendors are actively involved in the drivers. Oh, and there’s even support for browsers on mobile devices! Selenium RC is now considered by some as legacy, and although the Selenium project intends to continue supporting users, there is little to no active development on it. Both RC and WebDriver share the same core, so fixes for WebDriver will likely benefit Selenium RC users, but the development focus is clearly on WebDriver.

So, rather than talk about Selenium RC, I’m going to show you how to go straight from Selenium IDE to WebDriver. For the purposes of this demonstration I’m going to use the Python client library. I used to write all my tests in Java, but I have to confess that since using Python I find it a much more approachable language for test automation, especially if you’re new to programming languages.

First, make sure you have at least version 1.1.0 of Selenium IDE. This introduced the WebDriver formatters, which we’ll be using. Next you’ll need to enable experimental features in order to use the formatters. This is due to a number of bugs that could cause you to lose your test cases when switching back and forth between formats. It was disabled to prevent this from happening without a user being at least warned, and will hopefully be resolved soon. To enable it, click ‘Options’ > ‘Options…’ and check ‘Enable experimental features’. You can optionally check ‘Disable format change warning messages’ if you don’t want to be reminded of the risk every time you change formats.

Export to Python

Next, open or create a simple test in the IDE with a few commands and checks. Change the format from the ‘Options’ menu to ‘Python 2 (WebDriver)’. The source tab will be selected and if you look closely you’ll see that the same assertions appear in the source, although they look much different to the table view. Save the test case using ‘File’ > ‘Save Test Case As…’ and give a filename such as mytest.py

Install Python

For the next step you’ll need Python 2 installed, which you can get from http://www.python.org/getit/. You’ll also need to install setuptools from http://pypi.python.org/pypi/setuptools. Once you have these, you can run the following to install the Selenium python client library:

easy_install pip
pip install selenium

Run the Python test

Now that you have everything you need, you should be able to run the test. Run the following command, making sure you have the correct location of the mytest.py file:

python mytest.py

If everything goes well, you should see Firefox pop up and run your test, and then the results will be output to your console. There are a few problems that can occur though…

Troubleshooting

Can’t find Firefox binary

WebDriver doesn’t know where Firefox is installed, so it will try a few guesses based on defaults. If you’ve installed to an alternative location then you’ll need to specify it in you mytest.py file. Add the following to the top of the file along with the other imports:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

Then change the line where the self.driver variable is assigned to include the path to Firefox:

self.driver = webdriver.Firefox(firefox_binary=FirefoxBinary("C:\Program Files\Mozilla Firefox 6\firefox.exe"))

Unable to open URL

If the sites you are opening are relative then you will need to include the base URL. For example, change any occurrences of the following:

driver.get("/")

To include the base URL, using string formatting:

driver.get("%s/" % self.base_url)

Unicode

If you are trying to check text that include unicode characters then you may need to tweak the strings. For example the following:

self.assertEqual("QMO – quality.mozilla.org | The Home of Mozilla QA", driver.title)

Should become:

self.assertEqual(u"QMO u2013 quality.mozilla.org | The Home of Mozilla QA", driver.title)

Note the ‘u’ that precedes the string. If you’re not sure, use the failure messages for clarification on what the actual text that’s being returned from the browser is.

Verifications:

Selenium IDE has a rare feature for a testing framework, which is verifications (or soft assertions). You will see that if you format any verifications as a client language, then there’s often a much more verbose implementation. For example, Python sets up a list for catching the assert failures, and then checks that this list is empty at the end of the test. You may want to consider using just hard assertions for less verbose tests.

Conclusion

Unfortunately, the formatters are not a silver bullet. I personally prefer to write my tests directly in the client language, however if you already have a suite of Selenium IDE tests then the formatters are the quickest way to migrate to WebDriver. The formatters themselves are configurable, and it’s also worth noting that the WebDriver formatters are still very new and likely to see some improvements in the next few releases of Selenium IDE. If you would like to see some examples of WebDriver tests written in Python then I’d recommend the tests for the following Mozilla projects: QMO, Wiki, FlightDeck.

If you have any questions you might find the Selenium Users group useful.

If you find any issues in Selenium please check if it’s already been raised on the official issue tracker before raising a new report with as much information as possible.

You might be interested in... Problem Solving for Testers - 1 Day Course

MoT Courses-19

Tags: ,

One Response to “Running your first Selenium test without the IDE”

  1. Rina NoronhaNovember 28, 2011 at 6:24 pm #

    Hi, Dave!

    I’m the web editor at iMasters, one of the largest developer communities in Brazil. I´d like to talk to you about republishing your article at our site.

    Can you contact me at rina.noronha@imasters.com.br?

    Bests,
    Rina Noronha
    Journalist – web editor
    http://www.imasters.com.br
    redacao@imasters.com.br
    rina.noronha@imasters.com.br
    +55 27 3327-0320 / +55 27 9973-0700