A Selenium and Python appetizer
Selenium is a browser automation framework and was released in version 2.0.0 just this week. In this post I'll show you how easy it is to get started with using this fine framework in Python.
Peparing the kitchen
Let's create and jump into a virtualenv. You don't really need it, but at least we don't need no sudo for anything :)
$ virtualenv demo
New python executable in demo/bin/python
Installing distribute..................................................done.
$ source demo/bin/activate
(demo) $
Installing the Selenium Python package is as easy as possible. Just use your favorite package manage and install it from PyPI:
(demo) $ pip install selenium
This installs the selenium Python package which includes all necessary drivers to run your browser automation using Firefox, Chrome or IE locally or anything that the Selenium Remote Control provides. We come back to that later.
If you want to automate Google's Chrome on your local machine some little setup is needed. If you want to follow the examples in this post and if you want to use an installed Firefox feel free to skip this section. To automate Chrome make sure Chrome is installed and then download and unzip the chromedriver for your system from the Selenium download page. Place the chromedriver binary somewhere in your $PATH. That's it.
Is it Google?
Now that Selenium is up and running, let's write a simple script that verifies that the title of the Google page actually contains the word "Google". That's simple and obvious, but the script illustrates how to use Selenium in UnitTests.
And you can do all this fancy stuff interactive in the Python shell:
#!/usr/bin/env python
import unittest
from selenium import webdriver
class TestTitle(unittest.TestCase):
def setUp(self):
# Toggle comments to test with a different browser
self.driver = webdriver.Chrome()
#self.driver = webdriver.Firefox()
#self.driver = wevdriver.Ie()
def tearDown(self):
self.driver.close()
def test_title_tag(self):
self.driver.get('http://google.com')
title_tag = self.driver.find_element_by_tag_name('title')
self.assertEqual(title_tag.text, 'Google')
if __name__ == '__main__':
unittest.main()
This script has one little drawback. It initializes a new webdriver (aka browser) for each tests. That's very accurate to start with a fresh browser for each test, but very time consuming too. As an alternative you can modify this script to use a global webdriver instance that is initialized before unittest.main() and closed after that line.
Since webdriver uses a network protocol to communicate with the browser, it's painless to use it in an interactive shell:
(demo) $ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get('http://google.com')
>>> driver.find_element_by_tag_name('title')
That's a nice and easy way to figure out what webdriver commands are available and how they work.
Going headless
When using Selenium as part of your test suite, you don't have always an X environment around to start Firefox or Chrome. As you can see in the sample script above, it's easy to switch between different browsers without the need to change anything in your actual tests.
As the name suggests the remote webdriver talks to a Remote Control Server that let's you choose a browser. The nice thing is that the standalone server comes with a headless browser that even supports JavaScript execution much like Firefox or Chrome.
Download the standalone server JAR on the Selenium download page and simply run java -jar selenium-server-standalone-2.0.0.jar to start it. When the server is started you can initialize a headless browser in your scripts like this:
from selenium import webdriver
driver = webdriver.Remote(
command_executor= 'http://127.0.0.1:4444/wd/hub', # that's the default
desired_capabilities={'browserName': 'htmlunit',
'version': '2',
'javascriptEnabled': True})
That's all to run your tests using a headless browser. Ready for your continuous integration system running on a server somewhere.
Testing web pages is fun, isn't it?
Edit 2011-07-12: Corey Goldberg posted a very similar article on his blog a few hours before I published this post.