mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
### What? Migrate remaining direct `next-webdriver` test callers that have a `NextInstance` to `next.browser()`, and expose the shared `Playwright` browser type from `e2e-utils`. ### Why? `NextInstance.browser` should be the supported browser-opening interface for test fixtures, with `next-webdriver` kept as the private implementation detail. ### How? Updated affected development, e2e, and production tests to call `next.browser()` directly, passing `baseUrl` where tests intentionally target a manually spawned or proxied server. Shared helpers now receive browser callbacks from the test context, and browser types import `Playwright` from `e2e-utils` instead of deriving from `next.browser` or importing from private paths. <!-- NEXT_JS_LLM_PR -->
157 lines
3.8 KiB
TypeScript
157 lines
3.8 KiB
TypeScript
import type * as Playwright from 'playwright'
|
|
import type { Server } from 'http'
|
|
import { createRouterAct } from 'router-act'
|
|
import { findPort } from 'next-test-utils'
|
|
import { isNextStart, nextTestSetup } from 'e2e-utils'
|
|
import { join } from 'path'
|
|
import { createExportServer } from './server.mjs'
|
|
|
|
describe('segment cache (output: "export")', () => {
|
|
if (!isNextStart) {
|
|
test('build test should not run during dev test run', () => {})
|
|
return
|
|
}
|
|
|
|
const { next } = nextTestSetup({
|
|
files: __dirname,
|
|
skipStart: true,
|
|
disableAutoSkewProtection: true,
|
|
})
|
|
|
|
// To debug these tests locally, first build the app, then run:
|
|
//
|
|
// node start.mjs
|
|
//
|
|
// This will serve the static `/out` directory, and also set up a server-side
|
|
// rewrite, which some of the tests below rely on.
|
|
|
|
let port: number
|
|
let server: Server
|
|
|
|
beforeAll(async () => {
|
|
await next.build()
|
|
port = await findPort()
|
|
server = createExportServer(join(next.testDir, 'out'))
|
|
server.listen(port)
|
|
})
|
|
|
|
afterAll(() => {
|
|
server?.close()
|
|
})
|
|
|
|
it('basic prefetch in output: "export" mode', async () => {
|
|
let act
|
|
const browser = await next.browser('/', {
|
|
baseUrl: port,
|
|
beforePageLoad(p: Playwright.Page) {
|
|
act = createRouterAct(p)
|
|
},
|
|
})
|
|
|
|
await act(
|
|
async () => {
|
|
const checkbox = await browser.elementByCss(
|
|
'[data-link-accordion="/target-page"]'
|
|
)
|
|
await checkbox.click()
|
|
},
|
|
{
|
|
includes: 'Target page',
|
|
}
|
|
)
|
|
|
|
await act(
|
|
async () => {
|
|
const link = await browser.elementByCss('a[href="/target-page"]')
|
|
await link.click()
|
|
|
|
const div = await browser.elementById('target-page')
|
|
expect(await div.text()).toBe('Target page')
|
|
|
|
await browser.elementByCss('a[href="/"]')
|
|
},
|
|
{
|
|
includes: 'Demonstrates that per-segment prefetching works',
|
|
}
|
|
)
|
|
})
|
|
|
|
it('prefetch a link to a page that is rewritten server side', async () => {
|
|
let act
|
|
const browser = await next.browser('/', {
|
|
baseUrl: port,
|
|
beforePageLoad(p: Playwright.Page) {
|
|
act = createRouterAct(p)
|
|
},
|
|
})
|
|
|
|
await act(
|
|
async () => {
|
|
const checkbox = await browser.elementByCss(
|
|
'[data-link-accordion="/rewrite-to-target-page"]'
|
|
)
|
|
await checkbox.click()
|
|
},
|
|
{
|
|
includes: 'Target page',
|
|
}
|
|
)
|
|
|
|
await act(
|
|
async () => {
|
|
const link = await browser.elementByCss(
|
|
'a[href="/rewrite-to-target-page"]'
|
|
)
|
|
await link.click()
|
|
|
|
const div = await browser.elementById('target-page')
|
|
expect(await div.text()).toBe('Target page')
|
|
|
|
await browser.elementByCss('a[href="/"]')
|
|
},
|
|
{
|
|
includes: 'Demonstrates that per-segment prefetching works',
|
|
}
|
|
)
|
|
})
|
|
|
|
it('prefetch a link to a page that is redirected server side', async () => {
|
|
let act
|
|
const browser = await next.browser('/', {
|
|
baseUrl: port,
|
|
beforePageLoad(p: Playwright.Page) {
|
|
act = createRouterAct(p)
|
|
},
|
|
})
|
|
|
|
await act(
|
|
async () => {
|
|
const checkbox = await browser.elementByCss(
|
|
'[data-link-accordion="/redirect-to-target-page"]'
|
|
)
|
|
await checkbox.click()
|
|
},
|
|
{
|
|
includes: 'Target page',
|
|
}
|
|
)
|
|
|
|
await act(
|
|
async () => {
|
|
const link = await browser.elementByCss(
|
|
'a[href="/redirect-to-target-page"]'
|
|
)
|
|
await link.click()
|
|
|
|
const div = await browser.elementById('target-page')
|
|
expect(await div.text()).toBe('Target page')
|
|
|
|
await browser.elementByCss('a[href="/"]')
|
|
},
|
|
{
|
|
includes: 'Demonstrates that per-segment prefetching works',
|
|
}
|
|
)
|
|
})
|
|
})
|