TechBuzzPro

Dark Color Scheme for Playwright Screenshots

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When you capture screenshots with Playwright, the default color scheme usually matches your operating system or browser settings. If you need consistent dark-themed screenshots for documentation, visual regression testing, or demos, you can force the browser to render pages in dark mode. This guide shows you how to set a dark color scheme for Playwright screenshots using the built-in colorScheme option, plus how to handle pages that don't respond to it.

What You'll Need

  • A Node.js project with Playwright installed (npm install @playwright/test)
  • A basic understanding of writing Playwright test or script files
  • Access to a terminal/command prompt

Step-by-Step

  1. Create a new Playwright script or test file (e.g., screenshot.js or screenshot.spec.js). Open it in your code editor.
  2. Import Playwright's modules. For a script, use const { chromium } = require('@playwright/test'); or import { chromium } from '@playwright/test'; if using ES modules. For a test file, use const { test, expect } = require('@playwright/test');.
  3. Launch a browser with the dark color scheme. The simplest way is to pass colorScheme: 'dark' when launching the browser context. For example:
    const browser = await chromium.launch();
    const context = await browser.newContext({ colorScheme: 'dark' });
    const page = await context.newPage();
    In a test, you can set it in the test options: test.use({ colorScheme: 'dark' }); at the top of your test file, or inside a specific test using test('...', async ({ browser }) => { const context = await browser.newContext({ colorScheme: 'dark' }); ... }).
  4. Navigate to the page you want to screenshot. Use await page.goto('https://example.com');. Wait for any dynamic content to load if needed.
  5. Take the screenshot. Use await page.screenshot({ path: 'dark-screenshot.png' });. You can add options like fullPage: true for full-page captures.
  6. Close the browser. After the screenshot, call await browser.close(); to clean up resources.
  7. Run your script. Execute node screenshot.js or npx playwright test if using the test runner. Check the generated image to confirm the dark scheme is applied.

Common Mistakes

  • Forgetting to set the color scheme on the context. Setting it only on the browser launch (e.g., chromium.launch({ colorScheme: 'dark' })) does not work; it must be on the newContext() options or via test.use().
  • Assuming all websites support dark mode. Some sites ignore the prefers-color-scheme media query and always render in light mode. The color scheme option only affects sites that respond to it.
  • Not waiting for the page to fully load. If the page has async styles or images, the screenshot may capture a partially rendered page. Use await page.waitForLoadState('networkidle') or wait for specific elements.
  • Using the wrong import path. Ensure you're importing from @playwright/test (for tests) or playwright (for scripts) correctly. Mixing them can cause errors.

Tips

  • Combine with other emulation options. You can set viewport, locale, or timezoneId in the same context to make screenshots more realistic.
  • Use a project-wide configuration. In playwright.config.ts, you can set use: { colorScheme: 'dark' } to apply it to all tests by default.
  • Override per test if needed. If you have a few tests that need light mode, use test.use({ colorScheme: 'light' }) inside that test's describe block.
  • Test both schemes. For visual regression, take screenshots in both dark and light modes to ensure your UI looks good in both.
  • Check the rendered result. Open the screenshot in an image viewer to verify the dark theme is applied. If not, the site may be using a different mechanism (e.g., a toggle button) that you need to interact with.

Troubleshooting

  • Screenshot still shows light mode. First, confirm you set colorScheme: 'dark' on the context. If you did, the website might not support dark mode. Try a site known to support dark mode (e.g., https://developer.mozilla.org) to verify your setup. If that works, the issue is with the specific site.
  • Error: "colorScheme: expected one of light, dark, no-preference". This means you passed an invalid value. Use exactly 'dark' or 'light'. Also, ensure you're not passing it to launch() instead of newContext().
  • Playwright doesn't launch the browser. Check that you have browsers installed (npx playwright install). Also, ensure your Node.js version is compatible (Playwright 1.30+ requires Node 14 or higher).
  • The page takes too long to load. Increase the timeout in page.goto() or use waitUntil: 'domcontentloaded' to speed up navigation. If the page has heavy resources, consider waiting for specific selectors instead of network idle.
  • Dark mode is applied but some elements are still light. This is common when a site uses inline styles or doesn't fully implement dark mode. You may need to manually override styles using page.addStyleTag() or use a custom script to force dark mode.

Frequently Asked Questions

Does colorScheme work with all browsers?

Yes, Playwright supports colorScheme for Chromium, Firefox, and WebKit. However, the behavior may vary slightly; for example, Firefox might ignore it if the site uses a specific preference.

Can I set the color scheme per screenshot without creating a new context?

No, the color scheme is a property of the browser context. You would need to create a new context with the desired scheme for each screenshot, or use test.use() to change it per test.

Does this affect the screenshot if the site uses a toggle button?

No, colorScheme only simulates the user's preference. If the site has a manual dark mode toggle (e.g., a button), you'll need to click it using Playwright before taking the screenshot.

Can I take screenshots in both light and dark modes in the same test?

Yes, you can create two contexts within the same test, each with a different colorScheme, and take screenshots from each.

Does colorScheme affect the screenshot of a PDF or other non-HTML content?

No, it only affects HTML pages. For PDFs, the rendering is based on the PDF's own styles.

Is there a way to force dark mode on sites that don't support it?

You can inject a style tag that applies a dark background and light text to the body, but this is a hack and may not look good on all sites. A better approach is to use the browser's built-in forced dark mode, but Playwright doesn't expose that directly; you'd need to emulate it with CSS.