Wednesday, 19 January 2011

Introduction to Selenium for Python programmers

Selenium is an application that automates web browsers, helping you test your web application from a user perspective, in an automated manner. These properties make Selenium tests a perfect fit for validating your js-level functionality and implementing acceptance tests.

Of course, it has some drawbacks: you need to run your application from another process, which gives you some pain with checking the backend state of things. The tests might be quite slow, and - if you don't write them well - extremely fragile.

However, starting with basic Selenium tests is very simple, which I'm going to prove below. We will create a trivial website, with a single element only: a link to Google. Next, we will implement a Selenium test that makes sure this indeed happens.

Prepare the environment


We will work in a virtualenv called "seltest". If you don't know what virtualenv is, you likely want to read this first. Enter the directory of your choice and run the following commands:

mkdir seltest
cd seltest
virtualenv --no-site-packages .


We will work from within the seltest directory, so that we don't need to activate the virtualenv, and instead call our binaries by "bin/python" or "bin/pip".

Let's download the Selenium executable and its python bindings:

wget http://selenium.googlecode.com/files/selenium-server-standalone-2.0a4.jar
bin/pip install selenium


You can already play with Selenium. First, start another terminal window and run:

java -jar selenium-server-standalone-2.0a4.jar


Then from our main terminal:

$ bin/python
>>> from selenium.remote import connect
>>> from selenium import FIREFOX
>>> browser = connect(FIREFOX) # this will run the browser
>>> browser.get("http://www.yahoo.com") # you should see the browser navigating to yahoo
>>> browser.close() # this will close the session


Prepare and run the website



The website will consist of a single link, we can skip all the obligatory html boilerplate at this stage. Save the text

<a href="http://google.com">Go to Google</a>


into a file index.html, and from another terminal (yes, you will need three terminal windows) run:

Python -m SimpleHTTPServer


This will start serving your page on the port 8000, you can visit the page from your web browser on http://localhost:8000/


Implement the test



Open the file selenium_test.py in your editor of choice and dump the following

import unittest
from selenium.remote import connect
from selenium import FIREFOX

class SelTest(unittest.TestCase):
def setUp(self):
self.browser = connect(FIREFOX)
def tearDown(self):
self.browser.close()
def test_simple(self):
self.browser.get("http://localhost:8000/")
link = self.browser.find_element_by_partial_link_text("Google")
link.click()
self.assertEqual(self.browser.get_title(), "Google")

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


The setUp and tearDown methods manage the browser session, and the actual test lives in the test_simple method. We are using four methods from the browser object: get, find_element_by_partial_link_text, click and get_title. In case you wonder where these come from, look for the WebDriver class definition. You can find it in lib/python2.6/site-packages/selenium/remote/webdriver.py in your environment.


Run the test



Now, you are ready to run your test.


bin/python selenium_test.py


You should see something along the lines of:

$ bin/python selenium_test.py 
.
----------------------------------------------------------------------
Ran 1 test in 5.521s

OK


Which indicates, that all your tests passed correctly.

7 comments:

illume said...

Thanks! Very simple intro.

Marius Gedminas said...

You don't have to use a separate process -- you could use a thread.

id said...

Hello,

Nice little introduction. Short and sweet.
For any that come across this, Selenium
is (IHMO) probably the best way of testing
your web application from an end-users
perspective. And yes for all those folks that
like to have process, QA, compliance, acceptance
testing, etc, Selenium is the way to go!

cheers
James

Konrad said...

@Marius

You're right, obviously. I'll try show a solution like that in a follow-up post.

Francisco Souza said...

Nice and simple post, but I really like Selenium 2 with webdriver. Try it ;)

Konrad said...

@Francisco

This actually uses selenium2 with webdriver under the hood. You can tell by the wget line and the webdriver API.

nkuttler said...

Thanks! I'm just building my first selenium tests and the official docs are a little scanty.