From my previous post about using Selenium RC to test web user interface. Since Selenium RC is HTTP compliance server, controlling test case is done by sending HTTP (GET) request so any programming language that support HTTP will be able to use to control Selenium RC.
For this post, I will use Python as client driver.
Install Python
Installing Python is so simple. You can download an executable and double click and follow screen, that 's all. I use Python2.5
Look at the example
From previous post, after extract Selenium RC, you will a folder names selenium-python-client-driver-1.0-beta-1. This folder contains Python library use to control Selenium RC and some example files, See below for details;
- selenium.py, Python Selenium driver.
- selenium_test_suite.py, Test suit for example code.
- selenium_test_suite_headless.py, Another test suit.
- test_ajax_jsf.py, Ajax test example.
- test_default_server.py, test Selenium RC server itself.
- test_google.py, test google.
- test_i18n.py, test international web.
Getting Python Selenium RC sample up
- First, start Selenium RC, open command prompt and go to Selenium RC directory and type
java -jar selenium-server.jar
- This will start Selenium RC server
- Go to Python client driver, for sample, selenium-python-client-driver-1.0-beta-1.
- To run all example test, type
python selenium_test_suite.py
- You will Selenium RC automatically open web browser and does the test according to test code
- Example output,
testKeyPress (test_ajax_jsf.TestAjaxJSF) ... Using selenium server at localhost:4444 ok testLinks (test_default_server.TestDefaultServer) ... Using selenium server at localhost:4444 ok test_google (test_google.TestGoogle) ... ok test_i18n (test_i18n.TestI18n) ... ok
Ran 4 tests in 16.172s OK
Look at the code
""" Copyright 2006 ThoughtWorks, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from selenium import selenium import unittest import sys, time class TestAjaxJSF(unittest.TestCase): seleniumHost = 'localhost' seleniumPort = str(4444) #browserStartCommand = "c:\\program files\\internet explorer\\iexplore.exe" browserStartCommand = "*firefox" browserURL = "http://www.irian.at" def setUp(self): print "Using selenium server at " + self.seleniumHost + ":" + self.seleniumPort self.selenium = selenium( self.seleniumHost, self.seleniumPort, self.browserStartCommand, self.browserURL ) self.selenium.start() def testKeyPress(self): selenium = self.selenium input_id = 'ac4' update_id = 'ac4update' selenium.open( "http://www.irian.at/selenium-server/tests/html/ajax/ajax_autocompleter2_test.html" ) selenium.key_press(input_id, 74) time.sleep(0.5) selenium.key_press(input_id, 97) selenium.key_press(input_id, 110) time.sleep(0.5) self.failUnless('Jane Agnews' == selenium.get_text(update_id)) selenium.key_press(input_id, '\9') time.sleep(0.5) self.failUnless('Jane Agnews' == selenium.get_value(input_id)) def tearDown(self): self.selenium.stop() if __name__ == "__main__": unittest.main()
From example code, controlling Selenium RC using Python is simple and straightforward. If you ever write unit testing using Python unit test, writing test for Selenium RC looks very similar to writing Python unit test.
First, we set up Selenium RC server by specific host and port. After that, we specific web browser will be used to test and the web under test, in this case is http://www.irian.at.
In the setUp function, we instantiate selenium object by above variables and start the driver. In the test function, we use selenium.click to simulate use clicking. We also have a time sleep down the test. At the end of each action, we have a simple validation to verify test case.
At the tearDown, we cal selenium.stop() to stop the client.
As you can see, writing a test code is simple and allow rapid user interface testing.