Skip to content
CodeKruPro

Window Size and Position

50/70

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

About this lesson

Set the browser window size in Selenium Java with setSize() and Dimension

Learn Selenium JavaAdvancedWindow Management

How it works

driver.manage().window().setSize(new Dimension(1280, 800)) sets the outer size of the browser window in pixels, and getSize() reads it back as a Dimension with getWidth() and getHeight(). setPosition() and getPosition() do the same for the window's position, using a Point.

The outer size includes the browser's own toolbars, so the page gets less room. At 1024 by 768 in this runner, the page's own viewport is 1024 by 625. When a layout breakpoint matters, check window.innerWidth through JavaScript rather than the outer size.

Size changes what the page shows. The site's runner starts headless Chrome at 780 by 580, and at that width the testkru sidebar is pushed off-screen, which is why links in it are not displayed in earlier lessons. maximize() in headless mode cannot use a real screen; here it produces 800 by 600, so set an explicit size instead.

About the assignment

You resize the browser on the Text Fields page to a common laptop size and read the size back, then compare it with what you set.

What you will do

  1. Create a Dimension with the width and height from the task and pass it to setSize().
  2. Read the size back with getSize().
  3. Print the width and the height on separate lines with the labels from the expected output.

Common mistakes

  • Mixing up the Dimension import

    java.awt.Dimension has the same name. Selenium needs org.openqa.selenium.Dimension; with the wrong import, setSize() does not compile.

  • Using maximize() in headless runs

    It does not mean "screen size" without a screen. Set an explicit size so every run uses the same layout.

  • Reading the size from the page

    window.innerHeight reports the viewport, 625 here rather than the window height, because the window size includes the browser's toolbars. Compare like with like.

In a real test suite

Suites set one window size in setup so screenshots and layouts are identical on every machine, and responsive tests run the same steps at phone, tablet and desktop widths. ChromeOptions can also set the size at start with the --window-size argument.

Lesson 49 managed tabs. Lesson 51 starts the Actions class, beginning with a mouse hover.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        // TODO: Set window size to 1024x768
        // TODO: Get and print the window dimensions
        
        driver.quit();
    }
}