Roman Prokofyev

New-Based Trading at LGT Capital Partners. Tech Advisor at FAIRTIQ.

Django Selenium integration Library

28 Sep 2011 » sciencewise, django, selenium, en

Original article (in Russian): http://habrahabr.ru/blogs/django/129107/

Hi there. It’s not a secret that testing is an important part of any software development, and if you’re building web apps, you definitely need to test your web interface. And it can be a pain in the ass because you need to simulate the process of user interaction with your web app. Luckily, we have such tool as Selenium.

So in this post I would like to talk about a tool that we have created recently while working on the ScienceWISE project. We have used Selenium in our project since its version 1 branch, and we had a terrible mixture of our code with the code for Selenium integration. So we decided to refactor it and make a separate library. Thus, django-selenium was born. In the rest of this article I will describe the features of this library and how to use it.

Features

  • **Integration with django test system**. *django-selenium* provides customized test runner built on top of standard django test runner: **django_selenium.selenium_runner.SeleniumTestRunner.** You can use it directly in the django settings or subclass to add some custom code. Behind scenes, *SeleniumTestRunner* performs the following operations:
    • Runs **selenium-server.jar**
    • Runs **django test server** with appropriate fixtures loaded** **
  • **Writing selenium** tests are made simple. Now you can write selenium tests as simple as other django tests:
    • create file *seltests.py *inside you app
    • Subclass your test class from **django_selenium.testcases.SeleniumTestCase**
    • Write tests using selenium webriver API!** **
    Essentially, **SeleniumTestCase **just loads required fixtures in a transaction and starts Firefox (or other) driver for you.
  • **Standard webdriver class was extended**. We have also written a standard webriver extension called **django_selenium.testcases.MyDriver **to facilitate common test operations. These operations include:
    • **authorize() - **perform authentication using standard django login url and form
    • **open_url(relative_path) - **open some url on a testserver, accepts relative url from **reverse() function**
    • **click(selector) - **click on the element found by css selector **selector** argument
    • **click_and_wait**(**selector, new_selector**) - find and click on the css selector **selector**, then wait until css selector **new_selector **is visible
    • **is_element_present(selector)** - returns **True **if element is found by css selector **selector**, **False **otherwise.
    For other functions available, please check the source code.

Installation and configuration Very simple:

pip install django-selenium

Next, specify test runner in your settings.py file:

TEST_RUNNER = 'django_selenium.selenium_runner.SeleniumTestRunner'

Finally, write a test command that will replace standard django command (you need to place this code in yourapp/management/commands/test.py file):

from django_selenium.management.commands import test_selenium

class Command(test_selenium.Command):

    def handle(self, *test_labels, **options):
        super(Command, self).handle(*test_labels, **options)

New test command have two additional options:

  • **--selenium - **run all tests including selenium tests
  • **--selenium-only - * ***run only selenium tests

As usual, you can run particular selenium test or selenium tests for a particular app.

That’s all. Now I have created an example app for you to check all this functionality instantly: https://github.com/dragoon/django-selenium-testapp.

Lest try it.

# Download selenium-server.jar
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.7.0.jar

git clone git://github.com/dragoon/django-selenium-testapp.git
cd django-selenium-testapp
# set path to selenium-server.jar in SELENIUM_PATH
vi settings.py

# Run tests
./manage.py test --selenium-only
...
----------------------------------------------------------------------
Ran 1 test in 13.254s

OK

If everyting is fine, you should have seen the following on the screen: Please report any issues to https://github.com/dragoon/django-selenium/issues.