Posted on by & filed under django, programming, testing.

When you write any nontrivial software application, you soon realize that while fixing or improving one aspect of it, you often run a risk of accidentally breaking something else in the process.  And that going through the entire application looking for such broken parts every time you change something just isn’t very fun.  So you start writing automated tests so that a computer can do it instead, because unlike you or your associates working in QA, the computer doesn’t care if it’s fun or not.  The important thing to remember is that for most systems the ultimate goal is to make sure that the software works normally as a real person would use it, so ideally you need at least some tests that can fairly realistically pretend to be such a person.

Django has a rather nicely documented system for testing web applications built using it. At this point, most serious Django sites probably have some number of unit tests that load test data, exercise various parts of the code, and sometimes even pretend to be a very basic web browser by requesting a URL and analyzing the returned stream of text.  However, useful as they are, these tests don’t make for very convincing simulations of real users.  They don’t load all of the assets referenced by a page, run any of the JavaScript on it, look at the layout of elements on the page, try clicking on any of the links, etc.  Especially for AJAX-heavy sites, this leaves a wide range of potential bugs that aren’t being checked for at all.  Tools like Selenium improve on this by automating an actual web browser to load pages, run JavaScript, click on page elements, and so forth.  You can even pick which browser to automate, letting you test for an assortment of cross-browser compatibility bugs.  Django 1.4 added some support for running Selenium tests, but that part of the documentation is still a little thin.  In this series of blog posts I’ll be going over a few tips for getting more out of Selenium tests for a Django app.

The sample Selenium test in the Django documentation starts out like this:

One problem with this which soon becomes apparent: it assumes that your test will always be run using Firefox on the same machine which is running the tests and the application (LiveServerTestCase starts up an instance of your Django server which is used for all the test methods in the class).  Also, it can be useful to start a separate browser session for each test case rather than share one for the entire class; this can help avoid stability problems from automating the browser for too long and work better with tools I’ll discuss later in this series for capturing data about a test case run.  Solving these issues in a base class to be inherited by all your Selenium test classes can then look something like this:

You can now use an environment variable (SELENIUM_BROWSER) to determine which browser to use when running your tests.  By simply changing this variable and re-running the tests, you can test with different browsers.  If you’re running your test on a Mac, you can even automate a browser running in an iPhone or iPad simulator.  By noting which browser is being used in self.browser, you can automatically skip tests which depend on application features that are known not to work in it or which rely on Selenium features that don’t yet work with that browser:

Note that we still haven’t worked around the limitation that all the tests be run on the local machine; there are ways to do that, which I’ll discuss in the next post in this series.


6 Responses to “Writing a Selenium Test Framework for a Django Site (Part 1)”


    Hey just wanted to give you a quick heads up and let you know a
    few of the images aren’t loading properly. I’m not sure why but I think its a
    linking issue. I’ve tried it in two different browsers and both show the same results.

  2. Charlotte roofing contractor

    I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web will be
    much more useful than ever before.


  1.  Writing a Selenium Test Framework for a Django Site (Part 2) | Digital publishing and technology posts from the team at Safari Books Online
  2.  Writing a Selenium Test Framework for a Django Site (Part 3) | Digital publishing and technology posts from the team at Safari Books Online
  3.  Optimizing JavaScript in a Django Project: django-require « Safari Books Online: Publishing & Technology