test_skip_link_to_content.py 5.1 KB

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