Prompt
I'm building a responsive site and need screenshot tests for mobile and tablet layouts
Latest observation
Here’s a practical way to add screenshot tests for mobile and tablet layouts in a responsive site.
What to test
At minimum, capture screenshots at a few representative viewport sizes:
- Mobile
- 375×667 (iPhone SE / small phone)
- 390×844 (common modern phone)
- Tablet
- 768×1024 (iPad portrait)
- 1024×768 (iPad landscape)
You can expand later, but these usually cover the main layout breakpoints.
Recommended approach
Use a browser automation tool with visual regression support, such as:
- Playwright + screenshot assertions
- Cypress + image diff plugin
- Storybook + Chromatic
- Percy / Applitools for hosted visual testing
If you want something lightweight and strong for responsive layouts, Playwright is a great choice.
Example with Playwright
1) Install
npm i -D @playwright/test
npx playwright install
2) Create screenshot tests
// tests/responsive.spec.ts
import { test, expect } from '@playwright/test';
const viewports = [
{ name: 'mobile', width: 375, height: 667 },
{ name: 'tablet', width: 768, height: 1024 },
];
for (const vp of viewports) {
test(`homepage looks correct on ${vp.name}`, async ({ page }) => {
await page.setViewportSize({ width: vp.width, height: vp.height });
await page.goto('http://localhost:3000');
await expect(page).toHaveScreenshot(`${vp.name}-homepage.png`, {
fullPage: true,
});
});
}
3) Run tests
npx playwright test
The first run will generate baseline screenshots. Later runs compare against them.
Good practices for responsive screenshot tests
Test key pages and states
Don’t just test the homepage. Include:
- Navigation open/closed
- Forms
- Cards/grids
- Long content / edge cases
- Error states
Stabilize the page
To reduce flaky diffs:
- Disable animations
- Mock dynamic content/time
- Use consistent fonts
- Wait for network idle or specific UI readiness
- Avoid random content in screenshots
Use breakpoints, not every device
Test layout breakpoints rather than every phone/tablet model. This keeps tests manageable.
Keep screenshots focused
If full-page screenshots are too noisy, test specific sections/components instead.
If you use Storybook
A very effective workflow is:
- Build key responsive components in Storybook
- Capture screenshots per viewport using:
- Chromatic
- Playwright
- Loki
This is especially useful for component-level regression testing.
Suggested test matrix
A simple starting matrix:
| Layout | Viewport |
|---|---|
| Mobile | 375×667 |
| Mobile large | 390×844 |
| Tablet portrait | 768×1024 |
| Tablet landscape | 1024×768 |
If you want, I can also provide:
- a Playwright config for mobile/tablet screenshot testing,
- a Cypress version, or
- a setup tailored to React / Next.js / Vue / Angular.