From 39828fa7786a805f7a542abb929f352d3c5b722c Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:28:34 +0200 Subject: Added a high contrast mode to screenshot cases. Thank you to Sarah Abderemane and Nick Pope for the reviews. --- django/test/selenium.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'django/test') diff --git a/django/test/selenium.py b/django/test/selenium.py index 07df34ae1f..eecfd87bf1 100644 --- a/django/test/selenium.py +++ b/django/test/selenium.py @@ -191,6 +191,26 @@ class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase): finally: 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): if not self.screenshots: return -- cgit v1.3