Maximize Browser

Most of the time when you open firefox browser with selenium, browser will be in minimized mode but in few scenarios we might want to maximize the window. we can maximize the browser window with help of maximize() method present in the window class


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Options;
						import org.openqa.selenium.WebDriver.Window;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Quit 
						{
							public static void main(String[] args) throws Exception
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://selenium-mentor.com");
								// below line maximizes the browser window
								driver.manage().window().maximize();							
							}
						}
					


Elaborated Code :

In the above program we were able to maximize in one sigle line, but let's understand the code in eleborated way


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Options;
						import org.openqa.selenium.WebDriver.Window;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Quit 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();	
								driver.get("https://chercher.tech");
								
								// below three line of code is used to maximize the browser
								Options man = driver.manage();		
								Window win = man.window();		
								win.maximize();								
							}
						}

					

We do not have any method to minimize the window of the browser but we can resize the window .

Multi-level Inheritance in Java and Selenium

Browser Size

We can get the size of the browser window by using the getSize() method present in the Dimension class and it returns the Dimension Class type value.

Code Example for get size of Window

						import org.openqa.selenium.Dimension;
						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class GetSize
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								// below lin will fetches the size of the window.
								Dimension size = driver.manage().window().getSize();
								System.out.println("The size of the window : "+size);
							}
						}

					

The Output of the program:
The size of the window : (1024, 546)


If you donot want value with x and y instead of that if you need to get height or width then use following way.

Code Example for get size of Window

						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class GetSize 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();		
								driver.get("https://selenium-mentor.com");	
								int height = driver.manage().window().getSize().height;
								int width =driver.manage().window().getSize().width;
								int height1 = driver.manage().window().getSize().getHeight();
								int width2 = driver.manage().window().getSize().getWidth();		
								System.out.println("The height of the window by height variable : "+height);
								System.out.println("The height of the window by width variable : "+height1);
								System.out.println("by methods");
								System.out.println("The height of the window by getHeight() variable : "+width);
								System.out.println("The height of the window by getWidth() variable : "+width2);								
							}
						}
					

Output of the above program:

The height of the window by height variable : 546
The height of the window by width variable : 546
by methods
The height of the window by getHeight() variable :
The height of the window by getWidth() variable : 1024

Optimized Code for get size of Window


						import org.openqa.selenium.Dimension;
						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class SetPosition 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();		
								driver.get("https://selenium-qtp-mentor.com");	
								Dimension size = driver.manage().window().getSize();
								int height = size.height;
								int width =size.width;
								int height1 = size.getHeight();
								int width2 = size.getWidth();		
								System.out.println("The height of the window by height variable : "+height);
								System.out.println("The height of the window by height1 variable : "+height1);
								System.out.println("by methods");
								System.out.println("The width of the window by getWidth() variable : "+width);
								System.out.println("The width of the window by getWidth() variable : "+width2);								
							}
						}

					

Set Window Size

We can set the size of the browser window by using the setSize() method present in the window class.It accepts the point class constructor as the argument .


						import org.openqa.selenium.Dimension;
						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class SetSize 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();		
								driver.get("https://selenium-mentor.com");
								// below line sets the size of the browser
								driver.manage().window().setSize(new Dimension(200, 200));								
							}
						}
					

Add screenshot to pdf in selenium

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Copyright © CherCher Tech