DEV Community

Alperen Coşkun
Alperen Coşkun

Posted on

How to full screen a browser in Playwright?

One of the things that newcomers to Playwright may find strange at first is that even when the browser is full screen, the content displayed remains in a small area.

Image description

The main reason for this is that in the default device parameters, separate values are defined for the viewport. The viewport is the area in the browser where the content is displayed, for example if you go to whatismyviewport you will see a value smaller than your screen resolution.

Image description

If you look at the viewport resolution in the default device parameters in Playwright, you will see that it remains quite small and since these values remain constant, the area where the content will be displayed remains the same even if the browser is made full screen.



  "Desktop Chrome": {
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.4 Safari/537.36",
    "screen": {
      "width": 1920,
      "height": 1080
    },
    "viewport": {
      "width": 1280,
      "height": 720
    },
    "deviceScaleFactor": 1,
    "isMobile": false,
    "hasTouch": false,
    "defaultBrowserType": "chromium"
  },


Enter fullscreen mode Exit fullscreen mode

Source: deviceDescriptorsSource.json

I did some research to solve this situation and the solutions I found were either directly wrong or not enough. As a result of my own experiments, I obtained the solution I found the most suitable and it is as follows:



    {
      name: 'chromium',
      use: { 
        ...devices['Desktop Chrome'],
        deviceScaleFactor: undefined,
        viewport: null,
        launchOptions: {
          args: ['--start-maximized']
        },
      },
    },


Enter fullscreen mode Exit fullscreen mode

In the example above, I have added additional parameters for Chromium in the playwright.config.ts file. The argument in launchOptions already makes the Chromium (and of course Chrome) browser window fullscreen, but as I mentioned at the beginning of this article, the viewport resolution remains constant, so the area where the content is displayed is still small.

In order for the viewport resolution to change dynamically, the viewport parameter must be set to null, but if you do this alone, it is not enough because you get the following error when you try to run the test:



Error: "deviceScaleFactor" option is not supported with null "viewport"


Enter fullscreen mode Exit fullscreen mode

And that's why I also defined the deviceScaleFactor parameter as undefined. When you run the test in its final form, the browser now comes up full screen as it should in reality.

Image description

If you found my article useful, you can like it and share it for the benefit of people around you, thank you.

Top comments (2)

Collapse
 
marek_m_ae0d4991d63809f4 profile image
Marek M.

Thanks a lot! This helped me with Ag-grid tests under Playwright. Default browser settings cause tests to fail due to not loading the entire Ag-grid table.

Collapse
 
gisioraelvis profile image
ELVIS GISIORA

First thanks for sharing this hack
I used it and it worked when running tests in headed mode, but tests that were initially passing started to fail in headless and ci mode. An example of that is:
Error: locator.click: Test timeout of 60000ms exceeded.
Call log:

  • waiting for getByTitle('Add Person')
  • locator resolved to
  • attempting click action
  • waiting for element to be visible, enabled and stable
  • element is not visible
  • retrying click action, attempt #1
  • waiting for element to be visible, enabled and stable
  • element is not visible
  • retrying click action, attempt #2
  • waiting 20ms
  • waiting for element to be visible, enabled and stable
  • element is not visible
  • retrying click action, attempt #3
  • waiting 100ms
  • waiting for element to be visible, enabled and stable
  • element is not visible

I tried all manner of workarounds, including forced clicks and more specific selectors, to no success. Reverting to the default configs resolved the "element is not visible" issue and the tests started to pass again. To anyone intending to use the hack be aware of the issue