Set browser's height and width using Selenium


At times during automation we may be required to test an application with some specific browser window dimensions. In this post we will be studying how to resize a browser to an exact dimension.
In order to achieve this, we will use the setSize() method which takes the Dimension object as input. The Dimension object has width and height integer fields. Hence, we can set the width and height of the browser by calling the setSize method with the required dimensions of the browser window.

Code snippet to resize a browser

During automation its one of the best practices to maximize the browser in the initial phases (or in the BeforeTest method in case you are using TestNG as the testing framework). Selenium provides direct command to maximize the browser window. The following command is use to maximize the browser window.

int width = 600;
int height = 400;
Dimension dimension = new Dimension(width, height);
driver.manage().window().setSize(dimension);