Debian Codegraph @

tests/admin_views/test_skip_link_to_content.py

raw
1 from django.contrib.admin.tests import AdminSeleniumTestCase
2 from django.contrib.auth.models import User
3 from django.test import override_settings
4 from django.urls import reverse
5
6
7 @override_settings(ROOT_URLCONF="admin_views.urls")
8 class SeleniumTests(AdminSeleniumTestCase):
9 available_apps = ["admin_views"] + AdminSeleniumTestCase.available_apps
10
11 def setUp(self):
12 self.superuser = User.objects.create_superuser(
13 username="super",
14 password="secret",
15 email="[email protected]",
16 )
17
18 def test_use_skip_link_to_content(self):
19 from selenium.webdriver.common.action_chains import ActionChains
20 from selenium.webdriver.common.by import By
21 from selenium.webdriver.common.keys import Keys
22
23 self.admin_login(
24 username="super",
25 password="secret",
26 login_url=reverse("admin:index"),
27 )
28
29 # `Skip link` is not present.
30 skip_link = self.selenium.find_element(By.CLASS_NAME, "skip-to-content-link")
31 self.assertFalse(skip_link.is_displayed())
32
33 # 1st TAB is pressed, `skip link` is shown.
34 body = self.selenium.find_element(By.TAG_NAME, "body")
35 body.send_keys(Keys.TAB)
36 self.assertTrue(skip_link.is_displayed())
37
38 # Press RETURN to skip the navbar links (view site / documentation /
39 # change password / log out) and focus first model in the admin_views list.
40 skip_link.send_keys(Keys.RETURN)
41 self.assertFalse(skip_link.is_displayed()) # `skip link` disappear.
42 keys = [Keys.TAB, Keys.TAB] # The 1st TAB is the section title.
43 if self.browser == "firefox":
44 # For some reason Firefox doesn't focus the section title ('ADMIN_VIEWS').
45 keys.remove(Keys.TAB)
46 body.send_keys(keys)
47 actors_a_tag = self.selenium.find_element(By.LINK_TEXT, "Actors")
48 self.assertEqual(self.selenium.switch_to.active_element, actors_a_tag)
49
50 # Go to Actors changelist, skip sidebar and focus "Add actor +".
51 with self.wait_page_loaded():
52 actors_a_tag.send_keys(Keys.RETURN)
53 body = self.selenium.find_element(By.TAG_NAME, "body")
54 body.send_keys(Keys.TAB)
55 skip_link = self.selenium.find_element(By.CLASS_NAME, "skip-to-content-link")
56 self.assertTrue(skip_link.is_displayed())
57 ActionChains(self.selenium).send_keys(Keys.RETURN, Keys.TAB).perform()
58 actors_add_url = reverse("admin:admin_views_actor_add")
59 actors_a_tag = self.selenium.find_element(
60 By.CSS_SELECTOR, f"#content [href='{actors_add_url}']"
61 )
62 self.assertEqual(self.selenium.switch_to.active_element, actors_a_tag)
63
64 # Go to the Actor form and the first input will be focused automatically.
65 with self.wait_page_loaded():
66 actors_a_tag.send_keys(Keys.RETURN)
67 first_input = self.selenium.find_element(By.ID, "id_name")
68 self.assertEqual(self.selenium.switch_to.active_element, first_input)
69
70 def test_dont_use_skip_link_to_content(self):
71 from selenium.webdriver.common.by import By
72 from selenium.webdriver.common.keys import Keys
73
74 self.admin_login(
75 username="super",
76 password="secret",
77 login_url=reverse("admin:index"),
78 )
79
80 # `Skip link` is not present.
81 skip_link = self.selenium.find_element(By.CLASS_NAME, "skip-to-content-link")
82 self.assertFalse(skip_link.is_displayed())
83
84 # 1st TAB is pressed, `skip link` is shown.
85 body = self.selenium.find_element(By.TAG_NAME, "body")
86 body.send_keys(Keys.TAB)
87 self.assertTrue(skip_link.is_displayed())
88
89 # The 2nd TAB will focus the page title.
90 body.send_keys(Keys.TAB)
91 django_administration_title = self.selenium.find_element(
92 By.LINK_TEXT, "Django administration"
93 )
94 self.assertFalse(skip_link.is_displayed()) # `skip link` disappear.
95 self.assertEqual(
96 self.selenium.switch_to.active_element, django_administration_title
97 )
98
99 def test_skip_link_with_RTL_language_doesnt_create_horizontal_scrolling(self):
100 from selenium.webdriver.common.by import By
101 from selenium.webdriver.common.keys import Keys
102
103 with override_settings(LANGUAGE_CODE="ar"):
104 self.admin_login(
105 username="super",
106 password="secret",
107 login_url=reverse("admin:index"),
108 )
109
110 skip_link = self.selenium.find_element(
111 By.CLASS_NAME, "skip-to-content-link"
112 )
113 body = self.selenium.find_element(By.TAG_NAME, "body")
114 body.send_keys(Keys.TAB)
115 self.assertTrue(skip_link.is_displayed())
116
117 is_vertical_scrolleable = self.selenium.execute_script(
118 "return arguments[0].scrollHeight > arguments[0].offsetHeight;", body
119 )
120 is_horizontal_scrolleable = self.selenium.execute_script(
121 "return arguments[0].scrollWeight > arguments[0].offsetWeight;", body
122 )
123 self.assertTrue(is_vertical_scrolleable)
124 self.assertFalse(is_horizontal_scrolleable)