Resize browser window

0 votes

As pointed out in previous post (Maximize browser window), some websites implemented responsive design. You might want to execute some tests in smaller browser windows. Selenium WebDriver gives the ability to resize the browser window.

  @Test
  public void resizeWindow() {
    System.setProperty("webdriver.gecko.driver",
        FileUtil.findFileOnPath("mac/geckodriver"));
    final FirefoxDriver driver = new FirefoxDriver();

    driver.manage().window().setSize(new Dimension(400, 750));

    driver.get("http://www.selenium-in-action.io");
  }

See here a list of viewport sizes: http://viewportsizes.com/

Maximize browser window

0 votes

Some websites implemented responsive design. So, the website looks differently on smaller screens.
Therefore it’s important to maximize the browser window before the test starts.

@Test
public void maximizeFirefox() {
  // Start browser
  System.setProperty("webdriver.gecko.driver", FileUtil.findFileOnPath("mac/geckodriver"));
  final FirefoxDriver driver = new FirefoxDriver();

  // Maximize browser
  driver.manage().window().maximize();

  // Open website
  driver.get("http://www.selenium-in-action.io");
}

There are several bug reports noticing that this feature doesn’t work when using ChromeDriver.
Luckily the is a workaround, we can start Chrome in kiosk mode (full screen without toolbars).

@Test
public void startChromeFullscreen() {
  // Start browser with specific options
  final ChromeOptions options = new ChromeOptions();
  options.addArguments("start-fullscreen");
  System.setProperty("webdriver.chrome.driver", FileUtil.findFileOnPath("mac/chromedriver"));
  final ChromeDriver driver = new ChromeDriver(options);

  // Open website 
  driver.get("http://www.selenium-in-action.io");
}

Kiosk mode

@Test
public void startChromeKioskMode() {
  // Start browser with specific options  
  final ChromeOptions options = new ChromeOptions();
  options.addArguments("--kiosk");
  System.setProperty("webdriver.chrome.driver", FileUtil.findFileOnPath("mac/chromedriver"));
  final ChromeDriver driver = new ChromeDriver(options);

  // Open website 
  driver.get("http://www.selenium-in-action.io");
}