How to Maximize Browser in Selenium Webdriver

Before explaining that how to maximize browser in selenium Webdriver. First you need to know that why there is a need to maximize the browser.
Whenever you run Webdriver command to perform any action on web element, Selenium Webdriver search for that element and automatically attempts to scrolls a page (if it lies at the bottom of a page) in order to perform actions on it. If Webdriver API fails to scroll, it throws an exception.
Mean to say, if your web page size is bigger than your screen size and you wanna click on an element which lies at the bottom of a web page then Webdriver automatically scrolls a web page to perform a click on that web element.

sometimes you may have to maximize browser during scripts execution to avoid excessive scrolling and sometimes actions like ‘click’ may fail, if browser is not in maximized mode.

Here I will talk about the 2 different ways by which one can maximize browser.

First option :- Selenium provides an in-built function – maximize() for it.

// use this command to maximize browser
driver.manage().window().maximize();

Example:-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestSample {
  WebDriver driver;
  String baseUrl;

  @BeforeTest
  public void start() {
    driver = new FirefoxDriver();
    baseUrl = "http://www.gmail.com";
  }

  @Test
  public void testApp() throws InterruptedException {
    System.out.println("Launch Application");
    driver.get(baseUrl);
    driver.manage().window().maximize();
    Thread.sleep(5000);
  }

  @AfterTest
  public void close() {
    //closes browser
    driver.quit();
  }
}

Second Option :- If above code doesn’t work, you can use below code :

Toolkit toolkit = Toolkit.getDefaultToolkit();
int screenWidth = (int) toolkit.getScreenSize().getWidth();
int screenHeight = (int)toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(screenWidth, screenHeight);
driver.manage().window().setSize(screenResolution);</pre>

<strong style="color: green;">Example:-</strong>
<pre lang="java">
import java.awt.Toolkit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestSample {
  WebDriver driver;
  String baseUrl;

  @BeforeTest
  public void start() {
    driver = new FirefoxDriver();
    baseUrl = "http://www.gmail.com";
  }

  @Test
  public void testApp() throws InterruptedException {
    System.out.println("Launch Application");
    driver.get(baseUrl);
    
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    int screenWidth = (int) toolkit.getScreenSize().getWidth();
    int screenHeight = (int)toolkit.getScreenSize().getHeight();
    Dimension screenResolution = new Dimension(screenWidth, screenHeight);
    driver.manage().window().setSize(screenResolution);
    
    Thread.sleep(5000);
  }
 
  @AfterTest
  public void close() {
    //closes browser
    driver.quit();
  }

}

Leave a Reply

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