Increase Screen Size in Playwright Viewport & Browser
In this article
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
- Open your Playwright configuration file, typically named
playwright.config.tsorplaywright.config.js, located in the root of your project. - To set a global default viewport, locate or create the
useobject inside the config. Add theviewportproperty withwidthandheightvalues. For example:
This sets all tests to run in a full HD resolution unless overridden.viewport: { width: 1920, height: 1080 } - 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. - To simulate a mobile device, use Playwright’s built-in device descriptors. Import
devicesfrom@playwright/testand apply a preset likedevices['iPhone 13']. Example:
This automatically applies viewport, user agent, and touch support.use: { ...devices['iPhone 13'], } - For per-test customization, override the viewport directly in a test file using
test.use(). For instance:
This changes the viewport only for that specific test.test.use({ viewport: { width: 1280, height: 800 } }); test('loads dashboard correctly', async ({ page }) => { await page.goto('/dashboard'); // test logic }); - To manually resize the browser window during a test (not just viewport), use the
page.setViewportSize()method inside a test. Example:
This is useful when you need dynamic resizing within a single test scenario.await page.setViewportSize({ width: 1440, height: 900 }); - 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: falsein your config or launch options. - Launch a browser instance with custom window size using
browser.newContext()and pass theviewportoption. Example:
This is ideal for isolated test contexts with unique screen dimensions.const context = await browser.newContext({ viewport: { width: 1024, height: 768 } }); const page = await context.newPage(); - 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. - Save your configuration and run your tests using
npx playwright testto verify the screen size changes take effect.
Common Mistakes
- Forgetting to import
deviceswhen using device presets, leading to undefined references. - Mixing
viewportandscreenoptions incorrectly —screendefines the entire screen size (used in mobile emulation), whileviewportcontrols 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 afternewPage()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: nullduring 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.tsfor 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 callpage.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 likewindow.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 usingfullPage: true in screenshot() to capture beyond the viewport.
Can I test multiple screen sizes in one test file?
Yes, usetest.describe.configure() or separate test blocks with test.use() to run the same test logic under different viewport settings.