Enable High Contrast Mode in Firefox Playwright Tests
In this article
To enable high contrast mode in Firefox Playwright tests, you must emulate the forced-colors media feature via the browser context using the emulateMedia method. This allows you to verify that your web application adheres to accessibility standards (such as WCAG) and remains usable for users who rely on high-contrast system themes by overriding the browser's default rendering to simulate a high-contrast environment.
What You'll Need
- Node.js installed on your development machine.
- Playwright installed in your project (
npm install @playwright/test). - A test suite configured to run using the Firefox browser engine.
- Basic knowledge of JavaScript or TypeScript.
Step-by-Step
- Initialize your Playwright Test file: Create a new test file (e.g.,
accessibility.spec.ts) and import thetestandexpectfunctions from the@playwright/testpackage. - Define the Test Case: Create a
testblock. Since high contrast emulation is applied at the context level, you will need to interact with the browser context rather than just the page object. - Access the Browser Context: In your test, use the
page.context()method to get a reference to the current browser context. This is where environment-wide settings like screen size, locale, and media features are managed. - Apply the Media Emulation: Call the
emulateMedia({ colorScheme: 'dark' })or specifically target the forced-colors feature. To specifically trigger high contrast mode, useawait page.emulateMedia({ forcedColors: 'active' });. This tells the browser to act as if the operating system has a high contrast theme enabled. - Navigate to your Target URL: Use
await page.goto('your-url-here');after setting the emulation to ensure the page loads with the high contrast styles already active. - Verify Visuals or CSS: Use Playwright assertions to check if the correct high-contrast colors are applied. You can check the computed style of an element using
await page.locator('selector').evaluate(el => getComputedStyle(el).color)to ensure the colors have shifted to the forced-colors palette. - Perform a Visual Comparison: For the most accurate high contrast testing, use
await expect(page).toHaveScreenshot('high-contrast-home.png');. This compares the current high-contrast render against a known baseline image.
Common Mistakes
- Applying Emulation After Assertions: A common error is calling
emulateMediaafter the page has already been validated. Always set the media emulation before navigating to the page or before performing the specific check. - Confusing Dark Mode with High Contrast: Dark mode (
colorScheme: 'dark') is a preference for a dark palette, whereas High Contrast (forcedColors: 'active') is an accessibility feature that overrides specific colors to ensure maximum visibility. They are not the same. - Testing in Chromium Only: While Playwright supports multiple browsers, high contrast implementation can vary slightly between Chromium and Firefox. Ensure your config specifically includes
firefoxto validate cross-browser accessibility. - Ignoring CSS
forced-color-adjust: Developers often forget that if they useforced-color-adjust: nonein their CSS, the browser will ignore the high contrast mode for those elements. Testing should verify if this property is being overused, which can break accessibility.
Tips
- Use Global Setup: If every test in your suite requires high contrast mode, configure the emulation in the
playwright.config.tsfile under theuseobject to avoid repeating the code in every test file. - Combine with Accessibility Audits: Use the
@axe-core/playwrightlibrary alongside high contrast emulation to find programmatic accessibility violations while simultaneously checking visual contrast. - Test Multiple Palettes: While
forcedColors: 'active'is the primary trigger, remember that different operating systems (Windows vs macOS) handle high contrast differently. Use visual regression testing to capture these nuances. - Check Focus Indicators: High contrast mode is often used by keyboard-only users. Ensure your
:focusrings are clearly visible whenforcedColorsis active.
Troubleshooting
If the high contrast mode does not seem to be activating in your Firefox tests, check the following:
- Browser Version: Ensure you are using a recent version of Playwright and Firefox. Older versions of the browser engine may not fully support the
forced-colorsmedia query. - CSS Overrides: Inspect your application's CSS. If you see
forced-color-adjust: none;applied to thebodyor major containers, the browser is being explicitly told to ignore the high contrast settings. - Context Isolation: Ensure you are calling
emulateMediaon the correct context. If you are using multiple browser contexts in a single test, the emulation only applies to the one where the method was called. - Cache Issues: Occasionally, cached stylesheets can interfere with media query updates. Try clearing the browser cache or using an incognito-style context.
Frequently Asked Questions
Does forcedColors work on all browsers in Playwright?
Yes, Playwright supports emulateMedia across Chromium, Firefox, and WebKit, though the visual rendering of high contrast depends on how each browser engine implements the CSS specification.
Can I toggle high contrast mode mid-test?
Yes. You can call await page.emulateMedia({ forcedColors: 'active' }) and then later call await page.emulateMedia({ forcedColors: 'none' }) to switch back and compare the two states in a single test flow.
What is the difference between colorScheme and forcedColors?
colorScheme tells the browser whether the user prefers a light or dark theme. forcedColors simulates a system-level accessibility setting that overrides the website's colors with a limited, high-contrast palette.
How do I verify that high contrast is actually working?
The most reliable method is using toHaveScreenshot() for visual verification or using page.evaluate() to check if the computed CSS values match the expected high-contrast system colors.
Does this simulate a specific Windows High Contrast theme?
It simulates the forced-colors: active state defined in the CSS Media Queries Level 5 specification, which is the standard way browsers represent high contrast modes found in Windows and other OSs.