Maximize and Minimized the browser

In this section, we will discuss how to maximize and minimize the browser in selenium.

When you call driver.get(“URL”), this will open the default browser in minimized format. If you want to maximize the browser then you need to use the selenium WebDriver method named “maximize()”.

There are different ways to maximize the browsers. Also if we want we can resize the browser based on the requirement. Unlike maximize, Selenium WebDriver does not have any in-build method to minimize the browser. If we want to minimize the browser then we need to resize the browser.

There are 4 ways to perform the browser resize.

  • Void setSize() : This method sets the size of the browsers.
  • Void maximize() : This method maximizes the browsers.
  • Dimension getSize(): This method sets the size of the browsers in height and width. It returns the dimension of the browser.
  • Point setPosition(): This method sets the position of the current browser.

Syntax for Maximize the browser: driver.manage().window().maximize();

This is the best practice to automate the application after maximizing the browser. This is because when automation script will be executed this will be started with minimized window and some portion of the web page is not visible.

One question you can think that, why selenium WebDriver is opening browser in minimized format.

Because selenium will open the browser in default format and default format of the browser opening is in minimized way,

Another thing you need to remember that selenium WebDriver is not providing any method to minimize the browser. But you can resize the browser. Below is the syntax for resizing the browser.

driver.manage().window().setSize(size object);
Maximize and Minimize the browser

Example with Scenario:

Let’s understand the maximizing the browsers in a simple scenario. To maximize the browser we can follow below steps.

  • Open the chrome browser

 driver= new ChromeDriver();

  • Launch the site

driver.get(“https://www.guru99.com/”);

  • Resize the browser

driver.manage().window().setSize(d);

  • Wait for a few seconds as to view the resize action .

 Thread.sleep(10000);

  • Close the browser.

driver.quit();

Let’s understand the maximize , resize , minimize the browser with scenario by following above 5 steps.

Maximize the Browser:

In this section we will discuss how to maximize the browser.

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

public class MaximizeBrowser {

	public static void main(String args[]) throws InterruptedException
	{
		WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromedriver.exe");
        driver= new ChromeDriver(); //Open the chrome browser 
       // Launch the site 
     	driver.get("https://www.testinglpoint.com/");
	   //Rsize the browser
     	driver.manage().window().maximize();
      	//Wait for a few seconds as to view the resize action . 
     	Thread.sleep(10000);
     	//Close the browser
     	driver.quit();
	}	
}

Minimize the Browser:

In this section we will discuss how to minimize the browser.

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

public class MinimizeBrowser {

	public static void main(String args[]) throws InterruptedException
	{
		WebDriver driver;
		System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromedriver.exe");
		driver= new ChromeDriver(); //Open the chrome browser 
 		// Launch the site 
     	driver.get("https://www.testinglpoint.com/");
     	//Minimize the browser
     	Point p = new Point(0,3000);
driver.manage().window().setPosition(p);
     	//Wait for a few seconds as to view the resize action . 
     	Thread.sleep(10000);
      	//Close the browser
     	driver.quit();
	}	
}

Resize the Browser:

In this section we will discuss how to minimize the browser.

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

public class ResizeBrowser {

	public static void main(String args[]) throws InterruptedException
	{
      WebDriver driver;	
      System.setProperty("webdriver.chrome.driver", 	"E://Selenium//Selenium_Jars//chromedriver.exe");
	  driver= new ChromeDriver(); //Open the chrome browser 
 		 
       // Launch the site 
     	driver.get("https://www.testinglpoint.com/");
     	 
     	 //Minimize the browser
     	Dimension d = new dimension(300,1080);
driver.manage().window().setSize(d);     	       
     	//Wait for a few seconds as to view the resize action . 
     	Thread.sleep(10000);
     	       
     	//Close the browser
     	driver.quit();
	}	
}

Leave a Reply

Your email address will not be published. Required fields are marked *