How to Change the Size of Browser Window while Working with WebDriver?There are different ways to re-size browser window using Java. When you start the browser in WebDriver, it takes the default settings. However, at times it is required to change the size of the window. This is especially while testing responsive web design. This is to check the response of each element with varying browser window size.

  1. Java using Dimension

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.Dimension;

 public class BrowserOperations {

         WebDriver driver;

                public void launchBrowser() {

          driver = new FirefoxDriver();

     }

           public void resizeBrowser() {

          Dimension d = new Dimension(800,480);

          driver.manage().window().setSize(d);

     }

}

  1. Java using Chrome options

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.DesiredCapabilities;

 public class BrowserOperations {

     public static void main(String[] args) {

          System.setProperty(“webdriver.chrome.driver”,

               “/path/to/chromedriver”);

 ChromeOptions options = new ChromeOptions();

options.addArguments(“window-size=800,480”);

          DesiredCapabilities cap = DesiredCapabilities.chrome();

          cap.setCapability(ChromeOptions.CAPABILITY, options);

          WebDriver driver = new ChromeDriver(capabilities);

              driver.get(“http://xyz.com/”);

     }

}

Note: To set the browser window to maximum size, you need to just call the maximize() method, as written below.

Webdriver driver = new FirefoxDriver();

driver.manage().window().maximize();