Scroll to top

9z. WebDriver – Window maximize

Hiya Champs! Things don’t always work the way we want them to and that’s where the challenges take their form. With Selenium Webdriver, our testing lives are made far easier than we would expect them to be. One such situation is maximizing the browser window.

Screenshots are a life saver and in order to view all the web elements while grabbing one, it is important to maximize the browser window. Therefore, instead of scrolling down to a particular element, just maximize the window and complete the task at hand.

// Maximize the new window

Can you believe it? This one line is all that you were looking for! Finding it difficult to digest? No worries. Let us see an example to see this piece of code in action.


  1. Open Firefox browser
  2. Navigate to the demo site
  3. Get the current window handle
  4. Locate ‘Click to open a small window!’ button using Id
  5. Click the button to open the small window
  6. Get the window handles of both the open windows
  7. Loop through both handles
  8. Switch to the new window with the handle’s reference
  9. Get the title and print it to console
  10. Maximize the small window to full-screen size
  11. Close the new window
  12. Switch the control back to parent window and print the URL to the console
  13. Verify Eclipse IDE console output screen and JUnit pane for success result

JUnit code for this scenario is,


import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WindowMaximize {
    // Declaring variables
    private WebDriver driver;
    private String baseUrl;

    public void setUp() throws Exception {
        // Selenium version 3 beta releases require system property set up
        System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
                + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
        // Create a new instance for the class FirefoxDriver
        // that implements WebDriver interface
        driver = new FirefoxDriver();
        // Implicit wait for 5 seconds
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        // Assign the URL to be invoked to a String variable
        baseUrl = "";

    public void testPageTitle() throws Exception {
        // Open baseUrl in Firefox browser window
        // Get current window handle
        String parentWinHandle = driver.getWindowHandle();
        // Locate 'Click to open a small window!' button using id
        WebElement newWindowBtn = driver.findElement("win2"));
        // Click the button to open a new window;
        // Get the window handles of all open windows
        Set<String> winHandles = driver.getWindowHandles();
        // Loop through all handles
        for (String handle : winHandles) {
            if (!handle.equals(parentWinHandle)) {
                System.out.println("Title of the new window: " + driver.getTitle());
                // Maximize the new window
        // Switching the control back to parent window
        // Print the URL to the console
        System.out.println("Parent window URL: " + driver.getCurrentUrl());

    } // End of @Test

Execution Result:

Clear comments make the code self-explanatory.

In Eclipse IDE -> JUnit pane -> green bar shows that the test case is executed successfully. Console window shows the absence of any errors. It also shows all the printed messages as expected.

Don’t forget to shout in the comments section in case of any clarifications!

I will see you soon in another post. Have a great day!

Author avatar
Chandana Chaitanya
Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, “this is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.