2022-02-11 13:22:58 +00:00
|
|
|
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 {
|
2023-01-19 19:37:07 +00:00
|
|
|
// Workround for https://github.com/nuxt/nuxt/issues/13441
|
2022-03-03 19:03:48 +00:00
|
|
|
// TODO: Remove when upstream issue resolved
|
|
|
|
playwright = await import(String('playwright'))
|
2022-02-11 13:22:58 +00:00
|
|
|
} 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()
|
|
|
|
}
|
2022-08-26 15:47:29 +00:00
|
|
|
return ctx.browser!
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|