Usage

Subclass seleniumhelpers.SeleniumTestCase instead of django.test.LiveServerTestCase in your testcases.

Example

from seleniumhelpers import SeleniumTestCase

class TestFrontpage(SeleniumTestCase):
    def test_frontpageimage(self):
        # Use our shortcut instead of self.selenium.get(self.live_server_url + '/something')
        self.getPath('/something/')

        # Fail unless the expected image and text is available within 10 secons
        self.waitForCssSelector('img.frontpageimage')
        self.waitForText(u'This is the frontpage image text.')

        # Assert that we remembered to close the body. Mostly to show how
        # to get hold of the selenium WebDriver object
        # Note: The selenium attribute is actually set as an attribute on
        #       the class in SeleniumTestCase.setUpClass()
        self.assertTrue('</body>' in self.selenium.page_source)

Running tests

You can run the tests just like normal Django tests, however we provide the ability to override the browser used for the tests, and to completely skip all selenium tests.

Select selenium browser:

$ SELENIUM_BROWSER=Firefox python manage.py test

See available browsers:

$ python manage.py listseleniumbrowsers

Skip selenium tests:

$ SKIP_SELENIUMTESTS=1 python manage.py test

Note

SKIP_SELENIUMTESTS and SELENIUM_BROWSER may also be set in settings.py. See seleniumhelpers.SeleniumTestCase.setUpClass() for more details.

SeleniumTestCase API docs

class seleniumhelpers.SeleniumTestCase(methodName='runTest')

Extends django.test.LiveServerTestCase to simplify selenium testing.

executeScript(script, element)

Shortcut for self.selenium.executeScript(script, element).

failIfCssSelectorFound(element, css_selector)

Assert that element.find_element_by_css_selector(css_selector) raises NoSuchElementException.

getInnerHtml(element)

Get innerHTML of the given element.

getPath(path)

Shortcut for self.selenium.get(...) with path prefixed by live_server_url as argument.

classmethod setUpClass()

Adds the selenium attribute to the class. The selenium attribute defaults to an instance of selenium.webdriver.Chrome, however this can be overridden using the SELENIUM_BROWSER django setting or environment variable. If both the django setting and and environment variable is set, the environment variable is used. This means that you can set the default value in settings.py and override it in an environment variable, typically when running the test command:

SELENIUM_BROWSER=Firefox python manage.py test
waitFor(item, fn, timeout=10)

Wait for the fn function to return True. The item is forwarded as argument to fn.

Example (wait for text in an element):

waitFor(myelem, lambda myelem: len(myelem.text) > 0)
waitForCssSelector(cssselector, timeout=10, within=None)

Wait for the given cssselector.

Parameters:
  • timeout – Fail unless the cssselector is found before timeout seconds. Defaults to 10.
  • within – The element to run find_element_by_css_selector() on. Defaults to self.selenium.
waitForDisabled(element, timeout=10)

Wait for the given element to become disabled (element.is_enabled() == False).

Parameters:timeout – Fail unless the element becomes disabled before timeout seconds. Defaults to 10.
waitForEnabled(element, timeout=10)

Wait for the given element to become enabled (element.is_enabled() == True).

Parameters:timeout – Fail unless the element becomes enabled before timeout seconds. Defaults to 10.
waitForText(text, timeout=10)

Wait for text to appear in selenium.page_source.

Parameters:timeout – Fail unless the text appears in selenium.page_source before timeout seconds has passed. Defaults to 10.
waitForTitle(title)

Wait until the page title (title-tag) equals the given title.

waitForTitleContains(title)

Wait until the page title (title-tag) contains the given title.