Maximize Browser in Playwright Java

Profile picture for user arilio666
Submitted by arilio666 on

This article will show how we can maximize the browser using playwright java. We will see how to set width and height using viewport options, and also we will see how this can be done dynamically.


  • While creating a new context for the browser, we can set the viewport and width and height according to our machine.
        BrowserContext newContext = browser.newContext(new Browser.NewContextOptions().setViewportSize(1800, 880));
  • Here, we have created a new option inside the context and set the viewport.
  • Different calculation methods may result in differing values for width or height. 
  • It is recommended to use the screen size reported by the whatismyviewport website or calculate the values after maximizing the browser window.

Dynamically Set Viewport:

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
  • Using the package from Java.awt toolkit, we can get the screen size of our current working machine.
  • With the variable, we can get the width and height.
  • Now let us see how we can pass these variables.
browser.newContext(new Browser.NewContextOptions().setViewportSize(1800, 880));
  • The setViewportSize now accepts integers and won't accept double.
  • Let us convert the double to int.
    int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();
                BrowserContext newContext = browser.newContext(new Browser.NewContextOptions().setViewportSize(width, height));
  • Now after converting double to int, we can pass in the width and height variable, which will set the resolution of our current working pc.


  • This is how we can maximize browsers in playwright java.