About this lesson
Set the browser window size in Selenium Java with setSize() and Dimension
Learn Selenium JavaAdvancedWindow Management
How it works
driver.manage().window().setSize(new Dimension(1280, 800)) sets the outer size of the browser window in pixels, and getSize() reads it back as a Dimension with getWidth() and getHeight(). setPosition() and getPosition() do the same for the window's position, using a Point.
The outer size includes the browser's own toolbars, so the page gets less room. At 1024 by 768 in this runner, the page's own viewport is 1024 by 625. When a layout breakpoint matters, check window.innerWidth through JavaScript rather than the outer size.
Size changes what the page shows. The site's runner starts headless Chrome at 780 by 580, and at that width the testkru sidebar is pushed off-screen, which is why links in it are not displayed in earlier lessons. maximize() in headless mode cannot use a real screen; here it produces 800 by 600, so set an explicit size instead.
About the assignment
You resize the browser on the Text Fields page to a common laptop size and read the size back, then compare it with what you set.
What you will do
- Create a
Dimensionwith the width and height from the task and pass it tosetSize(). - Read the size back with
getSize(). - Print the width and the height on separate lines with the labels from the expected output.
Common mistakes
Mixing up the Dimension import
java.awt.Dimensionhas the same name. Selenium needsorg.openqa.selenium.Dimension; with the wrong import,setSize()does not compile.Using maximize() in headless runs
It does not mean "screen size" without a screen. Set an explicit size so every run uses the same layout.
Reading the size from the page
window.innerHeightreports the viewport, 625 here rather than the window height, because the window size includes the browser's toolbars. Compare like with like.
In a real test suite
Suites set one window size in setup so screenshots and layouts are identical on every machine, and responsive tests run the same steps at phone, tablet and desktop widths. ChromeOptions can also set the size at start with the --window-size argument.
Lesson 49 managed tabs. Lesson 51 starts the Actions class, beginning with a mouse hover.
Related lessons
- Find Element by Link TextA link whose visibility depends on window width.
- Take Viewport ScreenshotWindow size decides how much of the page the viewport screenshot captures.
- Get Window Handle and TitleThe window handle that these size calls apply to.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/TextFields");
// TODO: Set window size to 1024x768
// TODO: Get and print the window dimensions
driver.quit();
}
}