Skip to content

Commit 034d44e

Browse files
authored
fix(browser): scale the iframe for preview without the UI (#11856)
1 parent d951e63 commit 034d44e

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎packages/browser/src/client/orchestrator.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,9 @@ export class IframeOrchestrator {
446446
iframe.style.setProperty('width', 'var(--viewport-width)')
447447
iframe.style.setProperty('height', 'var(--viewport-height)')
448448

449-
// enable scaling only when using the UI, without UI the iframe fills the page
450-
if (config.browser.ui) {
449+
// scale the iframe to fit the UI; without the UI the provider resizes the page
450+
// to fit the iframe, except preview, which cannot resize the window
451+
if (config.browser.ui || getBrowserState().provider === 'preview') {
451452
if (config.browser.name !== 'firefox') {
452453
iframe.style.setProperty(
453454
'transform',
@@ -658,6 +659,10 @@ async function setIframeViewport(width: number, height: number) {
658659
document.body.style.setProperty('--viewport-width', `${width}px`)
659660
document.body.style.setProperty('--viewport-height', `${height}px`)
660661

662+
if (getBrowserState().provider === 'preview') {
663+
return
664+
}
665+
661666
// playwright emulates the viewport per page, so it keeps its size
662667
// between test files and the command round trip is only needed when
663668
// the size actually changes; other providers resize the window, which
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type { Browser } from 'playwright'
2+
import { preview } from '@vitest/browser-preview'
3+
import { chromium } from 'playwright'
4+
import { expect, onTestFinished, test } from 'vitest'
5+
import { runInlineTests } from '../../test-utils'
6+
7+
test(
8+
'preview provider scales the iframe without the browser UI',
9+
{ tags: ['browser'] },
10+
async () => {
11+
let browser: Browser | undefined
12+
onTestFinished(() => browser?.close())
13+
14+
const { stderr, testTree } = await runInlineTests(
15+
{
16+
'basic.test.ts': /* ts */ `
17+
import { expect, test } from 'vitest'
18+
import { page } from 'vitest/browser'
19+
20+
test('uses the configured viewport', () => {
21+
expect([innerWidth, innerHeight]).toEqual([414, 896])
22+
})
23+
24+
test('fits the iframe into the window', async () => {
25+
document.body.innerHTML = '<div style="position:absolute;top:800px">bottom</div>'
26+
await expect.element(page.getByText('bottom')).toBeInViewport()
27+
})
28+
29+
test('page.viewport resizes the iframe', async () => {
30+
await page.viewport(500, 400)
31+
expect([innerWidth, innerHeight]).toEqual([500, 400])
32+
})
33+
`,
34+
},
35+
{
36+
browser: {
37+
enabled: true,
38+
headless: false,
39+
ui: false,
40+
provider: preview(),
41+
instances: [{ browser: 'chromium' }],
42+
},
43+
$viteConfig: {
44+
plugins: [
45+
{
46+
name: 'open-preview-in-playwright',
47+
configureServer(server) {
48+
server.openBrowser = async () => {
49+
// preview restores `server.open` right after the call
50+
const url = String(server.config.server.open)
51+
browser = await chromium.launch()
52+
const page = await browser.newPage({ viewport: { width: 1280, height: 720 } })
53+
await page.goto(url)
54+
}
55+
},
56+
},
57+
],
58+
},
59+
},
60+
)
61+
62+
expect(stderr).toBe('')
63+
expect(testTree()).toMatchInlineSnapshot(`
64+
{
65+
"basic.test.ts": {
66+
"fits the iframe into the window": "passed",
67+
"page.viewport resizes the iframe": "passed",
68+
"uses the configured viewport": "passed",
69+
},
70+
}
71+
`)
72+
},
73+
)

0 commit comments

Comments
 (0)