mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
64b0986c05
Enables AI agents to programmatically access the runtime metadata of the current page. At the moment, we only include the segment trie details, but this endpoint will be open to extension. Like `get_errors`, we support collecting information from multiple tabs.
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { chromium, firefox, webkit, type Browser } from 'playwright'
|
|
|
|
function getFullUrl(appPortOrUrl: string | number, url: string): string {
|
|
const appUrl =
|
|
typeof appPortOrUrl === 'string'
|
|
? appPortOrUrl
|
|
: `http://localhost:${appPortOrUrl}`
|
|
return url.startsWith('/') ? `${appUrl}${url}` : url
|
|
}
|
|
|
|
/**
|
|
* Minimal standalone browser session launcher for testing multiple concurrent browser tabs.
|
|
* The standard test harness (next.browser) uses a singleton browser instance which doesn't
|
|
* support concurrent tabs needed for testing errors across multiple browser sessions.
|
|
*/
|
|
export async function launchStandaloneSession(
|
|
appPortOrUrl: string | number,
|
|
url: string
|
|
) {
|
|
const headless = !!process.env.HEADLESS
|
|
const browserName = (process.env.BROWSER_NAME || 'chrome').toLowerCase()
|
|
let browser: Browser
|
|
if (browserName === 'safari') {
|
|
browser = await webkit.launch({ headless })
|
|
} else if (browserName === 'firefox') {
|
|
browser = await firefox.launch({ headless })
|
|
} else {
|
|
browser = await chromium.launch({ headless })
|
|
}
|
|
const context = await browser.newContext()
|
|
const page = await context.newPage()
|
|
const fullUrl = getFullUrl(appPortOrUrl, url)
|
|
await page.goto(fullUrl, { waitUntil: 'load' })
|
|
return {
|
|
page,
|
|
close: async () => {
|
|
await page.close().catch(() => {})
|
|
await context.close().catch(() => {})
|
|
await browser.close().catch(() => {})
|
|
},
|
|
}
|
|
}
|