1 min read

Multi-browser Selenium tests with Django 1.4+

In Selenium tests with Django 1.4+ we saw how to run Selenium tests on Django 1.4+ based projects.

However, these tests were run on a single Web Driver, ie. a single browser.

Fortunately, Selenose 1.3 makes it easy to test on multiple browser. Let's switch the single browser tested project to a multiple browser tested project.

Sample project

Let's use the same poll application from the Django tutorial.

The source code can be found on the django14-selenose branch of this repository. Clone it with:

$ git clone -b django14-selenose https://github.com/shiningpanda/djangotutorial-selenium.git

Then step in the new djangotutorial-selenium folder, and install all requirements with pip by typing:

$ pip install -r requirements.txt

Configure tests

We should be able to switch browser via an option on the manage.py jenkins command line. To do so, edit djangotutorial/settings.py and set JENKINS_TASKS:

JENKINS_TASKS = [
    'django_jenkins.tasks.run_pylint',
    'django_jenkins.tasks.with_coverage',
    'django_jenkins.tasks.django_tests',
    'selenose.tasks.selenium_driver',
]

The first tasks are the django-jenkins default ones, but note the last one: selenose.tasks.selenium_driver. This is the core of the solution.

Write tests

In our project, Selenium tests are located in the polls/tests.py module.

Make all you tests inherit from selenose.cases.LiveServerTestCase. A test template would be:

from selenose.cases import LiveServerTestCase

class SampleTestCase(LiveServerTestCase):

    def test(self):
        self.driver.get(self.live_server_url)
        # You scenario here

Run tests

Then to run tests on Firefox, just execute:

$ python manage.py jenkins --selenium-driver=firefox
Creating test database for alias 'default'...
..
----------------------------------------------------------------------
Ran 2 tests in 15.258s

OK
Destroying test database for alias 'default'...

On Chrome? easily done (requires the Chrome Driver):

$ python manage.py jenkins --selenium-driver=chrome

This section details all the capabilities.

Need a Selenium Server?

Edit djangotutorial/settings.py and append selenose.tasks.selenium_server to JENKINS_TASKS:

JENKINS_TASKS = [
    # Other tasks
    'selenose.tasks.selenium_server',
]

More?

For a full documentation, see the Django Jenkins section on selenose.readthedocs.org.

What's next?

In the next blog post, we will see how to integrate this project with Jenkins!