| 1 |
from contextlib import contextmanager |
| 2 |
|
| 3 |
from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
| 4 |
from django.test import modify_settings |
| 5 |
from django.test.selenium import SeleniumTestCase |
| 6 |
from django.utils.deprecation import MiddlewareMixin |
| 7 |
from django.utils.translation import gettext as _ |
| 8 |
|
| 9 |
# Make unittest ignore frames in this module when reporting failures. |
| 10 |
__unittest = True |
| 11 |
|
| 12 |
|
| 13 |
class CSPMiddleware(MiddlewareMixin): |
| 14 |
"""The admin's JavaScript should be compatible with CSP.""" |
| 15 |
|
| 16 |
def process_response(self, request, response): |
| 17 |
response.headers["Content-Security-Policy"] = "default-src 'self'" |
| 18 |
return response |
| 19 |
|
| 20 |
|
| 21 |
@modify_settings(MIDDLEWARE={"append": "django.contrib.admin.tests.CSPMiddleware"}) |
| 22 |
class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase): |
| 23 |
available_apps = [ |
| 24 |
"django.contrib.admin", |
| 25 |
"django.contrib.auth", |
| 26 |
"django.contrib.contenttypes", |
| 27 |
"django.contrib.sessions", |
| 28 |
"django.contrib.sites", |
| 29 |
] |
| 30 |
|
| 31 |
def wait_until(self, callback, timeout=10): |
| 32 |
""" |
| 33 |
Block the execution of the tests until the specified callback returns a |
| 34 |
value that is not falsy. This method can be called, for example, after |
| 35 |
clicking a link or submitting a form. See the other public methods that |
| 36 |
call this function for more details. |
| 37 |
""" |
| 38 |
from selenium.webdriver.support.wait import WebDriverWait |
| 39 |
|
| 40 |
WebDriverWait(self.selenium, timeout).until(callback) |
| 41 |
|
| 42 |
def wait_for_and_switch_to_popup(self, num_windows=2, timeout=10): |
| 43 |
""" |
| 44 |
Block until `num_windows` are present and are ready (usually 2, but can |
| 45 |
be overridden in the case of pop-ups opening other pop-ups). Switch the |
| 46 |
current window to the new pop-up. |
| 47 |
""" |
| 48 |
self.wait_until(lambda d: len(d.window_handles) == num_windows, timeout) |
| 49 |
self.selenium.switch_to.window(self.selenium.window_handles[-1]) |
| 50 |
self.wait_page_ready() |
| 51 |
|
| 52 |
def wait_for(self, css_selector, timeout=10): |
| 53 |
""" |
| 54 |
Block until a CSS selector is found on the page. |
| 55 |
""" |
| 56 |
from selenium.webdriver.common.by import By |
| 57 |
from selenium.webdriver.support import expected_conditions as ec |
| 58 |
|
| 59 |
self.wait_until( |
| 60 |
ec.presence_of_element_located((By.CSS_SELECTOR, css_selector)), timeout |
| 61 |
) |
| 62 |
|
| 63 |
def wait_for_text(self, css_selector, text, timeout=10): |
| 64 |
""" |
| 65 |
Block until the text is found in the CSS selector. |
| 66 |
""" |
| 67 |
from selenium.webdriver.common.by import By |
| 68 |
from selenium.webdriver.support import expected_conditions as ec |
| 69 |
|
| 70 |
self.wait_until( |
| 71 |
ec.text_to_be_present_in_element((By.CSS_SELECTOR, css_selector), text), |
| 72 |
timeout, |
| 73 |
) |
| 74 |
|
| 75 |
def wait_for_value(self, css_selector, text, timeout=10): |
| 76 |
""" |
| 77 |
Block until the value is found in the CSS selector. |
| 78 |
""" |
| 79 |
from selenium.webdriver.common.by import By |
| 80 |
from selenium.webdriver.support import expected_conditions as ec |
| 81 |
|
| 82 |
self.wait_until( |
| 83 |
ec.text_to_be_present_in_element_value( |
| 84 |
(By.CSS_SELECTOR, css_selector), text |
| 85 |
), |
| 86 |
timeout, |
| 87 |
) |
| 88 |
|
| 89 |
def wait_until_visible(self, css_selector, timeout=10): |
| 90 |
""" |
| 91 |
Block until the element described by the CSS selector is visible. |
| 92 |
""" |
| 93 |
from selenium.webdriver.common.by import By |
| 94 |
from selenium.webdriver.support import expected_conditions as ec |
| 95 |
|
| 96 |
self.wait_until( |
| 97 |
ec.visibility_of_element_located((By.CSS_SELECTOR, css_selector)), timeout |
| 98 |
) |
| 99 |
|
| 100 |
def wait_until_invisible(self, css_selector, timeout=10): |
| 101 |
""" |
| 102 |
Block until the element described by the CSS selector is invisible. |
| 103 |
""" |
| 104 |
from selenium.webdriver.common.by import By |
| 105 |
from selenium.webdriver.support import expected_conditions as ec |
| 106 |
|
| 107 |
self.wait_until( |
| 108 |
ec.invisibility_of_element_located((By.CSS_SELECTOR, css_selector)), timeout |
| 109 |
) |
| 110 |
|
| 111 |
def wait_page_ready(self, timeout=10): |
| 112 |
""" |
| 113 |
Block until the page is ready. |
| 114 |
""" |
| 115 |
self.wait_until( |
| 116 |
lambda driver: driver.execute_script("return document.readyState;") |
| 117 |
== "complete", |
| 118 |
timeout, |
| 119 |
) |
| 120 |
|
| 121 |
@contextmanager |
| 122 |
def wait_page_loaded(self, timeout=10): |
| 123 |
""" |
| 124 |
Block until a new page has loaded and is ready. |
| 125 |
""" |
| 126 |
from selenium.webdriver.common.by import By |
| 127 |
from selenium.webdriver.support import expected_conditions as ec |
| 128 |
|
| 129 |
old_page = self.selenium.find_element(By.TAG_NAME, "html") |
| 130 |
yield |
| 131 |
# Wait for the next page to be loaded |
| 132 |
self.wait_until(ec.staleness_of(old_page), timeout=timeout) |
| 133 |
self.wait_page_ready(timeout=timeout) |
| 134 |
|
| 135 |
def admin_login(self, username, password, login_url="/admin/"): |
| 136 |
""" |
| 137 |
Log in to the admin. |
| 138 |
""" |
| 139 |
from selenium.webdriver.common.by import By |
| 140 |
|
| 141 |
self.selenium.get("%s%s" % (self.live_server_url, login_url)) |
| 142 |
username_input = self.selenium.find_element(By.NAME, "username") |
| 143 |
username_input.send_keys(username) |
| 144 |
password_input = self.selenium.find_element(By.NAME, "password") |
| 145 |
password_input.send_keys(password) |
| 146 |
login_text = _("Log in") |
| 147 |
with self.wait_page_loaded(): |
| 148 |
self.selenium.find_element( |
| 149 |
By.XPATH, '//input[@value="%s"]' % login_text |
| 150 |
).click() |
| 151 |
|
| 152 |
def select_option(self, selector, value): |
| 153 |
""" |
| 154 |
Select the <OPTION> with the value `value` inside the <SELECT> widget |
| 155 |
identified by the CSS selector `selector`. |
| 156 |
""" |
| 157 |
from selenium.webdriver.common.by import By |
| 158 |
from selenium.webdriver.support.ui import Select |
| 159 |
|
| 160 |
select = Select(self.selenium.find_element(By.CSS_SELECTOR, selector)) |
| 161 |
select.select_by_value(value) |
| 162 |
|
| 163 |
def deselect_option(self, selector, value): |
| 164 |
""" |
| 165 |
Deselect the <OPTION> with the value `value` inside the <SELECT> widget |
| 166 |
identified by the CSS selector `selector`. |
| 167 |
""" |
| 168 |
from selenium.webdriver.common.by import By |
| 169 |
from selenium.webdriver.support.ui import Select |
| 170 |
|
| 171 |
select = Select(self.selenium.find_element(By.CSS_SELECTOR, selector)) |
| 172 |
select.deselect_by_value(value) |
| 173 |
|
| 174 |
def assertCountSeleniumElements(self, selector, count, root_element=None): |
| 175 |
""" |
| 176 |
Assert number of matches for a CSS selector. |
| 177 |
|
| 178 |
`root_element` allow restriction to a pre-selected node. |
| 179 |
""" |
| 180 |
from selenium.webdriver.common.by import By |
| 181 |
|
| 182 |
root_element = root_element or self.selenium |
| 183 |
self.assertEqual( |
| 184 |
len(root_element.find_elements(By.CSS_SELECTOR, selector)), count |
| 185 |
) |
| 186 |
|
| 187 |
def _assertOptionsValues(self, options_selector, values): |
| 188 |
from selenium.webdriver.common.by import By |
| 189 |
|
| 190 |
if values: |
| 191 |
options = self.selenium.find_elements(By.CSS_SELECTOR, options_selector) |
| 192 |
actual_values = [] |
| 193 |
for option in options: |
| 194 |
actual_values.append(option.get_attribute("value")) |
| 195 |
self.assertEqual(values, actual_values) |
| 196 |
else: |
| 197 |
# Prevent the `find_elements(By.CSS_SELECTOR, …)` call from blocking |
| 198 |
# if the selector doesn't match any options as we expect it |
| 199 |
# to be the case. |
| 200 |
with self.disable_implicit_wait(): |
| 201 |
self.wait_until( |
| 202 |
lambda driver: not driver.find_elements( |
| 203 |
By.CSS_SELECTOR, options_selector |
| 204 |
) |
| 205 |
) |
| 206 |
|
| 207 |
def assertSelectOptions(self, selector, values): |
| 208 |
""" |
| 209 |
Assert that the <SELECT> widget identified by `selector` has the |
| 210 |
options with the given `values`. |
| 211 |
""" |
| 212 |
self._assertOptionsValues("%s > option" % selector, values) |
| 213 |
|
| 214 |
def assertSelectedOptions(self, selector, values): |
| 215 |
""" |
| 216 |
Assert that the <SELECT> widget identified by `selector` has the |
| 217 |
selected options with the given `values`. |
| 218 |
""" |
| 219 |
self._assertOptionsValues("%s > option:checked" % selector, values) |
| 220 |
|
| 221 |
def is_disabled(self, selector): |
| 222 |
""" |
| 223 |
Return True if the element identified by `selector` has the `disabled` |
| 224 |
attribute. |
| 225 |
""" |
| 226 |
from selenium.webdriver.common.by import By |
| 227 |
|
| 228 |
return ( |
| 229 |
self.selenium.find_element(By.CSS_SELECTOR, selector).get_attribute( |
| 230 |
"disabled" |
| 231 |
) |
| 232 |
== "true" |
| 233 |
) |