Monday 29 March 2021

Published March 29, 2021 by with 0 comment

Different Approaches For Maximizing Browser Window In Selenium Webdriver

Hello learners,  

Maximizing the browser window manually is a very simple task. But when we do it through Automation then there are number of ways we can do this. Generally, when the questions like what are the different ways we can maximize a window in Selenium? or What are different approaches for maximizing window in Selenium? asked during interviews, we ended up giving answers like by using maximize() method of the driver. But apart from that below are the different approaches that we can use for maximizing browser window in Selenium WebDriver.


Approch 1. Using maximize() Method:

We have different methods under driver.manage().window(). 

So, here we will use maximize() method to maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class UsingMaximizeFunction {

	public static void main(String[] args) {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

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

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

		// Close the browser
		driver.quit();

	}

}

Approch 2. Using fullscreen() Method:

As mentioned in Approch 1, we have different methods under driver.manage().window(). 

So, here we will use fullscreen() method to maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingFullScreenFunction {

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

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

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

		// Maximize current window using "fullscreen" function
		driver.manage().window().fullscreen();

		Thread.sleep(3000);

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

}

Approch 3. Using  setSize() Method:

Dimension class is used to set height and width of the object. 

So, here we can set the height and width of the browser as per our requirement. 

To do browser window maximization one should know the height and width of the maximize window. This will differ based on the screen resolution of your device. 

To use dimension class first create the object of Dimension class. 

Then pass that dimension object to setSize() method as driver.manage().window().setSize(dimension object) method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingWindowSize {

	public static void main(String[] args) {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

		// Maximize current window by setting windows size
		Dimension dim = new Dimension(1280, 860);
		driver.manage().window().setSize(dim);

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

		// Close the browser
		driver.quit();

	}

}

Approch 4. Using ChromeOptions window-size:

ChromeOptions class is used for customizing the ChromeDriver session. 

So, when we launch any browser using Selenium then it opens up with given configurations. 

First, create the ChromeOptions class object. 

Then add arguments to that object as chromeOptions.addArguments("window-size=1280,860"). Here 1280 is the width and 860 is the height for maximizing a window. You can pass width and height as per your requirement there. 

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingChromeOptionsWindowSize {

	public static void main(String[] args) {

		// Maximize current window using ChromeOptions "window-size"
		ChromeOptions chromeOptions = new ChromeOptions();
		chromeOptions.addArguments("window-size=1280,860");

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

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

		// Close the browser
		driver.quit();

	}

}

Approch 5. Using ChromeOptions --start-fullscreen:

We can use --start-fullscreen method of ChromeOptions. 

First, create the ChromeOptions class object. 

Then add arguments to that object as chromeOptions.addArguments("--start-fullscreen"). Here "--start-fullscreen" is a keyword that you have to use as it is.

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximized browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingChromeOptionsStartFullscreen {

	public static void main(String[] args) {

		// Maximize current window using ChromeOptions "--start-fullscreen"
		ChromeOptions chromeOptions = new ChromeOptions();
		chromeOptions.addArguments("--start-fullscreen");

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

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

		// Close the browser
		driver.quit();

	}

}

Approch 6. Using ChromeOptions --start-maximized/--kiosk:

We can use --start-maximized/--kiosk method of ChromeOptions. 

First, create the ChromeOptions class object. 

Then based upon our operating system we need to add arguments to that object.

If the operating system is windows then add arguments to ChromeOption object as chromeOptions.addArguments("--start-maximized"). Here "--start-maximized" is a keyword that you have to use as it is.

If the operating system is MAC/Linux then add arguments to ChromeOption object as chromeOptions.addArguments("--kiosk"). Here "--kiosk" is a keyword that you have to use as it is.

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximized browser window.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class UsingChromeOptionsStartMaximized {

	public static void main(String[] args) {

		ChromeOptions chromeOptions = new ChromeOptions();

		if (System.getProperty("os.name").startsWith("Windows")) {

			// To maximize window in Windows system use "--start-maximized"
			chromeOptions.addArguments("--start-maximized");
		} else {

			// To maximize window in Mac & linux system use "--kiosk"
			chromeOptions.addArguments("--kiosk");
		}

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

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

		// Close the browser
		driver.quit();

	}

}

The Source Code for above topic is available on GitHub Repository, You can get it from this URL:- 

0 comments:

Post a Comment