API-docs

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, msg='CSS selector, "{css_selector}" matches at least one element, when we expected it not to.')

Assert that element.find_element_by_css_selector(css_selector) does not raise NoSuchElementException.

classmethod getDriver(browser, use_rc)

Override this to create customize the selenium-attribute.

Parameters:
  • browser – The value of the SELENIUM_BROWSER setting.
  • use_rc – The value of bool(SELENIUM_USE_RC).
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=4, msg=None)

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, msg='myelem is empty')
waitForAndFindElementByCssSelector(cssselector, within=None, timeout=4)

Use waitForCssSelector() to wait until cssselector is found, then use self.selenium.find_element_by_css_selector to locate and return the element.

Parameters:
  • within – The element to run find_element_by_css_selector() on. Defaults to self.selenium.
  • timeout – Fail unless the cssselector is found before timeout seconds.
waitForCssSelector(cssselector, timeout=4, within=None, msg='No elements match css selector "{cssselector}".')

Wait for the given cssselector.

Parameters:
  • within – The element to run find_element_by_css_selector() on. Defaults to self.selenium.
  • timeout – Fail unless the cssselector is found before timeout seconds.
waitForCssSelectorNotFound(cssselector, timeout=4, within=None, msg='CSS selector, "{cssselector}" matches at least one element, when we expected it not to.')

Wait for the given cssselector not to be found.

Parameters:
  • within – The element to run find_elements_by_css_selector() on. Defaults to self.selenium.
  • timeout – Fail if the cssselector is still found after timeout seconds.
waitForDisabled(element, timeout=4, msg='The element is not disabled.')

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.
waitForDisplayed(element, timeout=4, msg='The element is not displayed.')

Wait for the given element to be displayed.

waitForEnabled(element, timeout=4, msg='The element is not enabled.')

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

Parameters:timeout – Fail unless the element becomes enabled before timeout seconds.
waitForNotDisplayed(element, timeout=4, msg='The element is not hidden.')

Wait for the given element to be hidden.

waitForText(text, timeout=4, msg='Could not find text "{text}"', within=None)

Wait for text to appear in selenium.page_source or from the text of an element.

Parameters:
  • within – The element to find text within (uses within.text). If this is not specified, we get text from selenium.page_source.
  • timeout – Fail unless the text appears in selenium.page_source before timeout seconds has passed.
waitForTitle(title, timeout=4)

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

waitForTitleContains(title, timeout=4)

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

Helper-functions

seleniumhelpers.get_setting_with_envfallback(setting, default=None, typecast=None)

Get the given setting and fall back to the default of not found in django.conf.settings or os.environ.

Parameters:
  • settings – The setting as a string.
  • default – The fallback if setting is not found.
  • typecast – A function that converts the given value from string to another type. E.g.: Use typecast=int to convert the value to int before returning.
seleniumhelpers.get_default_timeout()

Get the default timeout. Uses get_setting_with_envfallback() to get SELENIUM_DEFAULT_TIMEOUT. Defaults to 4.