When we launch a browser using Selenium WebDriver, by default it is not in its maximized state. In this post, we will see how to maximize and minimize a browser during automation.
Maximize the window
It is recommended to maximize the browser before performing any operation on the browser to make sure that the tests run in a consistent environment, with a known window size. This helps in avoiding issues that might arise due to varying browser window sizes.
When the browser window isn’t maximized, certain web elements might be hidden or located outside the visible area. Maximizing the window makes sure these elements become visible and accessible for interaction during automated testing.
driver.manage().window().maximize();
Below is an example to maximize the window.
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class Maximize_Demo {
public static void main(String args[]) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(4));
driver.get("https://www.bing.com/");
System.out.println("Page Title :" + driver.getTitle());
driver.quit();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2024/04/image-72.png?w=804)
Minimize the window
Minimizing the window enables us to carry out background tasks or operations on the machine without disrupting the test execution. This can be especially useful when we need to work on other tasks while the tests are in progress.
Minimizing the window can sometimes reveal behaviors or functionalities that are triggered when the browser window is not in focus or is minimized. This can be valuable for testing scenarios where the application’s behavior under such conditions needs to be verified.
Selenium 4 and above support the below code.
driver.manage().window().minimize();
Below is an example to minimize the window.
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class Minimize_Demo {
public static void main(String args[]) {
WebDriver driver = new ChromeDriver();
driver.manage().window().minimize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(4));
driver.get("https://www.bing.com/");
System.out.println("Page Title :" + driver.getTitle());
driver.quit();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2024/04/image-73.png?w=981)
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!