Yuce's Blog

Cognitive dissonance is yoga for the brain.*

Web Application Testing with Selenium: Basics and Setup

2009-10-09

In­tro­duc­tion

It's always hard to ensure an ap­pli­ca­tion works up to a spec­i­fi­ca­tion and continue keeping up to the specs in the course of de­vel­op­ment. Testing web ap­pli­ca­tions is even harder, since they are composed of more components, and usually at least some of the components of the ap­pli­ca­tion are not directly controlled by the developers, such as links to other internet resources or mashups.

Luckily, there's a great tool for easing the pain of web ap­pli­ca­tion testing: Selenium. Selenium simulates a human user, by con­trol­ling a web browser using Javascript through a system called Selenium Core. The simulated behaviour includes clicking on links, form and dialog buttons; filling in forms; drag&drop and others.

Currently there are two ways to develop and run Selenium tests, Selenium IDE which is a Firefox extension and Selenium Remote Control which consists of a Java server that controls the browser and client APIs for writing tests.

In this series of articles I will try to explain how to write tests for Selenium RC.

Pre­req­ui­sites and Install

Selenium server is written in Java and requires a Java runtime en­vi­ron­ment of version 5 or up, a recent version of Python is required (I've used 2.5 and 2.6) for the client API (other languages are also supported, including Java and PHP); last of all a browser with decent Javascript is required, Firefox and Safari works great. Internet Explorer and Opera are supported too.

If you are on Linux, most probably you already have Python and Firefox installed and may need to install Java; on Mac OSX Leopard and up you need to install Firefox; on Windows you need to install all pre­req­ui­sites.

In the rest of the article, I assume you're on a UNIX-like system and familar with the command line. If you're ready, let's roll!

  1. Download Selenium RC from: http://seleniumhq.org/download/ and extract it to somewhere convenient. The package contains Selenium server and client APIs for several languages.
  2. Create a directory for the tests, I picked ~/tests.
  3. Copy selenium-server.jar from selenium-server directory in the package to ~/tests.
  4. Copy selenium.py from selenium-python-client-driver-1.0.1 directory in the package to ~/tests. Al­ter­na­tive­ly, you can copy it to somewhere in your Python path.
  5. Selenium is installed!

Writing a Simple Test

Now, let's write a simple test using Selenium client API for Python. This test demon­strates only a small portion of Selenium's power. Save the following code to ~/tests/test_wikipedia.py.

Selenium has a nice Wikipedia page. Suppose we want to check whether that page is up frequently since we are fans of Selenium. We could write a test as follows:

import time
import unittest
from selenium import selenium

# Select Firefox as the browser
# (Note that this is not Google's Chrome)
BROWSER = '*chrome'
SITE_URL = 'http://en.wikipedia.org'

class WikipediaSeleniumTest(unittest.TestCase):
    def setUp(self):
        self.sel = selenium('localhost', 4444,
            BROWSER, SITE_URL)
        self.sel.start()

    def test_wikipedia(self):
        sel = self.sel
        sel.open('/')

        # Go to selenium page
        sel.type('id=searchInput', 'selenium')
        sel.click('id=searchGoButton')
        sel.wait_for_page_to_load(30000)
        # assure we are on selenium page
        assert sel.get_title().startswith('Selenium'), \
            'Selenium page not reached'

        # Wikipedia displays selenium element page,
        # let's visit the software one
        sel.click('link=Selenium (software)')
        sel.wait_for_page_to_load(30000)
        assert sel.get_title().startswith('Selenium (software)'), \
            'Selenium (software) page not reached'

        # wait a bit so we can see the last page
        time.sleep(5)

    def tearDown(self):
        self.sel.stop()

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

You can get the source from here http://python.pastebin.com/f3293f2a8. I will explain the code in the next article in this series, but we can run it and see how it works:

  1. Run Selenium server: java -jar selenium-server.jar&.
  2. Run the test: python test_wikipedia.py.

If everything goes right, you should see two Firefox windows: the one on the top contains executed Selenium commands (Selenese), and bottom one shows the automated Firefox window, which reflects the executed Selenese. You can make that window larger to see "selenium" is written in the search box and visited pages change as Selenium clicks buttons and links. If the test completes suc­cess­ful­ly, you will see something like the following in your console:

---------------------
Ran 1 test in 16.536s

OK

The dots represents number of tests completed suc­cess­ful­ly. Since there was only one test run, there's one dot.

python, selenium, testing, web

comments powered by Disqus