User Agents

Web browsers and everyday web tech, explained

Increase Screen Size in Playwright Viewport & Browser

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Adjusting the screen size in Playwright allows you to simulate different device resolutions and browser window dimensions during automated testing. This is essential for ensuring your web application renders correctly across desktops, tablets, and mobile devices. Playwright provides built-in support for configuring viewport sizes, either globally, per test, or through predefined device descriptors. By modifying the viewport or browser window size, you can test responsive layouts, catch layout bugs, and validate UI behavior under various screen conditions. Before adjusting screen size, ensure Playwright is properly installed and you're using a supported browser like Chromium, Firefox, or WebKit.

What You'll Need

  • Playwright installed via npm or another package manager
  • A configured test environment (Node.js 14 or higher)
  • Basic familiarity with JavaScript or TypeScript
  • An existing Playwright test file or project structure

Step-by-Step

  1. Open your Playwright configuration file, typically named playwright.config.ts or playwright.config.js, located in the root of your project.
  2. To set a global default viewport, locate or create the use object inside the config. Add the viewport property with width and height values. For example:
    viewport: { width: 1920, height: 1080 }
    This sets all tests to run in a full HD resolution unless overridden.
  3. If you want no viewport restrictions (browser uses default size), set viewport: null. This allows the browser window to expand naturally based on content and system settings.
  4. To simulate a mobile device, use Playwright’s built-in device descriptors. Import devices from @playwright/test and apply a preset like devices['iPhone 13']. Example:
    use: {
      ...devices['iPhone 13'],
    }
    This automatically applies viewport, user agent, and touch support.
  5. For per-test customization, override the viewport directly in a test file using test.use(). For instance:
    test.use({ viewport: { width: 1280, height: 800 } });
    
    test('loads dashboard correctly', async ({ page }) => {
      await page.goto('/dashboard');
      // test logic
    });
    This changes the viewport only for that specific test.
  6. To manually resize the browser window during a test (not just viewport), use the page.setViewportSize() method inside a test. Example:
    await page.setViewportSize({ width: 1440, height: 900 });
    This is useful when you need dynamic resizing within a single test scenario.
  7. For headless mode, viewport settings still apply. However, if you want to see the actual window size during debugging, run tests in headed mode by setting headless: false in your config or launch options.
  8. Launch a browser instance with custom window size using browser.newContext() and pass the viewport option. Example:
    const context = await browser.newContext({
      viewport: { width: 1024, height: 768 }
    });
    const page = await context.newPage();
    This is ideal for isolated test contexts with unique screen dimensions.
  9. Validate the change by adding a debug log or screenshot. Use await page.screenshot({ path: 'viewport-test.png' }) to capture the page and confirm the layout matches expectations.
  10. Save your configuration and run your tests using npx playwright test to verify the screen size changes take effect.

Common Mistakes

  • Forgetting to import devices when using device presets, leading to undefined references.
  • Mixing viewport and screen options incorrectly — screen defines the entire screen size (used in mobile emulation), while viewport controls the browser content area.
  • Setting a viewport size larger than the actual display, which may cause rendering issues or clipped screenshots in headed mode.
  • Using page.setViewportSize() before navigating to a page, which can throw errors. Always call it after newPage() and before navigation if needed.
  • Overriding viewport globally when only one test needs a custom size, leading to unintended side effects in other tests.

Tips

  • Use viewport: null during development to maximize the browser window and make debugging easier.
  • Combine viewport changes with device scaling for realistic mobile testing: some devices use deviceScaleFactor > 1, which affects how pixels render.
  • Create named configurations in playwright.config.ts for different screen profiles (e.g., desktop, tablet, mobile) using projects.
  • Use environment variables to switch viewport sizes between CI and local runs.
  • Take screenshots at different breakpoints to visually confirm responsive behavior.

Troubleshooting

If the screen size does not change as expected, first verify that the configuration is correctly formatted and the properties are spelled exactly as required (e.g., viewport, not viewPort). Check that you are not accidentally overriding settings in multiple places — global config, project config, and test-level overrides can conflict. If using device presets, ensure they are spread correctly with ... syntax. If screenshots appear cropped, confirm the viewport size matches the expected dimensions. In headed mode, if the window appears small despite large viewport settings, it may be constrained by system display limits. Try reducing the size or running on a machine with higher resolution. If setViewportSize() fails, ensure the page is already created and loaded before calling the method. Finally, clear any cached test results or restart the test runner if changes aren’t being picked up.

Frequently Asked Questions

Can I change the viewport size after the page loads?

Yes, you can call page.setViewportSize() at any time after the page is created, even mid-test, to simulate resizing.

What’s the difference between viewport and screen size in Playwright?

Viewport refers to the visible area of the web page, while screen size (used in device emulation) includes the full simulated screen, affecting things like window.screen properties.

Does viewport size affect mobile emulation?

Yes, viewport is a core part of mobile device emulation. Using presets like iPhone or iPad automatically sets appropriate viewport, user agent, and touch support.

Why is my screenshot smaller than the viewport I set?

This can happen if the page content is smaller or if there are rendering delays. Ensure the page is fully loaded and consider using fullPage: true in screenshot() to capture beyond the viewport.

Can I test multiple screen sizes in one test file?

Yes, use test.describe.configure() or separate test blocks with test.use() to run the same test logic under different viewport settings.

Is viewport size respected in headless mode?

Yes, viewport settings are fully applied in headless mode and determine how the page is rendered and captured in screenshots.