Search code examples
playwrightplaywright-dotnet

When can Playwright's Page.ViewportSize be null?


The Page.ViewportSize property is defined as nullable.

Under what conditions could it be null?


Solution

  • Viewport: null is mainly for headful / interactive mode . It simply means that the user would set the viewport size via resizing the window.

    NOTE: The null value opts out from the default presets, makes viewport depend on the host window size defined by the operating system. It makes the execution of the tests non-deterministic.

    Code Example:

    const playwright = require('playwright');
    
        (async () => {
          for (const browserType of ['chromium', 'firefox', 'webkit']) {
            const browser = await playwright[browserType].launch();
            const context = await browser.newContext({
              viewport: null
            });
            const page = await context.newPage();
            await page.goto('https://google.com');
            
           
            await page.screenshot({ path: `example-${browserType}.png` });
            await browser.close();
          }
        })();
    

    Reference: https://github.com/microsoft/playwright/issues/2170