Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
from django.test import LiveServerTestCase
|
|
|
|
from django.utils.importlib import import_module
|
|
|
|
from django.utils.unittest import SkipTest
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
class AdminSeleniumWebDriverTestCase(LiveServerTestCase):
|
|
|
|
webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
try:
|
|
|
|
# Import and start the WebDriver class.
|
|
|
|
module, attr = cls.webdriver_class.rsplit('.', 1)
|
|
|
|
mod = import_module(module)
|
|
|
|
WebDriver = getattr(mod, attr)
|
|
|
|
cls.selenium = WebDriver()
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
raise SkipTest('Selenium webdriver "%s" not installed or not '
|
2012-02-22 15:14:20 +08:00
|
|
|
'operational: %s' % (cls.webdriver_class, str(e)))
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
if hasattr(cls, 'selenium'):
|
|
|
|
cls.selenium.quit()
|
2012-10-04 18:45:22 +08:00
|
|
|
super(AdminSeleniumWebDriverTestCase, cls).tearDownClass()
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2011-12-29 06:20:30 +08:00
|
|
|
def wait_until(self, callback, timeout=10):
|
|
|
|
"""
|
|
|
|
Helper function that blocks the execution of the tests until the
|
|
|
|
specified callback returns a value that is not falsy. This function can
|
|
|
|
be called, for example, after clicking a link or submitting a form.
|
|
|
|
See the other public methods that call this function for more details.
|
|
|
|
"""
|
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
WebDriverWait(self.selenium, timeout).until(callback)
|
|
|
|
|
|
|
|
def wait_loaded_tag(self, tag_name, timeout=10):
|
|
|
|
"""
|
|
|
|
Helper function that blocks until the element with the given tag name
|
|
|
|
is found on the page.
|
|
|
|
"""
|
|
|
|
self.wait_until(
|
|
|
|
lambda driver: driver.find_element_by_tag_name(tag_name),
|
|
|
|
timeout
|
|
|
|
)
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
def admin_login(self, username, password, login_url='/admin/'):
|
|
|
|
"""
|
|
|
|
Helper function to log into the admin.
|
|
|
|
"""
|
|
|
|
self.selenium.get('%s%s' % (self.live_server_url, login_url))
|
|
|
|
username_input = self.selenium.find_element_by_name('username')
|
|
|
|
username_input.send_keys(username)
|
|
|
|
password_input = self.selenium.find_element_by_name('password')
|
|
|
|
password_input.send_keys(password)
|
|
|
|
login_text = _('Log in')
|
|
|
|
self.selenium.find_element_by_xpath(
|
|
|
|
'//input[@value="%s"]' % login_text).click()
|
2011-12-29 06:20:30 +08:00
|
|
|
# Wait for the next page to be loaded.
|
|
|
|
self.wait_loaded_tag('body')
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
def get_css_value(self, selector, attribute):
|
|
|
|
"""
|
|
|
|
Helper function that returns the value for the CSS attribute of an
|
|
|
|
DOM element specified by the given selector. Uses the jQuery that ships
|
|
|
|
with Django.
|
|
|
|
"""
|
|
|
|
return self.selenium.execute_script(
|
2012-02-20 00:42:12 +08:00
|
|
|
'return django.jQuery("%s").css("%s")' % (selector, attribute))
|
|
|
|
|
2012-02-23 16:07:07 +08:00
|
|
|
def get_select_option(self, selector, value):
|
2012-02-20 00:42:12 +08:00
|
|
|
"""
|
2012-02-23 16:07:07 +08:00
|
|
|
Returns the <OPTION> with the value `value` inside the <SELECT> widget
|
|
|
|
identified by the CSS selector `selector`.
|
2012-02-20 00:42:12 +08:00
|
|
|
"""
|
2012-02-20 00:53:55 +08:00
|
|
|
from selenium.common.exceptions import NoSuchElementException
|
2012-03-05 16:13:09 +08:00
|
|
|
options = self.selenium.find_elements_by_css_selector('%s > option' % selector)
|
2012-02-20 00:42:12 +08:00
|
|
|
for option in options:
|
|
|
|
if option.get_attribute('value') == value:
|
2012-02-23 16:07:07 +08:00
|
|
|
return option
|
|
|
|
raise NoSuchElementException('Option "%s" not found in "%s"' % (value, selector))
|
|
|
|
|
|
|
|
def assertSelectOptions(self, selector, values):
|
|
|
|
"""
|
|
|
|
Asserts that the <SELECT> widget identified by `selector` has the
|
|
|
|
options with the given `values`.
|
|
|
|
"""
|
2012-03-05 16:13:09 +08:00
|
|
|
options = self.selenium.find_elements_by_css_selector('%s > option' % selector)
|
2012-02-23 16:07:07 +08:00
|
|
|
actual_values = []
|
|
|
|
for option in options:
|
|
|
|
actual_values.append(option.get_attribute('value'))
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertEqual(values, actual_values)
|
|
|
|
|
|
|
|
def has_css_class(self, selector, klass):
|
|
|
|
"""
|
|
|
|
Returns True if the element identified by `selector` has the CSS class
|
|
|
|
`klass`.
|
|
|
|
"""
|
|
|
|
return (self.selenium.find_element_by_css_selector(selector)
|
2012-10-04 18:45:22 +08:00
|
|
|
.get_attribute('class').find(klass) != -1)
|