Repository navigation
feat(browserContext): support maximized viewport - #43363
Devin Rousso (dcrousso) wants to merge 2 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| Use `null` to disable the consistent viewport emulation. Learn more about [viewport emulation](../emulation#viewport). | ||
| Use `"maximized"` to disable viewport emulation and maximize each native browser window. This is only supported in headed mode and does not enter fullscreen mode or trigger the web Fullscreen API. | ||
|
|
||
| Calling [`method: Page.setViewportSize`] overrides `"maximized"` for that page and resizes its native window. |
There was a problem hiding this comment.
I think this sentence is confusing rather than helping.
| if (options.userAgent) | ||
| lines.push(` .setUserAgent(${quote(options.userAgent)})`); | ||
| if (options.viewport) | ||
| if (options.viewport && options.viewport !== 'maximized') |
There was a problem hiding this comment.
Why suddenly a java-only change? I'd expect either all languages or none.
| tests non-deterministic. | ||
| ::: | ||
|
|
||
| ## java-context-option-viewport |
There was a problem hiding this comment.
Let's sketch out how this API looks for other languages right away.
| properties: | ||
| width: int | ||
| height: int | ||
| viewportMaximized: boolean? |
There was a problem hiding this comment.
I'd recommend an enum viewportMode instead of noDefaultViewport+viewportMaximized. Otherwise it's unclear what noDefaultViewport: true, viewportMaximized: true means.
| it('should create context in maximized window without entering fullscreen', async ({ browser, browserName }) => { | ||
| const context = await browser.newContext({ viewport: 'maximized' }); | ||
| const page = await context.newPage(); | ||
| await expect.poll(() => windowIsMaximized(context, page, browserName)).toBe(true); |
There was a problem hiding this comment.
Why do we have to poll here? Shouldn't the newPage already be maximized right away? I'd expect that from the API.
| startAutomaticVideoRecording(this._page); | ||
| await Promise.all(promises); | ||
| if (contextOptions.viewportMaximized) { | ||
| await this._browserContext._browser._browserSession.send('Playwright.maximizeWindow', { |
There was a problem hiding this comment.
- Why is this not a part of
_updateViewport()method? It seems to be either viewport or maximized, but not both. - Similarly, does setting viewport undo
Playwright.maximizeWindow? If so, perhaps it should be the same webkit protocol method as well?
| await context.close(); | ||
| }); | ||
|
|
||
| it('should override maximized viewport when resizing a page', async ({ browser, browserName }) => { |
There was a problem hiding this comment.
I'd expect a symmetric test as well: create a context with viewport=567x345, then call page.setViewportSize('maximized') after.
| }, | ||
| })); | ||
| } | ||
| if (this._options.viewportMaximized) { |
There was a problem hiding this comment.
It feels like this should be a part of doUpdateDefaultViewport().
|
|
||
| async _initialize(hasUIWindow: boolean) { | ||
| const browserOptions = this._crPage._browserContext._browser.options; | ||
| const options = this._crPage._browserContext._options; |
There was a problem hiding this comment.
nit: contextOptions
| })); | ||
| } | ||
| // Make sure that the viewport emulationis set after the embedder window resize. | ||
| promises.push(this._client.send('Emulation.setDeviceMetricsOverride', metricsOverride)); |
There was a problem hiding this comment.
I'd expect the new "maximized" option to affect metricsOverride somehow...
12ac87f to
86ffb77
Compare
86ffb77 to
d74f097
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
d74f097 to
b9b3719
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
b9b3719 to
afcd112
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
🟢 The CI failure is a confirmed pre-existing flakeHi, I'm the Playwright bot and I checked the latest CI reports. The failure is unlikely to be caused by this PR. DetailsThe latest MCP report has one failure; the latest library/test-runner report has none. CI tested PR head Pre-existing flake
Triaged by the Playwright bot - agent run |
| this._browserContext._browser._browserSession.send('Playwright.maximizeWindow', { | ||
| pageProxyId: this._pageProxySession.sessionId, | ||
| }), | ||
| this._session.send('Page.setScreenSizeOverride', {}), |
There was a problem hiding this comment.
Shouldn't this also clear Emulation.setDeviceMetricsOverride?
| it.skip(({ isBidi }) => isBidi, 'maximized viewport is not supported by BiDi'); | ||
|
|
||
| async function windowIsMaximized(context: BrowserContext, page: Page, browserName: string) { | ||
| if (browserName === 'chromium') { |
There was a problem hiding this comment.
I wonder why use CDP instead of checking outerWidth/outerHeight like in other browsers?
|
|
||
| override async doUpdateDefaultViewport() { | ||
| if (this._options.viewportMode === 'maximized') { | ||
| await this._browser.session.send('Browser.setWindowMaximized', { |
There was a problem hiding this comment.
Since we control juggler, I'd prefer to use Browser.setDefaultViewport with "maximized" option, because maximized vs viewport are mutually exclusive. Otherwise, it's unclear what juggler would do if you send both commands. WDYT?
| return; | ||
| const promises = []; | ||
| if (!preserveWindowBoundaries && this._windowId) { | ||
| if (options.viewportMode === 'maximized') |
There was a problem hiding this comment.
I think it is better to gate this by a local field this._windowState, similar to this._metricsOverride, which would mean "the last set window state".
| Use `viewport="maximized"` to disable viewport emulation and maximize native browser windows. This is only supported in headed mode and does not enter fullscreen mode or trigger the web Fullscreen API. | ||
|
|
||
| :::note | ||
| The `no_viewport=True` and `viewport="maximized"` values opt out from the default presets and make viewport depend on the host window size defined by the operating system. It makes the execution of the tests non-deterministic. |
There was a problem hiding this comment.
Seeing these two together shows the non-ideal api 😄
There was a problem hiding this comment.
agreed
i kept the existing no_viewport option for compatibility rather than adding another flag for maximization
are we OK having a breaking change for this or do we need to keep it?
There was a problem hiding this comment.
We have to keep it for sure.
| import { PythonLanguageGenerator } from '../../../packages/isomorphic/codegen/python'; | ||
| import { getAPIRequestCodeGen } from '../../../packages/trace-viewer/src/ui/codegen'; | ||
|
|
||
| test('CG-192 should generate maximized viewport context options', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/1086' } }, () => { |
There was a problem hiding this comment.
I can't say I love this change detector test.
allow `viewport: 'maximized'` to maximize native browser windows without a default emulated viewport keep native maximization independent of browser fullscreen and the web Fullscreen API preserve the maximized outer window when `Page.setViewportSize()` enables viewport emulation
`noDefaultViewport` duplicates `viewportMode` use `viewportMode` as the only viewport mode setting
afcd112 to
c6e835a
Compare
Test results for "tests 1"1 flaky23594 passed, 725 skipped Merge workflow run. |
Test results for "MCP"5194 passed, 616 skipped Merge workflow run. |
allow
viewport: 'maximized'to maximize native browser windows without a default emulated viewportkeep native maximization independent of browser fullscreen and the web Fullscreen API
restore and resize the native window when
page.setViewportSize()enables viewport emulationfixes #1086