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.ui) {
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.ui) {
options.viewport = null
}
const context = await browser.newContext(options)
this.contexts.set(contextId, context)
return context
}
Expand Down
8 changes: 8 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,14 @@ export class WebdriverBrowserProvider implements BrowserProvider {
capabilities: this.buildCapabilities(),
})

if (this.ctx.config.browser.ui) {
await this.browser.maximizeWindow()
}
else {
const { width, height } = this.ctx.config.browser.viewport
await this.browser.setWindowSize(width, height)
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
}

return this.browser
}

Expand Down
4 changes: 2 additions & 2 deletions test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('running browser tests', async () => {
console.error(stderr)
})

expect(browserResultJson.testResults).toHaveLength(18)
expect(passedTests).toHaveLength(16)
expect(browserResultJson.testResults).toHaveLength(19)
expect(passedTests).toHaveLength(17)
expect(failedTests).toHaveLength(2)

expect(stderr).not.toContain('has been externalized for browser compatibility')
Expand Down
26 changes: 26 additions & 0 deletions test/browser/test/viewport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { page, server } from '@vitest/browser/context'
import { describe, expect, it } from 'vitest'

describe.skipIf(server.provider === 'preview')('viewport window has been properly initialized', () => {
it.skipIf(!page.config.browser.headless)('viewport has proper width and size', () => {
userquin marked this conversation as resolved.
Show resolved Hide resolved
const { width, height } = page.config.browser.viewport
const { width: actualWidth, height: actualHeight } = window.document.documentElement.getBoundingClientRect()

// eslint-disable-next-line no-console
console.log({ width, height, actualWidth, actualHeight })
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

expect(actualWidth).toBe(width)
expect(actualHeight).toBe(height)
})

it.skipIf(page.config.browser.headless)('window has been maximized', () => {
let topWindow = window
while (topWindow.parent && topWindow !== topWindow.parent) {
topWindow = topWindow.parent as unknown as any
}
// eslint-disable-next-line no-console
console.log({ availWidth: screen.availWidth, innerWidth: topWindow.innerWidth })

expect(screen.availWidth - topWindow.innerWidth === 0).toBe(true)
})
})
Loading