Responsive and Device Emulation Strategies

Users visit your site from many devices and screen sizes, so responsive behaviour is a core quality concern. Playwright can emulate different viewports and devices, helping you catch layout issues before they reach production.

Testing Responsive Layouts with Viewports and Devices

You can configure different projects for desktop and mobile profiles or set viewports directly in tests. This makes it easy to run the same scenarios across multiple layouts.

// playwright.config.ts (responsive projects)
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  projects: [
    {
      name: 'Desktop Chrome',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'Mobile Chrome',
      use: { ...devices['Pixel 5'] },
    },
  ],
});
// responsive-layout.spec.ts
import { test, expect } from '@playwright/test';

test('navigation collapses into mobile menu', async ({ page }) => {
  await page.setViewportSize({ width: 375, height: 812 });
  await page.goto('https://demo.myshop.com');

  await expect(page.getByRole('button', { name: 'Open menu' })).toBeVisible();
});
Note: Using projects for device profiles keeps configuration centralised while letting you reuse tests across layouts.
Tip: Focus responsive tests on high-impact components such as navigation bars, grids and critical forms.
Warning: Testing only at one desktop resolution can hide layout issues on tablets or small laptops.

Combining responsive checks with visual snapshots helps you detect layout regressions quickly when CSS changes.

Common Mistakes

Mistake 1 โ€” Only testing at one viewport size

This ignores many devices.

โŒ Wrong: Assuming that if it works at 1280px wide, it works everywhere.

โœ… Correct: Define a small set of representative viewports (mobile, tablet, desktop).

Mistake 2 โ€” Mixing device concerns with unrelated test logic

This complicates tests.

โŒ Wrong: Embedding lots of viewport changes inside unrelated scenarios.

โœ… Correct: Keep device-specific checks in clearly named tests or projects.

🧠 Test Yourself

How can you use Playwright to test responsive behaviour effectively?