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 .

Introduction

Maximize the browser is very important part of selenium framework, it is good practice to maximize the browser while automating the any web application. When the user executes the selenium framework or any script, browser may not be in the full screen state and need to maximize the browser to view all the elements of the web application .

As webdriver having default setting and it starts with that setting.

And it is good to maximize the browser at the start of the script, so maximize is required so that the script gets executed successfully without any error. Elements on the web application may not be recognized by the selenium if the browser is not maximized and thereby making framework fail.

There are different ways or methods to maximize or minimize the browser. We can also use the customize 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 , we need to use resize method to minimize the browser.

There are a few methods which can be used to resize the current browser window in selenium :

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

Void maximize() – This method is used to maximize 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.

Resize a browser using selenium web driver.

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.

Maximize a browser window using web driver.

a) Selenium script with explanation.

Script Description : In the below Selenium script shown the maximize 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 maximize 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.

Minimize a browser window using web driver.

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.firefox.marionette","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 maximize of the browser in selenium.
    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);
    

 

YOU MIGHT LIKE: