Visual Testing for Mobile and Responsive Web Apps

Visual Testing for Mobile and Responsive Web Apps

Responsive web applications render differently across screen sizes, orientations, and device pixel ratios. A layout that looks perfect at 1280px can break at 375px. A navigation that works fine in landscape might overflow in portrait. Visual regression testing for responsive apps means catching these breaks before they reach production.

This guide covers viewport-based visual testing, device emulation, and cross-browser visual testing for mobile breakpoints.

The Challenge of Responsive Visual Testing

A typical app has three or more breakpoints: mobile (320-480px), tablet (768-1024px), desktop (1280px+). Each breakpoint can have completely different layouts — stacked columns, hidden navigation, different font sizes, reordered content.

Testing all permutations manually isn't feasible. Visual regression catches regressions across every defined breakpoint automatically, on every PR.

Key concerns:

  • Viewport size: Same HTML, different CSS breakpoint applied
  • Device pixel ratio (DPR): Retina screens (DPR 2-3) render at higher resolution
  • Touch vs mouse: Some CSS uses hover states that don't apply on touch
  • Font rendering: Mobile OS font rendering differs from desktop
  • Image loading: Responsive images (srcset) may load different assets per viewport

Testing Viewports with Playwright

Playwright supports viewport configuration per test. Use it to snapshot at multiple breakpoints.

const { test, expect } = require('@playwright/test')

const viewports = [
  { name: 'mobile', width: 375, height: 812 },      // iPhone 13
  { name: 'tablet', width: 768, height: 1024 },     // iPad
  { name: 'desktop', width: 1280, height: 800 }
]

for (const viewport of viewports) {
  test(`homepage - ${viewport.name}`, async ({ page }) => {
    await page.setViewportSize({ width: viewport.width, height: viewport.height })
    await page.goto('/')
    await page.waitForLoadState('networkidle')
    
    await expect(page).toHaveScreenshot(`homepage-${viewport.name}.png`, {
      fullPage: true,
      animations: 'disabled'
    })
  })
}

Device Emulation

Playwright includes built-in device descriptors that set viewport, user agent, DPR, and touch support together:

const { devices } = require('@playwright/test')

const iphone13 = devices['iPhone 13']
const pixel5 = devices['Pixel 5']
const ipadPro = devices['iPad Pro 11']

test('mobile navigation - iPhone 13', async ({ playwright }) => {
  const browser = await playwright.chromium.launch()
  const context = await browser.newContext({
    ...iphone13,
    locale: 'en-US'
  })
  const page = await context.newPage()
  
  await page.goto('/')
  await page.waitForLoadState('networkidle')
  
  const screenshot = await page.screenshot({ fullPage: true })
  // compare with your visual testing tool
})

Full list of built-in devices: require('@playwright/test').devices — includes 100+ device presets.

Playwright Config for Multi-Device

// playwright.config.js
const { defineConfig, devices } = require('@playwright/test')

module.exports = defineConfig({
  projects: [
    {
      name: 'Desktop Chrome',
      use: { ...devices['Desktop Chrome'], viewport: { width: 1280, height: 800 } }
    },
    {
      name: 'Mobile Safari',
      use: { ...devices['iPhone 13'] }
    },
    {
      name: 'Tablet',
      use: { ...devices['iPad Pro 11'] }
    },
    {
      name: 'Mobile Android',
      use: { ...devices['Pixel 5'] }
    }
  ],
  snapshotPathTemplate: '{testDir}/snapshots/{projectName}/{testFilePath}/{arg}{ext}'
})

This runs every test file across all four device configurations, with separate snapshot directories per project.

Testing Viewports with Cypress

// cypress/e2e/visual/responsive.cy.js
const breakpoints = {
  mobile: [375, 812],
  tablet: [768, 1024],
  desktop: [1280, 800]
}

Object.entries(breakpoints).forEach(([name, [width, height]]) => {
  describe(`Responsive - ${name}`, () => {
    beforeEach(() => {
      cy.viewport(width, height)
    })

    it('homepage layout', () => {
      cy.visit('/')
      cy.waitForLoadState?.() || cy.wait(1000)
      cy.matchImageSnapshot(`homepage-${name}`)
    })

    it('navigation', () => {
      cy.visit('/')
      cy.get('nav').matchImageSnapshot(`nav-${name}`)
    })

    it('footer', () => {
      cy.visit('/')
      cy.get('footer').matchImageSnapshot(`footer-${name}`)
    })
  })
})

Mobile-Specific Interactions

Mobile breakpoints often show a hamburger menu instead of a full navigation. Test both states:

describe('Mobile navigation', () => {
  beforeEach(() => cy.viewport(375, 812))

  it('hamburger menu closed', () => {
    cy.visit('/')
    cy.matchImageSnapshot('mobile-nav-closed')
  })

  it('hamburger menu open', () => {
    cy.visit('/')
    cy.get('[data-testid="hamburger"]').click()
    cy.get('[data-testid="mobile-menu"]').should('be.visible')
    cy.matchImageSnapshot('mobile-nav-open')
  })
})

Percy Multi-Viewport Testing

Percy natively supports multiple viewport snapshots in a single test run:

// cypress/e2e/visual/percy-responsive.cy.js
it('homepage responsive', () => {
  cy.visit('/')
  cy.percySnapshot('Homepage', {
    widths: [375, 768, 1280]  // Percy renders all three
  })
})

Percy renders the page at each specified width and shows them side by side in its review dashboard. One test, three viewport snapshots, all reviewable in one place.

This is the most efficient approach for teams using Percy — no need to run the test three times.

Applitools Cross-Browser + Device Testing

Applitools Ultrafast Grid renders visual checkpoints across real browsers and devices simultaneously:

// applitools.config.js
module.exports = {
  testConcurrency: 5,
  browser: [
    // Desktop browsers
    { width: 1280, height: 800, name: 'chrome' },
    { width: 1280, height: 800, name: 'firefox' },
    { width: 1280, height: 800, name: 'safari' },
    
    // Mobile devices (real device rendering)
    { deviceName: 'iPhone 13', screenOrientation: 'portrait' },
    { deviceName: 'iPhone 13', screenOrientation: 'landscape' },
    { deviceName: 'Pixel 5', screenOrientation: 'portrait' },
    { deviceName: 'iPad Air 2', screenOrientation: 'landscape' }
  ]
}

A single Cypress or Playwright test run produces visual results across all configured browser/device combinations. This is the most comprehensive approach but requires an Applitools subscription.

Key Areas to Test at Mobile Breakpoints

Not every page element needs visual testing at every breakpoint. Focus on the high-value, high-risk areas:

Mobile navigation is usually completely different from desktop — hamburger menus, drawers, bottom tabs. Always test:

  • Closed state
  • Open/expanded state
  • Active/current page highlighting
  • Overflow behavior with long menu items

Typography

Font sizes, line heights, and text truncation often need breakpoint-specific values. Test:

  • Headings at each breakpoint
  • Body text readability
  • Text overflow/truncation in constrained containers

Images

Responsive images may load different assets via srcset. Verify:

  • Hero images crop correctly on mobile
  • Product images maintain aspect ratio
  • Icons remain sharp on high-DPR screens

Forms

Forms are notorious for mobile layout issues:

  • Input fields that overflow their container
  • Submit buttons that become too small to tap
  • Dropdown menus that don't scroll properly

Tables and Data Grids

Tables break at small viewports. Test:

  • Horizontal scroll behavior
  • Column hiding/stacking
  • Card view fallbacks for data tables

Handling Retina/High-DPR Screens

High device pixel ratio screens (Retina, HDPI) require handling in screenshots:

// Playwright — set deviceScaleFactor
const context = await browser.newContext({
  viewport: { width: 375, height: 812 },
  deviceScaleFactor: 2  // Retina
})

// Cypress — configure in cypress.json
// devicePixelRatio is not directly configurable in Cypress
// Use Percy or Applitools for true DPR testing

For consistent baseline management, pick one DPR and stick with it. Mixing DPR values across baseline captures produces false positives.

CI/CD for Multi-Viewport Visual Tests

Multi-viewport tests run longer than single-viewport tests. Optimize:

# GitHub Actions — parallel by viewport
jobs:
  visual-mobile:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm ci
      - run: npx playwright test --project="Mobile Safari"

  visual-tablet:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm ci
      - run: npx playwright test --project="Tablet"

  visual-desktop:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm ci
      - run: npx playwright test --project="Desktop Chrome"

Running viewport projects in parallel cuts total CI time roughly by the number of parallel jobs.

Common Pitfalls

Viewport-specific animations: CSS animations that only trigger at certain breakpoints cause flaky snapshots. Disable all animations in test environments:

// Inject CSS to disable animations
await page.addStyleTag({
  content: `
    *, *::before, *::after {
      animation-duration: 0s !important;
      transition-duration: 0s !important;
    }
  `
})

Scroll position: On mobile, sticky headers and bottom bars affect layout. Always scrollTo(0, 0) before snapping full pages.

Font loading: Mobile tests often run faster than font loading completes. Wait for fonts:

await page.waitForFunction(() => document.fonts.ready)

Soft keyboards: Device emulation doesn't emulate soft keyboard behavior. Forms that open the keyboard (and resize the viewport) can't be reliably tested with screenshots alone.

Summary

Responsive visual regression testing means running visual checks across every defined breakpoint. The right tool depends on your team:

  • Playwright multi-project: Best for teams already on Playwright, no external dependencies
  • Percy with widths: Most efficient for Percy users — one test, multiple viewports reviewed together
  • Applitools Ultrafast Grid: Best for teams needing real cross-browser + real device rendering

Whichever tool you use, test mobile breakpoints from day one. Responsive regressions are easy to introduce and hard to catch without automated visual checks.

Read more

Start now free