mirror of https://github.com/django/django.git
Added a high contrast mode to screenshot cases.
Thank you to Sarah Abderemane and Nick Pope for the reviews.
This commit is contained in:
parent
914bf69171
commit
39828fa778
|
@ -191,6 +191,26 @@ class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase):
|
||||||
finally:
|
finally:
|
||||||
self.selenium.execute_script("localStorage.removeItem('theme');")
|
self.selenium.execute_script("localStorage.removeItem('theme');")
|
||||||
|
|
||||||
|
def set_emulated_media(self, features, media=""):
|
||||||
|
if self.browser != "chrome":
|
||||||
|
self.skipTest("Emulated media controls are only supported on Chrome.")
|
||||||
|
# Chrome Dev Tools Protocol Emulation.setEmulatedMedia
|
||||||
|
# https://chromedevtools.github.io/devtools-protocol/1-3/Emulation/#method-setEmulatedMedia
|
||||||
|
self.selenium.execute_cdp_cmd(
|
||||||
|
"Emulation.setEmulatedMedia", {"media": media, "features": features}
|
||||||
|
)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def high_contrast(self):
|
||||||
|
self.set_emulated_media(features=[{"name": "forced-colors", "value": "active"}])
|
||||||
|
with self.desktop_size():
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
self.set_emulated_media(
|
||||||
|
features=[{"name": "forced-colors", "value": "none"}]
|
||||||
|
)
|
||||||
|
|
||||||
def take_screenshot(self, name):
|
def take_screenshot(self, name):
|
||||||
if not self.screenshots:
|
if not self.screenshots:
|
||||||
return
|
return
|
||||||
|
|
|
@ -278,16 +278,16 @@ For testing changes to the admin UI, the selenium tests can be run with the
|
||||||
To define when screenshots should be taken during a selenium test, the test
|
To define when screenshots should be taken during a selenium test, the test
|
||||||
class must use the ``@django.test.selenium.screenshot_cases`` decorator with a
|
class must use the ``@django.test.selenium.screenshot_cases`` decorator with a
|
||||||
list of supported screenshot types (``"desktop_size"``, ``"mobile_size"``,
|
list of supported screenshot types (``"desktop_size"``, ``"mobile_size"``,
|
||||||
``"small_screen_size"``, ``"rtl"``, and ``"dark"``). It can then call
|
``"small_screen_size"``, ``"rtl"``, ``"dark"``, and ``"high_contrast"``). It
|
||||||
``self.take_screenshot("unique-screenshot-name")`` at the desired point to
|
can then call ``self.take_screenshot("unique-screenshot-name")`` at the desired
|
||||||
generate the screenshots. For example::
|
point to generate the screenshots. For example::
|
||||||
|
|
||||||
from django.test.selenium import SeleniumTestCase, screenshot_cases
|
from django.test.selenium import SeleniumTestCase, screenshot_cases
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
class SeleniumTests(SeleniumTestCase):
|
class SeleniumTests(SeleniumTestCase):
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
|
||||||
def test_login_button_centered(self):
|
def test_login_button_centered(self):
|
||||||
self.selenium.get(self.live_server_url + reverse("admin:login"))
|
self.selenium.get(self.live_server_url + reverse("admin:login"))
|
||||||
self.take_screenshot("login")
|
self.take_screenshot("login")
|
||||||
|
@ -295,7 +295,8 @@ generate the screenshots. For example::
|
||||||
|
|
||||||
This generates multiple screenshots of the login page - one for a desktop
|
This generates multiple screenshots of the login page - one for a desktop
|
||||||
screen, one for a mobile screen, one for right-to-left languages on desktop,
|
screen, one for a mobile screen, one for right-to-left languages on desktop,
|
||||||
and one for the dark mode on desktop.
|
one for the dark mode on desktop, and one for high contrast mode on desktop
|
||||||
|
when using chrome.
|
||||||
|
|
||||||
.. versionchanged:: 5.1
|
.. versionchanged:: 5.1
|
||||||
|
|
||||||
|
|
|
@ -5764,7 +5764,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
title="A Long Title", published=True, slug="a-long-title"
|
title="A Long Title", published=True, slug="a-long-title"
|
||||||
)
|
)
|
||||||
|
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
|
||||||
def test_login_button_centered(self):
|
def test_login_button_centered(self):
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
@ -6070,7 +6070,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
self.assertEqual(slug1, "this-is-the-main-name-the-best-2012-02-18")
|
self.assertEqual(slug1, "this-is-the-main-name-the-best-2012-02-18")
|
||||||
self.assertEqual(slug2, "option-two-this-is-the-main-name-the-best")
|
self.assertEqual(slug2, "option-two-this-is-the-main-name-the-best")
|
||||||
|
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "dark", "high_contrast"])
|
||||||
def test_collapsible_fieldset(self):
|
def test_collapsible_fieldset(self):
|
||||||
"""
|
"""
|
||||||
The 'collapse' class in fieldsets definition allows to
|
The 'collapse' class in fieldsets definition allows to
|
||||||
|
@ -6093,7 +6093,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
)
|
)
|
||||||
self.take_screenshot("expanded")
|
self.take_screenshot("expanded")
|
||||||
|
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
|
||||||
def test_selectbox_height_collapsible_fieldset(self):
|
def test_selectbox_height_collapsible_fieldset(self):
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
@ -6121,7 +6121,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
)
|
)
|
||||||
self.take_screenshot("selectbox-collapsible")
|
self.take_screenshot("selectbox-collapsible")
|
||||||
|
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
|
||||||
def test_selectbox_height_not_collapsible_fieldset(self):
|
def test_selectbox_height_not_collapsible_fieldset(self):
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
@ -6152,7 +6152,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
)
|
)
|
||||||
self.take_screenshot("selectbox-non-collapsible")
|
self.take_screenshot("selectbox-non-collapsible")
|
||||||
|
|
||||||
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark"])
|
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
|
||||||
def test_first_field_focus(self):
|
def test_first_field_focus(self):
|
||||||
"""JavaScript-assisted auto-focus on first usable form field."""
|
"""JavaScript-assisted auto-focus on first usable form field."""
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
Loading…
Reference in New Issue