Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): playwright provider doesn't allow resizing the browser viewport #5984

Merged
merged 12 commits into from
Jul 1, 2024
Merged
24 changes: 19 additions & 5 deletions packages/browser/src/node/providers/playwright.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
Browser,

BrowserContext,
BrowserContextOptions,
Frame,
Expand Down Expand Up @@ -67,10 +66,21 @@ export class PlaywrightBrowserProvider implements BrowserProvider {

const playwright = await import('playwright')

const browser = await playwright[this.browserName].launch({
const launchOptions = {
...this.options?.launch,
headless: options.headless,
})
} satisfies LaunchOptions

if (this.ctx.config.browser.launchViewport.maximized) {
if (!launchOptions.args) {
launchOptions.args = []
}
if (!launchOptions.args.includes('--start-maximized') && !launchOptions.args.includes('--start-fullscreen')) {
launchOptions.args.push('--start-maximized')
}
}

const browser = await playwright[this.browserName].launch(launchOptions)
this.browser = browser
this.browserPromise = null
return this.browser
Expand All @@ -85,11 +95,15 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
}

const browser = await this.openBrowser()
const context = await browser.newContext({
const options = {
...this.options?.context,
ignoreHTTPSErrors: true,
serviceWorkers: 'allow',
})
} satisfies BrowserContextOptions
if (this.ctx.config.browser.launchViewport.maximized) {
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
options.viewport = null
}
const context = await browser.newContext(options)
this.contexts.set(contextId, context)
return context
}
Expand Down
4 changes: 4 additions & 0 deletions packages/browser/src/node/providers/webdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class WebdriverBrowserProvider implements BrowserProvider {
capabilities: this.buildCapabilities(),
})

if (this.ctx.config.browser.launchViewport.maximized) {
await this.browser.maximizeWindow()
}

return this.browser
}

Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ export function resolveConfig(
resolved.browser.isolate ??= true
resolved.browser.fileParallelism
??= options.fileParallelism ?? mode !== 'benchmark'
// don't maximize it yet, only when the ui is enabled
resolved.browser.launchViewport = { maximized: false }
// disable in headless mode by default, and if CI is detected
resolved.browser.ui ??= resolved.browser.headless === true ? false : !isCI
if (resolved.browser.screenshotDirectory) {
Expand Down Expand Up @@ -723,6 +725,7 @@ export function resolveConfig(
// enable includeTaskLocation by default in UI mode
if (resolved.browser.enabled) {
if (resolved.browser.ui) {
resolved.browser.launchViewport.maximized = true
resolved.includeTaskLocation ??= true
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,7 @@ export interface ResolvedBrowserOptions extends BrowserConfigOptions {
width: number
height: number
}
launchViewport: {
maximized: boolean
}
}
Loading