Bird
Raised Fist0
Selenium Javatesting~15 mins

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

Choose your learning style10 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

Practice

(1/5)
1. What does the maximize() method do in Selenium WebDriver window management?
easy
A. It retrieves the current size of the browser window.
B. It makes the browser window fill the entire screen.
C. It sets the browser window to a custom size.
D. It closes the browser window.

Solution

  1. Step 1: Understand the purpose of maximize()

    The maximize() method is used to enlarge the browser window to fill the entire screen.
  2. Step 2: Differentiate from other window methods

    Unlike setSize() which sets a custom size, maximize() fills the screen automatically.
  3. Final Answer:

    It makes the browser window fill the entire screen. -> Option B
  4. Quick Check:

    maximize() = fills screen [OK]
Hint: maximize() fills the screen fully, setSize() sets custom size [OK]
Common Mistakes:
  • Confusing maximize() with setSize()
  • Thinking maximize() returns window size
  • Assuming maximize() closes the window
2. Which of the following is the correct syntax to set the browser window size to 1024x768 in Selenium Java?
easy
A. driver.manage().window().maximize(1024, 768);
B. driver.window().setSize(1024, 768);
C. driver.setSize(1024, 768);
D. driver.manage().window().setSize(new Dimension(1024, 768));

Solution

  1. Step 1: Recall correct method to set window size

    In Selenium Java, setSize() requires a Dimension object with width and height.
  2. Step 2: Check syntax correctness

    driver.manage().window().setSize(new Dimension(1024, 768)); correctly uses driver.manage().window().setSize(new Dimension(1024, 768));. Other options misuse method names or parameters.
  3. Final Answer:

    driver.manage().window().setSize(new Dimension(1024, 768)); -> Option D
  4. Quick Check:

    setSize() needs Dimension object [OK]
Hint: Use new Dimension(width, height) inside setSize() [OK]
Common Mistakes:
  • Calling maximize() with parameters
  • Using setSize() without Dimension object
  • Wrong method chaining like driver.window()
3. What will be the output of the following code snippet?
driver.manage().window().setSize(new Dimension(800, 600));
Dimension size = driver.manage().window().getSize();
System.out.println(size.getWidth() + "," + size.getHeight());
medium
A. 800,600
B. 600,800
C. 0,0
D. Throws a runtime exception

Solution

  1. Step 1: Set window size to 800x600

    The code sets the browser window size to width=800 and height=600 using setSize().
  2. Step 2: Retrieve and print the window size

    The getSize() method returns the current size, which is then printed as "width,height".
  3. Final Answer:

    800,600 -> Option A
  4. Quick Check:

    setSize(800,600) then getSize() = 800,600 [OK]
Hint: getSize() returns current size after setSize() [OK]
Common Mistakes:
  • Mixing width and height order
  • Expecting maximize() effect instead
  • Assuming getSize() returns null or error
4. Identify the error in the following Selenium Java code snippet for setting window size:
driver.manage().window().setSize(1024, 768);
medium
A. The method name should be setWindowSize(), not setSize().
B. setSize() cannot be called on driver.manage().window().
C. setSize() requires a Dimension object, not two integers.
D. The code is correct and will run without errors.

Solution

  1. Step 1: Check setSize() method parameters

    The setSize() method requires a single Dimension object, not two separate integers.
  2. Step 2: Identify the error cause

    Passing two integers directly causes a compile-time error because no matching method exists.
  3. Final Answer:

    setSize() requires a Dimension object, not two integers. -> Option C
  4. Quick Check:

    setSize() needs Dimension object [OK]
Hint: Always wrap width and height in new Dimension() for setSize() [OK]
Common Mistakes:
  • Passing width and height as separate arguments
  • Using wrong method name
  • Ignoring compile-time errors
5. You want to maximize the browser window and then verify if the window width is greater than 1200 pixels. Which code snippet correctly performs this check in Selenium Java?
hard
A. driver.manage().window().maximize(); Dimension size = driver.manage().window().getSize(); if (size.getWidth() > 1200) { System.out.println("Width is greater than 1200"); }
B. driver.manage().window().setSize(new Dimension(1200, 800)); Dimension size = driver.manage().window().getSize(); if (size.getWidth() > 1200) { System.out.println("Width is greater than 1200"); }
C. driver.manage().window().maximize(1200, 800); Dimension size = driver.manage().window().getSize(); if (size.getWidth() > 1200) { System.out.println("Width is greater than 1200"); }
D. driver.manage().window().maximize(); Dimension size = driver.manage().window().getSize(); if (size.getHeight() > 1200) { System.out.println("Width is greater than 1200"); }

Solution

  1. Step 1: Maximize the browser window

    Use maximize() without parameters to fill the screen.
  2. Step 2: Get window size and check width

    Retrieve size with getSize() and compare width with 1200 pixels.
  3. Step 3: Verify correctness of condition and output

    driver.manage().window().maximize(); Dimension size = driver.manage().window().getSize(); if (size.getWidth() > 1200) { System.out.println("Width is greater than 1200"); } correctly checks width > 1200 and prints the message.
  4. Final Answer:

    Option A code snippet correctly maximizes and checks width. -> Option A
  5. Quick Check:

    maximize() then getSize().getWidth() > 1200 [OK]
Hint: maximize() no params; getSize() then check width [OK]
Common Mistakes:
  • Passing parameters to maximize()
  • Checking height instead of width
  • Using setSize() instead of maximize() for full screen
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard