How to Maximize Browser Window in Selenium

Maximize Browser in Selenium

In this tutorial, you will learn how to maximize, minimize or resize the browser using selenium Webdriver. Explained through different scenarios using maximize() method and dimensions for resizing the browser.

Why Maximize a Browser in Selenium Automation?

Elements on the web application may not be recognized by the selenium if the browser is not maximized and thereby making framework fail. Hence, Maximize the browser is very important part of selenium framework. It is good practice to maximize the browser while automating any web application. When the user executes the selenium framework or any script, browser may not be in the full screen state and you need to maximize the browser using window maximize in Selenium to view all the elements of the web application. It is good to maximize the browser at the start of the script, so that the script gets executed successfully without any error.

Steps to Maximize Window in Selenium

Here is how to maximize browser in Selenium:

To maximize browser in Selenium, you need to call the maximize() Selenium command to maximize window interface of the driver class.

void maximize() – This method is used to maximize the current browser.

Maximize Window In Selenium
Maximize the Browser in Selenium

You can customize the size of the browser according to the requirement of the scenario. Selenium webdriver does not provide any method for minimizing the browser, there is no such direct method. You need to use resize method to minimize the browser.

void setSize() – This method is used to set the size of the current browser.

Dimension getSize() – This method is used to get the size of the browser in height and width. It returns the dimension of the browser.

Point setPosition() – This method is used to set the position of the current browser.

How to Maximize a Browser window using Webdriver

a) Selenium script with explanation.

Script Description : In the below maximize in Selenium script shown, the maximization of the browser using testNG framework, steps of the scenario for maximize window Selenium are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the browser maximize in Selenium action .
  4. Close the browser.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Maximize {

	public static void main(String args[]) throws InterruptedException
	{
		WebDriver driver;
		
		System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
		 driver= new ChromeDriver();
 		 
         // Launch the application
     	 driver.get("https://www.guru99.com/");
     	 
     	     	//Resize current window to the set dimension
     	        driver.manage().window().maximize();
     	       
     	       //To Delay execution for 10 sec. as to view the maximize browser
     	        Thread.sleep(10000);
     	       
     	        //Close the browser
     	        driver.quit();
	}	
}

b) Output Analysis

Opened the chrome browser , maximized the browser, wait for a few seconds and closed the browser.

How to Resize a Browser using selenium Webdriver

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the resize of the browser using testNG framework , steps of the scenario are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the resize action .
  4. Close the browser.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Resize {

	public static void main(String args[]) throws InterruptedException
	{
		WebDriver driver;
	
		System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
		 driver= new ChromeDriver();
 		 
         // Launch the application
     	 driver.get("https://www.guru99.com/");
     	 
     	Dimension d = new Dimension(300,1080);
     	//Resize current window to the set dimension
     	   driver.manage().window().setSize(d);
     	        
     	 //To Delay execution for 10 sec. as to view the resize browser
     	 Thread.sleep(10000);
     	       
     	 //Close the browser
     	 driver.quit();
	}	
}

b) Output Analysis

Opened the chrome browser , resized the browser, wait for a few seconds and closed the browser.

How to Minimize a Browser window using Webdriver.

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the minimize of the browser using testNG framework , steps of the scenario are :

  1. Open the chrome browser .
  2. Launch the site .
  3. Wait for a few seconds as to view the minimize action .
  4. Close the browser.
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Minimize {

	public static void main(String args[]) throws InterruptedException
	{
		WebDriver driver;
	
		System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
		 driver= new ChromeDriver();
 		 
         // Launch the application
     	 driver.get("https://www.guru99.com/");
     	 
     	Point p = new Point(0,3000);
     	
     	//Minimize the current window to the set position
     	        driver.manage().window().setPosition(p);
     	        
     	       //To Delay execution for 10 sec. as to view the minimize browser
     	        //you can view in the taskbar below of the screen.
     	        Thread.sleep(10000);
     	        
     	        //Close the browser
     	        driver.quit();
	}	
}

Note: If the user wants to use Firefox browser, then user needs to set property of FirefoxDriver and create FirefoxDriver object instead of ChromeDriver in all above 3 scenarios scripts as given below:

System.setProperty("webdriver.gecko.driver","E://Selenium//Selenium_Jars//geckodriver.exe ");
 driver= new FirefoxDriver();

b) Output Analysis

Opened the chrome browser , minimized the browser, wait for a few seconds and closed the browser.

TroubleShooting

  • Use the latest versions of Selenium Jars, chromedriver, marionette driver and IEdriver etc.
  • Check the compatibility of the selenium jars and browser used.

Summary

  • In the above tutorial, we illustrate the resizing of the browser through different scenarios like to maximize, minimize and resize as required in the project framework for different functionality.
  • In the first scenario, we have shown the resize of the browser in selenium.
    Dimension d = new Dimension(300,1080);
    driver.manage().window().setSize(d);
    
  • In the second scenario, we have shown the Selenium maximize window of the browser.
    driver.manage().window().maximize();
    
  • In the third scenario, we have shown the minimize of the browser in selenium.
    Point p = new Point(0,3000);     	
    driver.manage().window().setPosition(p);