API-docs¶
SeleniumTestCase API docs¶
-
class
seleniumhelpers.SeleniumTestCase(methodName='runTest')¶ Extends
django.test.LiveServerTestCaseto 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 raiseNoSuchElementException.
-
classmethod
getDriver(browser, use_rc)¶ Override this to create customize the
selenium-attribute.Parameters: - browser – The value of the
SELENIUM_BROWSERsetting. - use_rc – The value of
bool(SELENIUM_USE_RC).
- browser – The value of the
-
getInnerHtml(element)¶ Get
innerHTMLof the given element.
-
getPath(path)¶ Shortcut for
self.selenium.get(...)withpathprefixed bylive_server_urlas argument.
-
classmethod
setUpClass()¶ Adds the
seleniumattribute to the class. Theseleniumattribute defaults to an instance ofselenium.webdriver.Chrome, however this can be overridden using theSELENIUM_BROWSERdjango 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 insettings.pyand override it in an environment variable, typically when running thetestcommand:SELENIUM_BROWSER=Firefox python manage.py test
-
waitFor(item, fn, timeout=4, msg=None)¶ Wait for the
fnfunction to returnTrue. Theitemis forwarded as argument tofn.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 untilcssselectoris found, then useself.selenium.find_element_by_css_selectorto locate and return the element.Parameters: - within – The element to run
find_element_by_css_selector()on. Defaults toself.selenium. - timeout – Fail unless the
cssselectoris found beforetimeoutseconds.
- within – The element to run
-
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 toself.selenium. - timeout – Fail unless the
cssselectoris found beforetimeoutseconds.
- within – The element to run
-
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
cssselectornot to be found.Parameters: - within – The element to run
find_elements_by_css_selector()on. Defaults toself.selenium. - timeout – Fail if the
cssselectoris still found aftertimeoutseconds.
- within – The element to run
-
waitForDisabled(element, timeout=4, msg='The element is not disabled.')¶ Wait for the given
elementto become disabled (element.is_enabled() == False).Parameters: timeout – Fail unless the elementbecomes disabled beforetimeoutseconds. Defaults to10.
-
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
elementto become enabled (element.is_enabled() == True).Parameters: timeout – Fail unless the elementbecomes enabled beforetimeoutseconds.
-
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
textto appear inselenium.page_sourceor 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
textappears inselenium.page_sourcebeforetimeoutseconds has passed.
- within – The element to find text within (uses within.text). If
this is not specified, we get text from
-
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.settingsoros.environ.Parameters: - settings – The setting as a string.
- default – The fallback if
settingis not found. - typecast – A function that converts the given value from string to another type.
E.g.: Use
typecast=intto convert the value to int before returning.
-
seleniumhelpers.get_default_timeout()¶ Get the default timeout. Uses
get_setting_with_envfallback()to getSELENIUM_DEFAULT_TIMEOUT. Defaults to4.