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 raiseNoSuchElementException
.
-
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)
.
- browser – The value of the
-
getInnerHtml
(element)¶ Get
innerHTML
of the given element.
-
getPath
(path)¶ Shortcut for
self.selenium.get(...)
withpath
prefixed bylive_server_url
as argument.
-
classmethod
setUpClass
()¶ Adds the
selenium
attribute to the class. Theselenium
attribute defaults to an instance ofselenium.webdriver.Chrome
, however this can be overridden using theSELENIUM_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 insettings.py
and override it in an environment variable, typically when running thetest
command:SELENIUM_BROWSER=Firefox python manage.py test
-
waitFor
(item, fn, timeout=4, msg=None)¶ Wait for the
fn
function to returnTrue
. Theitem
is 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 untilcssselector
is found, then useself.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 toself.selenium
. - timeout – Fail unless the
cssselector
is found beforetimeout
seconds.
- 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
cssselector
is found beforetimeout
seconds.
- 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
cssselector
not to be found.Parameters: - within – The element to run
find_elements_by_css_selector()
on. Defaults toself.selenium
. - timeout – Fail if the
cssselector
is still found aftertimeout
seconds.
- within – The element to run
-
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 beforetimeout
seconds. 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
element
to become enabled (element.is_enabled() == True
).Parameters: timeout – Fail unless the element
becomes enabled beforetimeout
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 inselenium.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 inselenium.page_source
beforetimeout
seconds 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.settings
oros.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 getSELENIUM_DEFAULT_TIMEOUT
. Defaults to4
.