diff --git a/src/core/browser.ts b/src/core/browser.ts index e45a2b300..45030be0c 100644 --- a/src/core/browser.ts +++ b/src/core/browser.ts @@ -1,4 +1,4 @@ -import type { Browser, BrowserContextOptions } from 'playwright-core' +import type { Browser, BrowserContextOptions, Page } from 'playwright-core' import { useTestContext } from './context' import { url } from './server' @@ -32,12 +32,36 @@ export async function getBrowser (): Promise { return ctx.browser! } -export async function createPage (path?: string, options?: BrowserContextOptions) { +type _GotoOptions = NonNullable[1]> +interface GotoOptions extends Omit<_GotoOptions, 'waitUntil'> { + waitUntil?: 'hydration' | 'route' | _GotoOptions['waitUntil'] +} + +interface NuxtPage extends Omit { + goto: (url: string, options?: GotoOptions) => Promise +} + +export async function createPage (path?: string, options?: BrowserContextOptions): Promise { const browser = await getBrowser() - const page = await browser.newPage(options) + const page = await browser.newPage(options) as unknown as NuxtPage + + const _goto = page.goto.bind(page) + page.goto = async (url, options) => { + const waitUntil = options?.waitUntil + if (waitUntil && ['hydration', 'route'].includes(waitUntil)) { + delete options.waitUntil + } + const res = await _goto(url, options) + if (waitUntil === 'hydration') { + await page.waitForFunction(() => window.useNuxtApp?.().isHydrating === false) + } else if (waitUntil === 'route') { + await page.waitForFunction((route) => window.useNuxtApp?.()._route.fullPath === route, path) + } + return res + } if (path) { - await page.goto(url(path)) + await page.goto(url(path), { waitUntil: 'hydration' }) } return page