Categories
Uncategorized

How to maximise browser window in Selenium ?

Scenario:-

What are the options we have to maximize our application using Selenium 




Enjoy the beauty of nature
Motivating others to maximize their learning by sharing more.

Note:- Above image may look irrelevant as per the topic, but it is to motivate ppl to unleash their hidden potential,maximize learning and share more like a colorful nature which shares freshness and happiness with us.Please share your views as comments.

Solution:-

Approach1:

Using maximize() command
This is one of the recommended methodology, but works for webdriver version >2.21
Syntax:-

driver.manage().window().maximize();

Approach2:

Toolkit utility(Java Library)
 It considers the maximization of the browser window as per the current resolution of the system.It queries the native operating system directly and is platform independent.Fetch the screenresolution of the system that we can use in “setsize” method.What is AWT toolkit.

Syntax:-

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


Code:-
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int)toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(Width,Height); driver.manage().window().setSize(screenResolution);

Approach3:

Using Sendkeys IF nothing works out “SendKeys” is our best buddy.

Syntax:-

  driver.findElement(By.id("test")).sendKeys(Keys.F11);

Approach4:

Using WebDriverBackedSelenium

What is WebDriverBackedSelenium?
This object is provided for backwards compatibility. It allows those who have existing test suites using the Selenium-RC API to use WebDriver under the covers. It’s provided to help ease the migration path to Selenium-Web driver.We can use this object to create our tests with Selenium Remote Control syntax. Remember we don’t need to start the selenium server to work with this.

For browser maximization we will simply use the windowMaximize method of selenium RC in webdriver.

Syntax:-

Selenium selenium = new WebDriverBackedSelenium(driver,baseUrl);
selenium.windowMaximize()


Notes: 

1.We can Resize browser window or Customize browser window size
we can use SetSize method (approach2)
Syntax:-

driver.manage().window().setSize(new Dimension(520, 680));

 

2.We can also use JavaScriptExecutor using its executescript method, to run javascript to maximize a window.

3.In chrome we can use “–start-maximized” in chrome options to maximize window.
Syntax:-

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");

Code

import java.awt.Toolkit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class MaximizeBrowser {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String sUrl = "http://www.uftHelp.com";
driver.get(sUrl);
//Using "Maximize" command
driver.manage().window().maximize();
System.out.println("Maximized browser using 'Maximize' command");
//Resize window
driver.manage().window().setSize(new Dimension(200, 100));
//Using "ToolKit"
Toolkit toolkit = Toolkit.getDefaultToolkit();
//Taking the current width/height of the screen
int width =(int) toolkit.getScreenSize().getWidth();
int height = (int)toolkit.getScreenSize().getHeight();
//using setsize to resize it to the maximum screenResolution
driver.manage().window().setSize(new Dimension(width,height));
System.out.println("Maximized browser using 'Toolkit' Library");
//Resize window
driver.manage().window().setSize(new Dimension(600, 800));
//Using Sendkeys
driver.findElement(By.id("contact-Us")).sendKeys(Keys.F11);
System.out.println("Maximized browser using 'Sendkeys'");
//Quit Browser
driver.quit();

}

}

Working with iFrame in Selenium

Leave a Reply

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