CI | python selenium Wed 29 August 2012 Olivier Mansion

With the new Django 1.4+ support for in-browser testing frameworks, it was time for selenose to support django-jenkins, a plug and play continuous integration tool for Django and Jenkins.

Selenose now provides two Selenium related tasks for django-jenkins:

Selenium Server Task

This task starts a Selenium Server before running tests, and stops it at the end of the tests.

To enable it, edit your settings.py and append selenose.tasks.selenium_server to JENKINS_TASKS:

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

If this setting does not exist yet, do not forget to create it with the default tasks:

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

Options for Selenium Server are the same as for the nose Selenium Server Plugin. Set them in a setup.cfg located in the current working directory, for instance:

[selenium-server]
debug = true
log = selenium-server.log

You can also specify the path to the configuration file with the --selenose-config option on the manage.py jenkins command line:

$ python manage.py jenkins --help
[...]
  selenose.tasks.selenium_server:
    --selenose-config=SELENOSE_CONFIGS
                        Load selenose configuration from config file(s). May
                        be specified multiple times; in that case, all config
                        files will be loaded and combined.

In your tests, just create a new Remote Web Driver calling the server and that's it:

from django.test import LiveServerTestCase

from selenium import webdriver

class TestCase(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
        super(BaseTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(BaseTestCase, cls).tearDownClass()
        cls.driver.quit()

    def test(self):
        driver.get(self.live_server_url)

Selenium Driver Task

This task provides a Selenium Web Driver to Selenium tests.

To enable it, edit your settings.py and append selenose.tasks.selenium_driver to JENKINS_TASKS:

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

If this setting does not exist yet, do not forget to create it with the default tasks:

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

But enabling this task is not enough, a Web Driver environment is also required. An environment declares all the necessary parameters to create a new Web Driver. See selenose documentation for more details.

The environments are defined in a setup.cfg located in the current working directory, for instance:

[selenium-driver:sample]
webdriver = firefox

You can also specify the path to the configuration file containing the environments with the --selenose-config option on the manage.py jenkins command line:

$ python manage.py jenkins --help
[...]
  selenose.tasks.selenium_driver:
    --selenose-config=SELENOSE_CONFIGS
                        Load selenose configuration from config file(s). May
                        be specified multiple times; in that case, all config
                        files will be loaded and combined.
    --selenium-driver=SELENIUM_DRIVER
                        Enable the provided environment.

To enable an environment, use the --selenium-driver option on the manage.py jenkins command line:

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

Then the Web Driver is directly available in you tests with self.driver and there is no need to cleanup after use, selenose will do it for you:

from selenose.cases import LiveServerTestCase

class TestCase(LiveServerTestCase):

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

Combining Server & Driver

To combine a Selenium Server and a Selenium Driver task, just enable them both in the settings: the command_executor option of the remote Web Driver will know the correct value to reach the Selenium Server.

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

Made with love: Tomotcha