It's always hard to ensure an application works up to a specification and continue keeping up to the specs in the course of development. Testing web applications is even harder, since they are composed of more components, and usually at least some of the components of the application are not directly controlled by the developers, such as links to other internet resources or mashups.
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.
Prerequisites and Install
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 prerequisites.
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!
- 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.
- Create a directory for the tests, I picked
selenium-serverdirectory in the package to
selenium-python-client-driver-1.0.1directory in the package to
~/tests. Alternatively, you can copy it to somewhere in your Python path.
- Selenium is installed!
Writing a Simple Test
Now, let's write a simple test using Selenium client API for Python.
This test demonstrates only a small portion of Selenium's power.
Save the following code to
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:
- Run Selenium server:
java -jar selenium-server.jar&.
- Run the test:
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 successfully, you will see something like the following in your console:
--------------------- Ran 1 test in 16.536s OK
The dots represents number of tests completed successfully. Since there was only one test run, there's one dot.