playwright
playwright copied to clipboard
[Feature] BrowserContext.maximizeWindow()
Selenium feature: https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html#selenium.webdriver.remote.webdriver.WebDriver.maximize_window
Requested via Slack.
Scenario: Automate a website and then let the user complete something. Its great if the max possible width/height could be determined so the user has "maximum available viewport".
But not sure how or if its easy implementable since we in CR e.g. emulate the viewport independent from the window size.
For Chromium it works already like that:
// @ts-check
const playwright = require('playwright');
(async () => {
const browser = await playwright.chromium.launch({
args: ["--start-maximized"],
headless: false,
});
const context = await browser.newContext({
viewport: null
});
const page = await context.newPage();
await page.goto('http://whatsmyuseragent.org/');
await browser.close();
})();
It is a great feature
IT would be great to have this !
Voting this up. It would "obviously" include a full screen trigger too which brings us to https://github.com/microsoft/playwright/issues/1086.
Voting this up!
+1 for this solution
+1 for this solution
in Selenium we have .maximize(), so de we have any such in playwright?
Would really be great to have this
Solution for Java:

Upvote
Upvote.
Upvote
Upvote
Upvote
Upvoting this as well. Use cases:
- being able to see the whole page clearly while working on tests locally
- allowing multiple people with different screens to work on testautomation without any problems (fixed viewport would be problematic because everyone has different monitors and resolutions)
- running tests on Browserstack and clearly seeing what's actually happening in the videos (see the issue reported by another user here: https://playwright.slack.com/archives/CSUHZPVLM/p1646804973919919)
Solution for Java:
Unfortunately, this is not working!
@azad-derakhshani-GS
allowing multiple people with different screens to work on testautomation without any problems (fixed viewport would be problematic because everyone has different monitors and resolutions)
This looks backwards to me: maximize would make pages behave differently, while fixed viewport guarantees consistent behavior. What do I miss?
Upvoting!
Upvoting
@azad-derakhshani-GS This looks backwards to me: maximize would make pages behave differently, while fixed viewport guarantees consistent behavior. What do I miss?
A well-designed page behaves consistently across different viewports, so this is nothing for the testautomation framework to worry about. Trust me, in several years of TA experience, maximizing the browser window never caused any such issues, not even once. It's also closer to the way a real user would behave. But Playwright artificially shrinks the viewport so the pages aren't even displayed in full, which (despite it scrolling into elements) leads to page elements not being fully displayed while PW interacts with them.
Also to elaborate a bit further on the second point from my previous post:
Not having a flexible (= maximized) resolution means I have to define a specific screen resolution (even null does mean 1280x720). But a static resolution could be incompatible with other people's screens.
Sizing the browser window flexibly via maximize eliminates that problem.
Solution in Java:
try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = (int)screenSize.getWidth(); int height = (int)screenSize.getHeight();
BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(width,height) Page page = context.newPage();
Upvote
Upvote
Upvote
does anyone have solution in .net(c#) for maximizing the browser window?
does anyone have solution in .net(c#) for maximizing the browser window?
The following worked for me at last.
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
Args = new [] { "--start-maximized" },
SlowMo = 100
});
var context = await browser.NewContextAsync(new BrowserNewContextOptions {
ViewportSize = ViewportSize.NoViewport
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");
does anyone have solution in .net(c#) for maximizing the browser window?
The following worked for me at last.
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, Args = new [] { "--start-maximized" }, SlowMo = 100 }); var context = await browser.NewContextAsync(new BrowserNewContextOptions { ViewportSize = ViewportSize.NoViewport }); var page = await context.NewPageAsync(); await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");
Thanks Mamoorkhan,
At least the above now changes the browser size. But It is not maximizing to the full screen. Like only half - as if split screen with another app.
Any idea?
@pavelfeldman mamoorkhan
does anyone have solution in .net(c#) for maximizing the browser window?
The following worked for me at last.
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, Args = new [] { "--start-maximized" }, SlowMo = 100 }); var context = await browser.NewContextAsync(new BrowserNewContextOptions { ViewportSize = ViewportSize.NoViewport }); var page = await context.NewPageAsync(); await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");
@mamoorkhan Thanks, but still I am not seeing the maximized browser by using the above code
@NitishJoggessur and @dbhimar I was getting the same half screen maximised window when I was using only the --start-maximized flag, but when I added the viewport settings and created a new context for the page, it worked. I am using windows 10 x64 Enterprise. I am not sure if that helps.
@NitishJoggessur and @dbhimar I was getting the same half screen maximised window when I was using only the --start-maximized flag, but when I added the viewport settings and created a new context for the page, it worked. I am using windows 10 x64 Enterprise. I am not sure if that helps. @mamoorkhan
I am also using the windows 10 x64
_playwright = await Playwright.CreateAsync();
_browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
Channel = "msedge",
Args = new[]
{
"--start-maximized"
},
});
_browserContext = await _browser.NewContextAsync();
await _browserContext.Tracing.StartAsync(new()
{
Screenshots = true,
Snapshots = true,
Sources = true
});
_page = await _browserContext.NewPageAsync();
await _browser.NewContextAsync(new BrowserNewContextOptions()
{
RecordVideoDir = "videos/",
ViewportSize = ViewportSize.NoViewport
});
above is the code which I am trying to maximize the browser window