Skip to content

Commit

Permalink
feat(e2e): add 'route'/'hydration' options for page.goto() (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Mar 6, 2024
1 parent 2bf6b02 commit e8f1c00
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/core/browser.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -32,12 +32,36 @@ export async function getBrowser (): Promise<Browser> {
return ctx.browser!
}

export async function createPage (path?: string, options?: BrowserContextOptions) {
type _GotoOptions = NonNullable<Parameters<Page['goto']>[1]>
interface GotoOptions extends Omit<_GotoOptions, 'waitUntil'> {
waitUntil?: 'hydration' | 'route' | _GotoOptions['waitUntil']
}

interface NuxtPage extends Omit<Page, 'goto'> {
goto: (url: string, options?: GotoOptions) => Promise<void>
}

export async function createPage (path?: string, options?: BrowserContextOptions): Promise<NuxtPage> {
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
Expand Down

0 comments on commit e8f1c00

Please sign in to comment.