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.

10 comments:

René Dudfield said...

Thanks! Very simple intro.

Anonymous said...

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

Anonymous 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.

fsouza 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.

Let2know said...

Best plug! this is an each level of simple to apply weblog that I can conclusively come steerage to more noteworthy age this yr! gratitude for educational announce. Bandicam Crack Keygen

Links For You said...

this is a particularly wonderful helpful asset which you are offering and you find the money for it away for justifiable. I truly like seeing site that arrangement the charge of providing an energies helpful asset for excuse. Convertxtodvd Key

Cyberz Pc said...

Charming, notice. I just tracked down your blog and expected to declare that i've esteemed examining your weblog posts. Once more after every one in all degree of part of, I can really get involved with your feed, and that I objective you could form quickly! Dad Birthday Wishes