Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / admin / tests.py
1 import sys
2
3 from django.test import LiveServerTestCase
4 from django.utils.importlib import import_module
5 from django.utils.unittest import SkipTest
6 from django.utils.translation import ugettext as _
7
8 class AdminSeleniumWebDriverTestCase(LiveServerTestCase):
9 webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver'
10
11 @classmethod
12 def setUpClass(cls):
13 if sys.version_info < (2, 6):
14 raise SkipTest('Selenium Webdriver does not support Python < 2.6.')
15 try:
16 # Import and start the WebDriver class.
17 module, attr = cls.webdriver_class.rsplit('.', 1)
18 mod = import_module(module)
19 WebDriver = getattr(mod, attr)
20 cls.selenium = WebDriver()
21 except Exception, e:
22 raise SkipTest('Selenium webdriver "%s" not installed or not '
23 'operational: %s' % (cls.webdriver_class, str(e)))
24 super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
25
26 @classmethod
27 def tearDownClass(cls):
28 if hasattr(cls, 'selenium'):
29 cls.selenium.quit()
30 super(AdminSeleniumWebDriverTestCase, cls).tearDownClass()
31
32 def wait_until(self, callback, timeout=10):
33 """
34 Helper function that blocks the execution of the tests until the
35 specified callback returns a value that is not falsy. This function can
36 be called, for example, after clicking a link or submitting a form.
37 See the other public methods that call this function for more details.
38 """
39 from selenium.webdriver.support.wait import WebDriverWait
40 WebDriverWait(self.selenium, timeout).until(callback)
41
42 def wait_loaded_tag(self, tag_name, timeout=10):
43 """
44 Helper function that blocks until the element with the given tag name
45 is found on the page.
46 """
47 self.wait_until(
48 lambda driver: driver.find_element_by_tag_name(tag_name),
49 timeout
50 )
51
52 def admin_login(self, username, password, login_url='/admin/'):
53 """
54 Helper function to log into the admin.
55 """
56 self.selenium.get('%s%s' % (self.live_server_url, login_url))
57 username_input = self.selenium.find_element_by_name('username')
58 username_input.send_keys(username)
59 password_input = self.selenium.find_element_by_name('password')
60 password_input.send_keys(password)
61 login_text = _('Log in')
62 self.selenium.find_element_by_xpath(
63 '//input[@value="%s"]' % login_text).click()
64 # Wait for the next page to be loaded.
65 self.wait_loaded_tag('body')
66
67 def get_css_value(self, selector, attribute):
68 """
69 Helper function that returns the value for the CSS attribute of an
70 DOM element specified by the given selector. Uses the jQuery that ships
71 with Django.
72 """
73 return self.selenium.execute_script(
74 'return django.jQuery("%s").css("%s")' % (selector, attribute))
75
76 def get_select_option(self, selector, value):
77 """
78 Returns the <OPTION> with the value `value` inside the <SELECT> widget
79 identified by the CSS selector `selector`.
80 """
81 from selenium.common.exceptions import NoSuchElementException
82 options = self.selenium.find_elements_by_css_selector('%s > option' % selector)
83 for option in options:
84 if option.get_attribute('value') == value:
85 return option
86 raise NoSuchElementException('Option "%s" not found in "%s"' % (value, selector))
87
88 def assertSelectOptions(self, selector, values):
89 """
90 Asserts that the <SELECT> widget identified by `selector` has the
91 options with the given `values`.
92 """
93 options = self.selenium.find_elements_by_css_selector('%s > option' % selector)
94 actual_values = []
95 for option in options:
96 actual_values.append(option.get_attribute('value'))
97 self.assertEqual(values, actual_values)
98
99 def has_css_class(self, selector, klass):
100 """
101 Returns True if the element identified by `selector` has the CSS class
102 `klass`.
103 """
104 return (self.selenium.find_element_by_css_selector(selector)
105 .get_attribute('class').find(klass) != -1)