Sub topic 11
Testing both the baseline and the enhanced experience
This chapter assumes you can write @supports rules and baseline-first CSS, and that you know the cascade rules from earlier chapters. You also need basic JavaScript and a command line. The examples use Playwright, but the ideas carry over to any browser test tool.
Why you need to test both paths
A progressive enhancement has two outputs. The baseline is what a browser gets when the @supports condition is false. The enhanced version is what it gets when the condition is true. Most developers only see the enhanced version, because their daily browser supports everything. The baseline then goes untested and breaks without anyone noticing.
Typical baseline failures:
- A fallback declaration is overridden by an enhancement that sits outside the
@supportsblock. - The baseline depends on a property that the enhanced branch silently removes.
- Text is unreadable because the baseline colour was only ever viewed with a blur behind it.
- Layout overflows because the flexbox fallback was never checked at narrow widths.
The goal is to run each code path on purpose, not to wait for a user on an old browser to report a bug.
What each path must prove
The two paths have different pass criteria.
| Path | Question to answer | Example check |
|---|---|---|
| Baseline | Is the page fully usable without the enhancement? | Gallery items wrap and remain readable. |
| Enhanced | Does the enhancement actually apply and look right? | Gallery uses a grid with equal columns. |
| Both | Does nothing leak from one path into the other? | No leftover `float` or `width` conflicts. |
The baseline does not have to look identical to the enhanced version. It has to work. A flexbox gallery with slightly uneven last-row items is acceptable. A gallery whose items overlap is a bug.
Setting up a small test page
Here is a minimal page that uses the gallery pattern from earlier in the tutorial. Save the first file as index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gallery test</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="gallery">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
</div>
</body>
</html>Save this as styles.css in the same folder:
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}Serve the folder with python3 -m http.server 8080 and open http://localhost:8080/index.html. In a current browser you will see the grid branch.
Forcing the baseline
Your browser supports grid, so the condition is true and you cannot see the fallback. You need a way to make the condition false without changing the browser. These are the options, from quickest to most realistic.
Toggle declarations in DevTools
In the Styles pane of your browser's developer tools, you can untick individual declarations. Unticking display: grid and grid-template-columns inside the @supports block shows you roughly what the baseline looks like. This is fast and good for exploring. It is not repeatable, and it does not prove that the real condition evaluates to false in a real old browser.
Poison the condition
Temporarily change the condition to something no browser supports:
@supports (display: force-baseline) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}A declaration with an invalid value never matches, so every browser takes the baseline path. Reload the page and you see what an old browser sees. This tests your actual cascade, source order and specificity, which DevTools unticking does not fully do. The weakness is that you must remember to revert the edit.
Automate the poisoning
You can avoid the manual edit by letting a test tool rewrite the stylesheet in transit. The Playwright test below loads the page twice. In one test it rewrites the condition so the baseline applies. In the other it leaves the stylesheet alone.
Install the tool with npm init -y, then npm install -D @playwright/test, then npx playwright install chromium. Save this as gallery.spec.js:
const { test, expect } = require('@playwright/test');
const PAGE_URL = 'http://localhost:8080/index.html';
async function forceBaseline(page) {
await page.route('**/styles.css', async (route) => {
const response = await route.fetch();
const css = await response.text();
await route.fulfill({
response,
body: css.replaceAll(
'@supports (display: grid)',
'@supports (display: force-baseline)'
),
});
});
}
async function galleryDisplay(page) {
return page
.locator('.gallery')
.evaluate((el) => getComputedStyle(el).display);
}
test('baseline: gallery uses flexbox', async ({ page }) => {
await forceBaseline(page);
await page.goto(PAGE_URL);
expect(await galleryDisplay(page)).toBe('flex');
});
test('enhanced: gallery uses grid', async ({ page }) => {
await page.goto(PAGE_URL);
expect(await galleryDisplay(page)).toBe('grid');
});With the server running, run npx playwright test. Both tests should pass in Chromium. The baseline test fails if someone moves the grid declaration outside the @supports block, because then the rewrite no longer reaches it and the gallery stays a grid. That is the kind of regression you want to catch.
Use real older or different browsers
Poisoning conditions covers your cascade. It cannot catch engine-specific bugs, such as a flexbox gap rendering differently in an older browser. For that, run the page in real browsers. Options include older versions installed locally, cloud device services, and the Firefox and WebKit engines that Playwright ships. Playwright can run the same spec against all three:
// playwright.config.js
module.exports = {
projects: [
{ name: 'chromium', use: { browserName: 'chromium' } },
{ name: 'firefox', use: { browserName: 'firefox' } },
{ name: 'webkit', use: { browserName: 'webkit' } },
],
};Run npx playwright install once to download all three engines. If one engine lacks a feature, the enhanced test fails there. Decide for each failure whether the expectation is wrong or the support gap is real, and then change the test or the CSS.
Choosing the right technique
| Technique | Repeatable | Tests your cascade | Tests real engines |
|---|---|---|---|
| Untick in DevTools | No | Partly | No |
| Poison the condition by hand | Yes, if you remember to revert | Yes | No |
| Rewrite in an automated test | Yes | Yes | No |
| Real older or other browsers | Yes | Yes | Yes |
Use the first row while you explore, the third row in continuous integration, and the fourth row before a release.
What to check on the baseline
When the baseline path is active, work through this list:
- Content is reachable. Nothing is hidden, clipped or overlapping.
- Text is legible. Contrast ratios still pass without the enhancement.
- Interaction works. Buttons, links and form fields can be used with keyboard and pointer.
- Layout survives narrow and wide viewports. Resize from about 320 pixels up to a large desktop width.
- No console errors. CSS does not log errors, but JavaScript that depends on a class added by
CSS.supports()might.
The translucent panel pattern shows why the contrast check matters. The baseline background is rgb(255 255 255 / 0.95), which is nearly opaque so text stays readable. The enhanced version uses 0.6 opacity and relies on the blur to keep text readable. If you only tested the enhanced version, you might never discover that the baseline is fine, or you might reverse the values by mistake and ship an unreadable baseline. Check both.
What to check on the enhanced path
When the condition is true, confirm that:
- the enhanced rule really wins the cascade, which a computed-style assertion proves;
- the enhancement does not leave stray baseline properties that conflict with it, such as a
flexvalue on a grid child that changes its size unexpectedly; - the page does not shift or flash when the enhanced rules load.
In the gallery example, .gallery > * { flex: 1 1 12rem; } stays active after the grid rule applies. Flex properties are ignored on grid items, so it is harmless here. You should still verify this once and not assume it.
Keeping JavaScript checks consistent
If your script calls CSS.supports() and adds a class, that class is a second source of truth next to the @supports rule. Test that the two agree. This test runs in a real browser and compares the JavaScript answer with the computed style:
const { test, expect } = require('@playwright/test');
test('CSS.supports agrees with applied styles', async ({ page }) => {
await page.goto('http://localhost:8080/index.html');
const result = await page.evaluate(() => ({
supported: CSS.supports('display', 'grid'),
display: getComputedStyle(document.querySelector('.gallery')).display,
}));
expect(result.supported).toBe(result.display === 'grid');
});If the two ever disagree, you have either a typo in one of the conditions or a condition that differs between the CSS and the script. Keep the strings identical and, where possible, define the condition in one place.
Visual and manual review
Computed-style assertions confirm which branch applied. They do not confirm that it looks right. Add one of these for the visual side:
- Screenshot comparison. Take a screenshot per path, for example
await expect(page).toHaveScreenshot('baseline.png'). The first run stores the reference image, and later runs compare against it. Store separate reference images for baseline and enhanced. - Manual pass. Before a release, open the page with the condition poisoned and read through it once.
Screenshots differ slightly between operating systems and engines. Run them in the same environment each time, such as one CI container, or you will see false failures.
A routine you can keep
A lightweight routine that scales to a real project:
- Write the baseline first and check it with no enhancement at all.
- Add the enhancement inside
@supportsand check the enhanced path. - Add an automated test per path that asserts the computed style.
- Run the suite in at least two engines in continuous integration.
- Before release, do a manual baseline pass with the condition poisoned and a spot check in one older real browser.
Common mistakes in testing
- Asserting only on the enhanced path. The baseline can rot unnoticed.
- Forgetting to revert a hand-poisoned condition. Automated rewriting avoids this.
- Matching the rewrite string loosely. If you change the condition text in the CSS, the
replaceAllin the test silently stops matching, and the baseline test then fails for the wrong reason or passes without testing anything. Keep the strings in step, and consider asserting that the rewrite changed something. - Testing one viewport. Baseline layouts often fail only at narrow or wide sizes.
- Treating "looks different" as "broken". Define what the baseline must do before you judge it.
Exercise
Your team's @supports (backdrop-filter: blur(8px)) rule is tested only in the latest Chrome, and all tests pass. List two concrete things that could still be wrong, and name one technique that would catch each.
Show answer
First, the baseline panel background (rgb(255 255 255 / 0.95)) might be unreadable or wrong in browsers without backdrop-filter. Force the baseline by poisoning the condition, either by hand or with a test that rewrites the stylesheet, and check contrast and layout. Second, an engine-specific rendering difference, such as how Firefox or Safari handle the prefixed property, might break the enhanced look. Run the same spec in Firefox and WebKit, or in a real older browser, to catch it.
Exercise
A Playwright baseline test rewrites @supports (display: grid) to @supports (display: force-baseline). A teammate later changes the CSS to @supports (display:grid) with no space. The baseline test still passes in Chromium on a grid-capable page. Why is that a problem, and what should you change?
Show answer
The rewrite string no longer matches, so the stylesheet is served unchanged. The grid branch applies, and the test's assertion should then fail with 'grid' instead of 'flex'. If it passes, the assertion is wrong, or the test checks something the grid branch also satisfies. Either way, the baseline path is not being exercised. Make the rewrite more robust, for example with a regular expression that tolerates whitespace, assert that the CSS text actually changed, and keep the assertion on computed display equal to 'flex'.