App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How to Maximize Chrome Window in Selenium Webdriver using Java

How to Maximize Chrome Window in Selenium Webdriver using Java

By Jash Unadkat, Technical Content Writer at BrowserStack -

When browser windows are maximized, it reduces the chances of Selenium scripts missing out on web elements they must interact with during automated tests. It is possible that certain elements may not be visible to or recognized by Selenium if the browser window is not in a maximized state.

Maximizing a browser window at first also provides better visibility to the QAs for the test cases being executed. Thus QAs must consider maximizing the browser window as a best practice.

As Chrome is the most widely used browser, this article will explore two simple ways to maximize a Chrome window in Selenium Webdriver using Java.

1. Use the maximize() method from WebDriver.Window Interface

The code snippet below implements four basic scenarios:

  1. Launching the Chrome browser
  2. Navigating to the desired URL
  3. Maximizing the Chrome Window
  4. Terminating the browser

Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Max {

public static void main(String args[]) throws InterruptedException
{
System.setProperty("<Path of the ChromeDriver>");
WebDriver driver = new ChromeDriver();

// Navigate to a website
driver.get("https://www.browserstack.com/");

//Mazimize current window
driver.manage().window().maximize();

//Delay execution for 5 seconds to view the maximize operation
Thread.sleep(5000);

//Close the browser
driver.quit();
} 
}

Successful execution of the selenium script above will do the following: launch the Chrome browser, navigate to the BrowserStack website, maximize the Chrome Window, and wait for five seconds.

Try Selenium Testing on Real Device Cloud for Free

2. Use the ChromeOptions class

An alternate method that can maximize the Chrome window is to use the ChromeOptions class. This method informs the Chrome browser explicitly to launch in maximized mode.

Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Max {

public static void main(String args[]) throws InterruptedException
{

System.setProperty("<Path of the ChromeDriver>");

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");

WebDriver driver = new ChromeDriver(options);

// Navigate to a website
driver.get("https://www.browserstack.com/")

//Close the browser
driver.quit();
} 
}

Successful execution of the script above will do the following: launch the Chrome browser in maximized mode and navigate to the Browserstack website.

Note: In the first method, the operation is performed after launching the Chrome browser. In the second method, the browser is, by default launched in maximized mode.


Also read: How to run Selenium tests on Chrome using ChromeDriver


Maximizing a browser window prior to the execution of test cases provides better visibility to the QA. Doing so also ensures that no elements are left unidentified when tests are being executed. Implementing either of the methods explained above will help QAs avoid test failure.

Tags
Automation Testing Cross browser testing Selenium Selenium Webdriver

Featured Articles

How to handle Alerts and Popups in Selenium?

Learn Selenium with Java to run Automated Tests

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.