How to Maximize Browser Window in Selenium Ruby
Maximize the current browser window in Selenium Ruby with `driver.manage.window.maximize`. Learn when to resize or use fullscreen, and how window behavior affects tests.
To maximize a browser window in Selenium Ruby, call driver.manage.window.maximize on your WebDriver instance. It asks the operating system to enlarge the current browser window to the maximum available area; the exact resulting geometry can vary by operating system and window manager.
Runnable Ruby example
Install the Selenium Ruby gem, then create a WebDriver session, maximize its current window, and quit the session when finished:
gem install selenium-webdriver
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
begin
driver.manage.window.maximize
driver.navigate.to 'https://example.com'
puts "Window size: #{driver.manage.window.size}"
ensure
driver.quit
end
The maximize call is the same for other Selenium-supported browsers: create the appropriate driver, then call driver.manage.window.maximize. The code uses Chrome as an example. Selenium documents this window operation in its WebDriver window-management guide and Ruby Bindings guide.
What maximizing does
Maximizing enlarges the current WebDriver window. On most operating systems, the window fills the available screen area while leaving operating-system menus and toolbars visible. This is different from fullscreen, and it does not guarantee identical browser dimensions on every machine.
Use maximize when the test should use the largest area provided by the environment. If a test depends on a responsive breakpoint or repeatable viewport, set explicit dimensions and check the size in the environment where the test runs. The browser window’s outer size and the web page’s viewport are not necessarily the same.
Choose maximize, resize, or fullscreen
| Goal | Ruby call | Use it when |
|---|---|---|
| Use the maximum available window area | driver.manage.window.maximize |
The test should adapt to the screen or window manager. |
| Request specific window dimensions | driver.manage.window.resize_to(1024, 768) |
You need a chosen size, such as when exercising a particular layout. |
| Fill the screen like an F11-style presentation | driver.manage.window.fullscreen |
You want fullscreen behavior rather than a maximized window with system chrome. |
# Request a particular outer-window size
driver.manage.window.resize_to(1024, 768)
# Use the browser's fullscreen mode instead
driver.manage.window.fullscreen
These are separate operations. Maximize uses the available area, resize_to requests dimensions, and fullscreen fills the screen. The Selenium guide describes fullscreen as similar to pressing F11 in most browsers. A requested outer-window size may not equal the page viewport size.
Reliable window sizing in tests
- Pick the state that matches the test. Use maximize for the largest available window; use a fixed size for tests that target a known layout; use fullscreen only when fullscreen behavior is part of the scenario.
- Apply the operation after creating the driver. The call operates on the current WebDriver window.
- Check the resulting dimensions when they matter. Window managers and execution environments can affect the result. Selenium exposes the window size through
driver.manage.window.size. - Keep test environments consistent. Headless and remote browser environments may have different screen or window-manager behavior than a local desktop. If your layout assertion depends on exact dimensions, request a size and verify what the page receives in that environment.
Do not treat maximize as a portable way to select a specific responsive breakpoint. For repeatable layout checks, explicit sizing and verification are the dependable approach.
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| The window does not fill the physical display. | Maximize uses the area offered by the operating system or window manager, which may exclude system menus and toolbars. | Use fullscreen if the scenario requires the entire screen, or set explicit dimensions if it requires a known size. |
| The page still uses a narrow or unexpected layout. | The browser viewport differs from the outer window size, or the environment’s available screen area is smaller than expected. | Inspect driver.manage.window.size and verify the page viewport in the same environment. Set a specific window size if needed. |
| The maximize call appears to have no effect. | The current session or environment may not provide a conventional visible desktop window, or the wrong window may be selected. | Confirm the driver session is active and the intended window is current. In remote or headless runs, request a size and verify the resulting dimensions. |
| A test passes locally but fails in another environment. | Available screen geometry and window-manager behavior differ. | Avoid relying on maximize for exact geometry. Use an explicit size and validate the rendered viewport where the test runs. |
Performance, reliability, and cost
Maximizing is a WebDriver window-management command; it does not itself capture a screenshot or create another browser session. For reliable tests, the key concern is environmental variation, not a particular performance or cost figure. Reuse the browser session for the test flow, choose the intended window state, and verify dimensions when layout depends on them.
Or skip the browser setup
If your goal is a website screenshot rather than a Selenium window-management test, ScreenshotNeo returns an image or PDF from one GET request. Its API can be used without creating and managing a Selenium browser session. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const bytes = new Uint8Array(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', bytes));
ScreenshotNeo removes cookie banners, popups, and chat widgets before the shot. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots, and 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000. Sign up for the free plan.
FAQ
Does maximize mean fullscreen?
No. Maximize generally fills the available area while leaving operating-system menus and toolbars visible. Fullscreen is the F11-like mode.
Can maximize guarantee a desktop viewport size?
No. The resulting geometry depends on the operating system and window manager. Set a specific size and verify the page viewport when exact dimensions matter.
Which window does the command affect?
It affects the current WebDriver window. Make sure the intended window is selected before applying the operation.


