Playwright Mobile Emulation Tutorial: Complete Guide for Responsive Testing with TypeScript

Introduction

Modern web applications must work seamlessly on desktops, tablets, and smartphones. A website that looks perfect on a laptop may have layout issues, broken navigation, or usability problems on smaller screens. This is where a Playwright Mobile Emulation Tutorial becomes valuable.

Playwright includes powerful built-in mobile device emulation that allows you to simulate popular Android phones, iPhones, iPads, and custom devices without needing physical hardware. Using Playwright Mobile Testing, you can validate responsive layouts, touch interactions, orientation changes, geolocation, permissions, and mobile browser behavior directly from your automation framework.

In this Playwright Mobile Emulation Tutorial, you’ll learn how to emulate mobile devices with TypeScript, configure device profiles, test responsive user interfaces, debug mobile issues, integrate tests into CI/CD pipelines, and apply enterprise automation best practices.


What Is Mobile Emulation in Playwright?

Mobile emulation is the process of simulating a mobile device within a desktop browser. Playwright changes browser characteristics such as:

  • Viewport size
  • User agent
  • Device scale factor
  • Touch support
  • Mobile browser behavior

This enables realistic testing without requiring actual phones or tablets.

Mobile Testing Workflow

Playwright Test

        │

        ▼

Device Profile

        │

        ▼

Chromium / Firefox / WebKit

        │

        ▼

Responsive Website

        │

        ▼

Assertions & Reports

Although emulation closely matches real devices, it’s still a simulation. For hardware-specific features or performance testing, complement emulation with testing on physical devices.


Why Use Mobile Emulation for Responsive Testing?

Responsive testing ensures your application works correctly across different screen sizes and orientations.

Benefits

  • Validate responsive layouts
  • Detect CSS issues
  • Test mobile navigation menus
  • Verify forms on small screens
  • Simulate touch interactions
  • Reduce testing costs
  • Faster execution than physical devices
  • Easy integration into CI/CD pipelines
Desktop TestingMobile Emulation
Large viewportMobile viewport
Mouse interactionTouch interaction
Desktop layoutResponsive layout
Standard user agentMobile user agent

Prerequisites (Node.js, Playwright, TypeScript, VS Code)

Before starting this Playwright Mobile Emulation Tutorial, install:

  • Node.js (LTS version)
  • npm
  • Playwright
  • TypeScript
  • Visual Studio Code

Verify Node.js:

node -v

npm -v

Create a Playwright project:

npm init playwright@latest

Choose TypeScript when prompted.


Setting Up a Playwright TypeScript Project

A recommended project structure:

playwright-mobile-project

├── tests

├── pages

├── fixtures

├── utils

├── playwright.config.ts

├── package.json

├── tsconfig.json

├── playwright-report

└── test-results

Folder Overview

FolderPurpose
testsTest scripts
pagesPage Object Model classes
fixturesShared setup
utilsHelper methods
playwright-reportHTML reports
test-resultsScreenshots, videos, traces

This structure scales well for both small and enterprise automation projects.


Using Built-in Device Descriptors (devices)

Playwright provides predefined device profiles through the devices object.

Example configuration:

import { defineConfig, devices } from ‘@playwright/test’;

export default defineConfig({

  projects: [

    {

      name: ‘iPhone 14’,

      use: {

        …devices[‘iPhone 14’]

      }

    }

  ]

});

Step-by-Step Explanation

  1. Import devices from Playwright.
  2. Create a browser project.
  3. Apply the built-in device profile.
  4. Playwright automatically configures viewport, user agent, and touch support.

This is the simplest way to start Playwright Device Emulation.


Emulating iPhone, Android, iPad, and Custom Devices

iPhone Example

projects: [

  {

    name: ‘iPhone 13’,

    use: {

      …devices[‘iPhone 13’]

    }

  }

]


Android Example

projects: [

  {

    name: ‘Pixel 7’,

    use: {

      …devices[‘Pixel 7’]

    }

  }

]


iPad Example

projects: [

  {

    name: ‘iPad Pro 11’,

    use: {

      …devices[‘iPad Pro 11’]

    }

  }

]


Custom Device

use: {

  viewport: {

    width: 390,

    height: 844

  },

  isMobile: true,

  hasTouch: true

}

Custom profiles are useful for testing devices that are not included in Playwright’s predefined list.


Testing Responsive Layouts and Viewports

A responsive application should adapt to different screen sizes.

Example:

import { test, expect } from ‘@playwright/test’;

test(‘Verify mobile navigation’, async ({ page }) => {

  await page.goto(‘https://example.com’);

  await page.getByRole(‘button’, { name: ‘Menu’ }).click();

  await expect(

    page.getByRole(‘navigation’)

  ).toBeVisible();

});

What This Test Does

  1. Opens the application.
  2. Clicks the mobile menu icon.
  3. Confirms that the navigation menu becomes visible.

This is a common responsive testing scenario.


Handling Touch Events, Geolocation, Permissions, and Orientation

Touch Support

When using a mobile device profile, touch support is automatically enabled.


Geolocation

use: {

  geolocation: {

    latitude: 40.7128,

    longitude: -74.0060

  },

  permissions: [‘geolocation’]

}

This allows you to test location-based features.


Permissions

Grant browser permissions:

permissions: [

‘notifications’,

‘geolocation’

]


Landscape Orientation

use: {

  viewport: {

    width: 844,

    height: 390

  }

}

Switching viewport dimensions helps validate landscape layouts.


Running Tests Across Multiple Mobile Devices

You can execute the same tests on several devices.

projects: [

{

name:’iPhone 14′,

use:{…devices[‘iPhone 14’]}

},

{

name:’Pixel 7′,

use:{…devices[‘Pixel 7’]}

},

{

name:’iPad Mini’,

use:{…devices[‘iPad Mini’]}

}

]

Execution Flow

Playwright Test

        │

 ┌──────┼──────────┐

 ▼      ▼          ▼

iPhone Pixel     iPad

        │

        ▼

Combined Report

This helps identify device-specific UI issues while keeping a single test suite.


Debugging with Playwright Inspector, Trace Viewer, Screenshots, and Videos

Enable debugging features:

use: {

  screenshot:’only-on-failure’,

  video:’retain-on-failure’,

  trace:’on-first-retry’

}

Useful tools include:

  • Playwright Inspector
  • Trace Viewer
  • HTML Reports
  • Screenshots
  • Videos

These artifacts simplify troubleshooting when responsive layouts fail.


Integrating Mobile Tests into CI/CD Pipelines

Example GitHub Actions workflow:

name: Mobile Tests

on:

  push:

jobs:

  test:

    runs-on: ubuntu-latest

    steps:

    – uses: actions/checkout@v4

    – uses: actions/setup-node@v4

      with:

        node-version: 20

    – run: npm ci

    – run: npx playwright install –with-deps

    – run: npx playwright test

This pipeline installs dependencies, downloads browsers, and executes your mobile tests automatically.

The same principles apply when using Jenkins or Azure DevOps.


Enterprise Best Practices for Mobile Automation

  • Use built-in Playwright device descriptors whenever possible.
  • Keep responsive tests independent and repeatable.
  • Use semantic locators such as getByRole().
  • Test both portrait and landscape orientations.
  • Validate mobile-specific navigation and gestures.
  • Capture screenshots and traces for failed tests.
  • Run mobile tests in CI/CD pipelines.
  • Separate smoke and regression suites.
  • Reuse Page Object Models across desktop and mobile tests.
  • Test multiple viewport sizes during regression.

Common Mobile Emulation Issues and Troubleshooting

ProblemSolution
Mobile menu not visibleVerify viewport size and responsive breakpoints
Wrong device profileConfirm the correct device descriptor is used
Touch events not workingEnsure hasTouch or a mobile device profile is enabled
Geolocation not availableGrant geolocation permission
Layout differs from a real deviceValidate with physical devices for hardware-specific behavior
Screenshots missingEnable screenshots in the Playwright configuration
Responsive CSS issuesInspect media queries and viewport settings

Real-Time Responsive Testing Project Example

E-Commerce Website

Test scenario:

  1. Open the home page on an iPhone profile.
  2. Open the hamburger menu.
  3. Search for a product.
  4. Open the product details page.
  5. Add the item to the cart.
  6. Verify the cart badge updates.

Additional mobile scenarios include:

  • Registration form validation
  • Mobile checkout flow
  • Sticky header behavior
  • Responsive product filters
  • Mobile search suggestions

Playwright Mobile Emulation Interview Questions

  1. What is mobile emulation in Playwright?
  2. What is the devices object?
  3. How do you emulate an iPhone?
  4. How do you emulate an Android device?
  5. What is a custom viewport?
  6. What is responsive testing?
  7. How do you test landscape mode?
  8. How do you test portrait mode?
  9. What is hasTouch?
  10. How do you configure geolocation?
  11. How do you grant browser permissions?
  12. What are Playwright projects?
  13. How do you run tests across multiple devices?
  14. What is Trace Viewer?
  15. How do you capture screenshots?
  16. How do you record videos?
  17. How do you debug responsive issues?
  18. How do you integrate mobile tests with CI/CD?
  19. What are common mobile automation challenges?
  20. Why use semantic locators?
  21. What is the difference between emulation and real-device testing?
  22. How do you organize mobile Page Objects?
  23. How do you reduce flaky mobile tests?
  24. What enterprise practices improve mobile automation?
  25. How would you test a responsive navigation menu?

FAQs

What is the Playwright Mobile Emulation Tutorial?

It is a step-by-step guide to testing responsive web applications using Playwright’s built-in mobile device emulation features.

Does Playwright support Android and iPhone emulation?

Yes. Playwright provides built-in device descriptors for many Android phones, iPhones, and iPads.

Can I create custom devices?

Yes. You can define custom viewport dimensions, touch support, and other settings.

Does mobile emulation replace real-device testing?

No. Mobile emulation is excellent for functional and responsive testing, but physical device testing is still recommended for validating hardware-specific behavior, browser differences, and performance.

Can I run mobile tests in CI/CD?

Yes. Mobile emulation works well with GitHub Actions, Jenkins, Azure DevOps, and other CI platforms.

Can I use the Page Object Model for mobile testing?

Yes. The same Page Object Model can often be reused for both desktop and mobile automation, with adjustments for mobile-specific UI when necessary.

Can Playwright test orientation changes?

Yes. You can emulate portrait and landscape layouts by using appropriate device profiles or custom viewport settings.

Is mobile emulation suitable for enterprise automation?

Yes. It provides a scalable way to include responsive testing in automated regression suites and CI/CD pipelines.

Leave a Comment

Your email address will not be published. Required fields are marked *