Skip to content
CodeKruPro

Set the browser window size and read it back

Automation taskEasy

Asked as: "A menu is missing in CI but visible on your laptop. How do you make the window size the same everywhere?" Setting the size and reading it back is the first step. Responsive pages change their layout with the window width, so a fixed size gives every run the same layout.

Window Size and Position

Window Size and Position

Selenium can control the browser window size and position:

  • manage().window().maximize()
  • manage().window().setSize(new Dimension(w, h))
  • manage().window().getSize()

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Set the window size to 1024x768
  3. Get and print the width and height

Expected output:

Width: 1024
Height: 768