Skip to main content

Browser Driver Window

Browser Driver Window

Screen resolution can impact how your web application renders, so WebDriver provides mechanisms for moving and resizing the browser window.

Current window object can be retrieved from driver using the method:

Window window = driver.getActiveWindow();

Get window size

Fetches the size of the browser window in pixels.

//Access each dimension individually
int width = driver.getActiveWindow().getSize().getWidth();
int height = driver.getActiveWindow().getSize().getHeight();

//Or store the dimensions and query them later
Dimension size = driver.getActiveWindow().getSize();
int width1 = size.getWidth();
int height1 = size.getHeight();

Set window size

Restores the window and sets the window size.

driver.getActiveWindow().setSize(new Dimension(1024, 768));

Get window position

Fetches the coordinates of the top left coordinate of the browser window.

// Access each dimension individually
int x = driver.getActiveWindow().getPosition().getX();
int y = driver.getActiveWindow().getPosition().getY();

// Or store the dimensions and query them later
Point position = driver.getActiveWindow().getPosition();
int x1 = position.getX();
int y1 = position.getY();

Set window position

Moves the window to the chosen position.

// Move the window to the top left of the primary monitor
driver.getActiveWindow().setPosition(new Point(0, 0));

Maximize window

Enlarges the window. For most operating systems, the window will fill the screen, without blocking the operating system’s own menus and toolbars.

driver.getActiveWindow().maximize();

Minimize window

Minimizes the window of current browsing context. The exact behavior of this command is specific to individual window managers.

Minimize Window typically hides the window in the system tray.

driver.getActiveWindow().minimize();

Fullscreen window

Fills the entire screen, similar to pressing F11 in most browsers.

driver.getActiveWindow().fullscreen();