0
0
Selenium Javatesting~15 mins

Window management (maximize, size) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify browser window maximize and resize functionality
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com'
Step 2: Maximize the browser window
Step 3: Verify the browser window is maximized
Step 4: Resize the browser window to width 800 pixels and height 600 pixels
Step 5: Verify the browser window size is 800x600 pixels
✅ Expected Result: Browser window should maximize successfully and then resize to 800x600 pixels with correct size verification
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify window is maximized by comparing window size to screen size
Verify window size after resize matches 800x600 pixels
Best Practices:
Use explicit waits if needed before assertions
Use WebDriver's manage().window() methods for window operations
Use assertions from a testing framework like TestNG or JUnit
Avoid hardcoded waits; prefer checking window size directly
Automated Solution
Selenium Java
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class WindowManagementTest {
    private WebDriver driver;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver executable if needed
        // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testWindowMaximizeAndResize() {
        driver.get("https://example.com");

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

        // Get window size after maximize
        Dimension maxSize = driver.manage().window().getSize();

        // Get screen size from Toolkit
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        // Assert window size is close to screen size (allow small difference)
        Assert.assertTrue(
            Math.abs(maxSize.getWidth() - screenSize.getWidth()) < 10 &&
            Math.abs(maxSize.getHeight() - screenSize.getHeight()) < 10,
            "Window is not maximized properly"
        );

        // Resize window to 800x600
        Dimension newSize = new Dimension(800, 600);
        driver.manage().window().setSize(newSize);

        // Get window size after resize
        Dimension resizedSize = driver.manage().window().getSize();

        // Assert window size matches 800x600
        Assert.assertEquals(resizedSize.getWidth(), 800, "Width is not 800 pixels");
        Assert.assertEquals(resizedSize.getHeight(), 600, "Height is not 600 pixels");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with Java and TestNG for assertions.

setUp() initializes the ChromeDriver before tests.

testWindowMaximizeAndResize() opens the example.com website, maximizes the window, and verifies the window size is close to the screen size using Java AWT Toolkit to get screen dimensions.

Then it resizes the window to 800x600 pixels and verifies the window size matches exactly.

Assertions ensure the window is properly maximized and resized.

tearDown() closes the browser after the test.

This approach uses WebDriver's window management methods and TestNG assertions for clear validation.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() to wait for window resize instead of directly checking window size
Hardcoding window size values without verifying screen size for maximize check
Not quitting the WebDriver session after test
Bonus Challenge

Now add data-driven testing with 3 different window sizes: 1024x768, 1280x720, and 1366x768

Show Hint