Skip to content
gkanwar edited this page Nov 5, 2011 · 1 revision

Writing Selenium tests

We use the django_selenium python package (https://github.com/dragoon/django-selenium) for our selenium tests. This package looks under the main directory of all the django apps installed and tries to find the seltests.py module.

Within the seltest modules it finds, it runs test classes similar to normal testing. Within test classes you write, you should override the setUp function to do any set up and define functions have 'test' in the name as the functions to be run. In order to use selenium in your tests, your class should subclass SeleniumTestCase from the django_selenium.testcases module.

As for writing the actual selenium tests themselves, check out the Selenium HQ docs on Selenium 2.0 (http://seleniumhq.org/docs/03_webdriver.html). The docs have several python examples, but are mostly in java. For details on the python bindings, you can check out the google code page (http://code.google.com/p/selenium/wiki/PythonBindings). The documentation on python bindings is patchy at best, and it's often more useful to either take a look at the source code or mess around in the shell, using 'dir' to list all options for different objects. The google code page also has information on particular webdrivers.

Here's an example selenium test that tries to log into the server, and makes use of some of the most common techniques:

# seltests.py
from django_selenium.testcases import SeleniumTestCase
from selenium.webdriver.support.ui import WebDriverWait 

class ExampleTestCase(SeleniumTestCase):
	# Perform any set up
	def setUp(self):
		SeleniumTestCase.setUp(self)
		print “Setting up!”
	# This function is not run as a test
	def areWeThereYet(driver):
		# Run a javascript function that tells us our place in the login line
		return driver.execute_script(“return getPlaceInLine()”)
	# This function is run as a test (it starts with “test”)
	def testSquare(self):
		# Go to the homepage (this goes to the root directory of our server)
		self.open_url(“/”)
		elem = self.find_element_by_name(“username”)
		elem.send_keys(“example_username”)
		elem = self.find_element_by_name(“password”)
		elem.send_keys(“example_password”)
		elem.submit()
		#Wait until areWeThereYet returns true or 10 seconds passes
		try:
			WebDriverWait(self, 10).until(areWeThereYet)
		except:
			print “Waiting in line timed out!”
		self.failUnlesss(self.is_text_present(“Hello!”))

Running Selenium tests

Use the 'seltest' command issued to manage.py to run selenium tests, hybrid tests, or normal tests (it includes the functionality of the regular 'test' command). You can run regular tests only with python manage.py seltest, all tests (including selenium tests) with python manage.py seltest --selenium, and selenium tests only with python manage.py seltest --selenium-only. You can also run tests for specific django apps similar to regular django testing, ex: python manage.py seltest my_app --selenium.

You can change settings for running the selenium tests in local_settings.py. The settings are:

  • SELENIUM_DRIVERS – Which webdriver you want to run the tests on (i.e. 'Chrome', 'Firefox', etc)
  • SELENIUM_TEST_RUNNER – If you want to write your own test runner that adds in some functionality. We currently have a dummy one that simply calls the super class under esp/utils/custom_test_runner.py. This is probably a good place to start adding functionality to the test runner.
  • SELENIUM_DISPLAY – This sets which display you want tests to run on. This is useful for headless machines (eg. Hippo).
  • SELENIUM_PATH – The path to the selenium standalone jar file. This is already set in django_settings.py and shouldn't need to be changed.
  • SELENIUM_TESTSERVER_PORT – The port on which to connect to the testserver. This shouldn't need to be changed.
  • SELENIUM_TIMEOUT – Sets the timeout for the tests. This shouldn't need to be changed either.
Clone this wiki locally