Change browser window wize
Learn how to resize browser window during an Automation tests.
You can use native framework commands or methods to resize browser windows in an Automate TurboScale 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 does not maximize on launch. This can keep you from viewing all the elements of the web application you are testing.
You can maximize the browser window by using a Selenium command in your test script.
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
Some test scenarios may require you to resize the browser window. You can add a Selenium command to your test script to execute this step.
If the browser window is larger than the display screen resolution, elements will appear off-screen. Learn how to change the desktop screen resolution.
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 is running. While you can select the desktop screen resolution of your choice, you can also use the following option 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!