mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-16 02:44:51 +00:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
|
import type { Browser, BrowserContextOptions } from 'playwright'
|
||
|
import { useTestContext } from './context'
|
||
|
import { url } from './server'
|
||
|
|
||
|
export async function createBrowser () {
|
||
|
const ctx = useTestContext()
|
||
|
|
||
|
let playwright: typeof import('playwright')
|
||
|
try {
|
||
|
playwright = await import('playwright')
|
||
|
} catch {
|
||
|
/* istanbul ignore next */
|
||
|
throw new Error(`
|
||
|
The dependency 'playwright' not found.
|
||
|
Please run 'yarn add --dev playwright' or 'npm install --save-dev playwright'
|
||
|
`)
|
||
|
}
|
||
|
|
||
|
const { type, launch } = ctx.options.browserOptions
|
||
|
if (!playwright[type]) {
|
||
|
throw new Error(`Invalid browser '${type}'`)
|
||
|
}
|
||
|
|
||
|
ctx.browser = await playwright[type].launch(launch)
|
||
|
}
|
||
|
|
||
|
export async function getBrowser (): Promise<Browser> {
|
||
|
const ctx = useTestContext()
|
||
|
if (!ctx.browser) {
|
||
|
await createBrowser()
|
||
|
}
|
||
|
return ctx.browser
|
||
|
}
|
||
|
|
||
|
export async function createPage (path?: string, options?: BrowserContextOptions) {
|
||
|
const browser = await getBrowser()
|
||
|
const page = await browser.newPage(options)
|
||
|
|
||
|
if (path) {
|
||
|
await page.goto(url(path))
|
||
|
}
|
||
|
|
||
|
return page
|
||
|
}
|