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
driver.manage().window().maximize();

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.

Scenario

  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,

package com.blog.junitTests;

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;

    @Before
    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 = "https://chandanachaitanya.github.io/selenium-practice-site/";
    }

    @Test
    public void testPageTitle() throws Exception {
        // Open baseUrl in Firefox browser window
        driver.get(baseUrl);        
        // Get current window handle
        String parentWinHandle = driver.getWindowHandle();
        // Locate 'Click to open a small window!' button using id
        WebElement newWindowBtn = driver.findElement(By.id("win2"));
        // Click the button to open a new window
        newWindowBtn.click();
        // 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)) {
                driver.switchTo().window(handle);
                System.out.println("Title of the new window: " + driver.getTitle());
                // Maximize the new window
                driver.manage().window().maximize();
                driver.close();
            }
        }
        // Switching the control back to parent window
        driver.switchTo().window(parentWinHandle);
        // 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!

Leave 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.