Change Browser Window Size
Learn how to resize browser window during an Automation tests.
Introduction
You can use native framework commands or methods to resize browser windows in an Automate session. This guide will help you:
- Maximize browser window during test runtime
- Resize browser window during test runtime
Maximizing browser window size
In any Selenium test - whether run locally or on a Selenium Grid—the browser window is not maximized upon launch. This can keep you from viewing all the elements of the web application you’re testing.
You can programmatically maximize the browser window by adding a Selenium command to your test script.
Supported Browsers and Operating Systems
Description | |
---|---|
Supported Browser | All (Chrome, Safari, Firefox, Edge, Opera) |
The following sample shows you how to maximize a browser window:
driver.manage().window().maximize();
driver.manage().window().maximize();
driver.Manage().Window.Maximize();
$web_driver->manage()->window()->maximize();
driver.maximize_window()
driver.manage.window.maximize
$driver->maximize_window();
Resizing the browser window
You might have some test scenarios which require you to resize the browser window. To execute this step, you can add a Selenium command to your test script.
Supported Browsers and Operating Systems
Description | |
---|---|
Supported Browser | All (Chrome, Safari, Firefox, Edge, Opera) |
The following sample shows you how to resize the browser window to custom dimensions using Selenium:
/* Requires Java Library
You need to import the library org.openqa.selenium.Dimension into your Java project before implementing the resize action in your script. */
Dimension dimension = new Dimension(1920, 1080);
driver.manage().window().setSize(dimension);
driver.manage.window.resize_to(1920, 1080)
driver.Manage().Window.Size = new Size(x, y);
driver.set_window_size(width, height)
driver.manage().window().setSize(x, y);
/* Requires PHP Class
You need to use the class "Facebook\WebDriver\WebDriverDimension" into your PHP test before implementing the resize action in your code. */
$web_driver->manage()->window()->setSize(new WebDriverDimension(1920, 1080));
$driver->set_window_size(1920, 1080);
By default, the Playwright tests run on a default viewport size of 1280x720
. But that might not be the screen resolution of the OS where your test in running. While you have the option of selecting the desktop screen resolution of your choice, you can also use the option shown below to maximize the browser window during your Playwright test.
const { chromium } = require('playwright');
const cp = require('child_process');
const clientPlaywrightVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];
(async () => {
const caps = {
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
'client.playwrightVersion': clientPlaywrightVersion
};
const browser = await chromium.connect({
wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
});
const context = await browser.newContext({
viewport: null
});
const page = await context.newPage();
await page.goto('https://www.google.com/ncr');
await browser.close();
})();
You can also choose any other viewport
while creating the newContext
. Read more about viewport options while creating contexts in Playwright.
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!