How to maximise browser window in Selenium ? – User friendly Tech help

Scenario:-

n

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

n
n

n

n

n

n

n

n

n

Motivating others to maximize their learning by sharing more.

n

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.

n

Solution:-

n

Approach1:–

n

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

n

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

n

Toolkit utility(Java Library)
n 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.

n

Syntax:-

n

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

n
nCode:-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);n

Approach3:–

n

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

n

Syntax:-

n

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

n

Approach4:–

n

Using WebDriverBackedSelenium

n

What is WebDriverBackedSelenium?
nThis 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.

n

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

n

Syntax:-

n

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

n

Notes:– 
n
n1.We can Resize browser window or Customize browser window size
nwe can use SetSize method (approach2)
nSyntax:-

n

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

n

 
n
n2.We can also use JavaScriptExecutor using its executescript method, to run javascript to maximize a window.
n
n3.In chrome we can use “–start-maximized” in chrome options to maximize window.
nSyntax:-

n

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

n

Code

n

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();
}
}

n

Working with iFrame in Selenium

Was this article helpful?
YesNo

Similar Posts