Window management (maximize, size) in Selenium Java - Build an Automation Script
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.
Now add data-driven testing with 3 different window sizes: 1024x768, 1280x720, and 1366x768
Practice
maximize() method do in Selenium WebDriver window management?Solution
Step 1: Understand the purpose of maximize()
Themaximize()method is used to enlarge the browser window to fill the entire screen.Step 2: Differentiate from other window methods
UnlikesetSize()which sets a custom size,maximize()fills the screen automatically.Final Answer:
It makes the browser window fill the entire screen. -> Option BQuick Check:
maximize() = fills screen [OK]
- Confusing maximize() with setSize()
- Thinking maximize() returns window size
- Assuming maximize() closes the window
Solution
Step 1: Recall correct method to set window size
In Selenium Java,setSize()requires aDimensionobject with width and height.Step 2: Check syntax correctness
driver.manage().window().setSize(new Dimension(1024, 768)); correctly usesdriver.manage().window().setSize(new Dimension(1024, 768));. Other options misuse method names or parameters.Final Answer:
driver.manage().window().setSize(new Dimension(1024, 768)); -> Option DQuick Check:
setSize() needs Dimension object [OK]
- Calling maximize() with parameters
- Using setSize() without Dimension object
- Wrong method chaining like driver.window()
driver.manage().window().setSize(new Dimension(800, 600)); Dimension size = driver.manage().window().getSize(); System.out.println(size.getWidth() + "," + size.getHeight());
Solution
Step 1: Set window size to 800x600
The code sets the browser window size to width=800 and height=600 usingsetSize().Step 2: Retrieve and print the window size
ThegetSize()method returns the current size, which is then printed as "width,height".Final Answer:
800,600 -> Option AQuick Check:
setSize(800,600) then getSize() = 800,600 [OK]
- Mixing width and height order
- Expecting maximize() effect instead
- Assuming getSize() returns null or error
driver.manage().window().setSize(1024, 768);
Solution
Step 1: Check setSize() method parameters
ThesetSize()method requires a singleDimensionobject, not two separate integers.Step 2: Identify the error cause
Passing two integers directly causes a compile-time error because no matching method exists.Final Answer:
setSize() requires a Dimension object, not two integers. -> Option CQuick Check:
setSize() needs Dimension object [OK]
- Passing width and height as separate arguments
- Using wrong method name
- Ignoring compile-time errors
Solution
Step 1: Maximize the browser window
Usemaximize()without parameters to fill the screen.Step 2: Get window size and check width
Retrieve size withgetSize()and compare width with 1200 pixels.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.Final Answer:
Option A code snippet correctly maximizes and checks width. -> Option AQuick Check:
maximize() then getSize().getWidth() > 1200 [OK]
- Passing parameters to maximize()
- Checking height instead of width
- Using setSize() instead of maximize() for full screen
Ready to prove it?
Pass the test and this pattern moves unknown
