Unverified Commit 00ca8df2 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

test: add inline flight response reuse test (#34364)

To collect the requests starting from page load, we need to register event listeners before page loading (`page.goto` API invocation). By default webdriver will wait for hydration, so we need to register events inside `browser.loadPage` to ensure we can collect the requests before hydration is done.

### Changes
* Add `beforePageLoad` for next webdriver
* registry page `'request'` event before loading page
* update related tests for agruments change

### Usage

```js
browser = await webdriver(appPort, '/path', {
   waitHydration, // default true
   retryWaitHydration, // default false
   disableCache, // default false
   beforePageLoad, // default undefined
})

```
parent 01998188
...@@ -132,8 +132,9 @@ const runTests = (isDev) => { ...@@ -132,8 +132,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`, `${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -154,8 +155,9 @@ const runTests = (isDev) => { ...@@ -154,8 +155,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`, `${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -178,8 +180,9 @@ const runTests = (isDev) => { ...@@ -178,8 +180,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-_`, `${basePath}/gsp-blog/redirect-dest-_`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
...@@ -194,8 +197,9 @@ const runTests = (isDev) => { ...@@ -194,8 +197,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-_`, `${basePath}/gsp-blog/redirect-dest-_`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
...@@ -210,8 +214,9 @@ const runTests = (isDev) => { ...@@ -210,8 +214,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-_missing`, `${basePath}/gsp-blog/redirect-dest-_missing`,
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -231,8 +236,9 @@ const runTests = (isDev) => { ...@@ -231,8 +236,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gsp-blog/redirect-dest-external`, `${basePath}/gsp-blog/redirect-dest-external`,
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -248,8 +254,9 @@ const runTests = (isDev) => { ...@@ -248,8 +254,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gssp-blog/redirect-dest-external`, `${basePath}/gssp-blog/redirect-dest-external`,
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -279,8 +286,9 @@ const runTests = (isDev) => { ...@@ -279,8 +286,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/gssp-blog/redirect-dest-_gssp-blog_first`, `${basePath}/gssp-blog/redirect-dest-_gssp-blog_first`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gssp') await browser.waitForElementByCss('#gssp')
...@@ -294,7 +302,9 @@ const runTests = (isDev) => { ...@@ -294,7 +302,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSSP page is navigated to client-side (internal normal)', async () => { it('should apply redirect when GSSP page is navigated to client-side (internal normal)', async () => {
const browser = await webdriver(appPort, `${basePath}`, true, true) const browser = await webdriver(appPort, `${basePath}`, {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_another') window.next.router.push('/gssp-blog/redirect-dest-_another')
...@@ -307,7 +317,9 @@ const runTests = (isDev) => { ...@@ -307,7 +317,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSSP page is navigated to client-side (external)', async () => { it('should apply redirect when GSSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, `${basePath}`, true, true) const browser = await webdriver(appPort, `${basePath}`, {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first') window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first')
...@@ -324,7 +336,9 @@ const runTests = (isDev) => { ...@@ -324,7 +336,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSP page is navigated to client-side (internal)', async () => { it('should apply redirect when GSP page is navigated to client-side (internal)', async () => {
const browser = await webdriver(appPort, `${basePath}`, true, true) const browser = await webdriver(appPort, `${basePath}`, {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_another') window.next.router.push('/gsp-blog/redirect-dest-_another')
...@@ -337,7 +351,9 @@ const runTests = (isDev) => { ...@@ -337,7 +351,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSP page is navigated to client-side (external)', async () => { it('should apply redirect when GSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, `${basePath}`, true, true) const browser = await webdriver(appPort, `${basePath}`, {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first') window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first')
...@@ -357,8 +373,9 @@ const runTests = (isDev) => { ...@@ -357,8 +373,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/another?mark_as=root`, `${basePath}/another?mark_as=root`,
true, {
true retryWaitHydration: true,
}
) )
await browser.eval(`(function () { await browser.eval(`(function () {
...@@ -384,8 +401,9 @@ const runTests = (isDev) => { ...@@ -384,8 +401,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/another?mark_as=root`, `${basePath}/another?mark_as=root`,
true, {
true retryWaitHydration: true,
}
) )
await browser.eval(`(function () { await browser.eval(`(function () {
...@@ -411,8 +429,9 @@ const runTests = (isDev) => { ...@@ -411,8 +429,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/another?mark_as=root`, `${basePath}/another?mark_as=root`,
true, {
true retryWaitHydration: true,
}
) )
await browser.eval(`(function () { await browser.eval(`(function () {
...@@ -438,8 +457,9 @@ const runTests = (isDev) => { ...@@ -438,8 +457,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
`${basePath}/another?mark_as=root`, `${basePath}/another?mark_as=root`,
true, {
true retryWaitHydration: true,
}
) )
await browser.eval(`(function () { await browser.eval(`(function () {
......
...@@ -92,8 +92,9 @@ const runTests = (isDev) => { ...@@ -92,8 +92,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog/redirect-dest-_gsp-blog_first', '/gsp-blog/redirect-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -113,8 +114,9 @@ const runTests = (isDev) => { ...@@ -113,8 +114,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog-blocking/redirect-dest-_gsp-blog_first', '/gsp-blog-blocking/redirect-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -134,8 +136,9 @@ const runTests = (isDev) => { ...@@ -134,8 +136,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog-blocking/redirect-dest-_gsp-blog_first', '/gsp-blog-blocking/redirect-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -155,8 +158,9 @@ const runTests = (isDev) => { ...@@ -155,8 +158,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first', '/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -176,8 +180,9 @@ const runTests = (isDev) => { ...@@ -176,8 +180,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first', '/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -198,8 +203,9 @@ const runTests = (isDev) => { ...@@ -198,8 +203,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog/redirect-dest-_gsp-blog_first', '/gsp-blog/redirect-dest-_gsp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gsp') await browser.waitForElementByCss('#gsp')
...@@ -219,12 +225,9 @@ const runTests = (isDev) => { ...@@ -219,12 +225,9 @@ const runTests = (isDev) => {
} }
it('should apply redirect when fallback GSP page is visited directly (internal normal)', async () => { it('should apply redirect when fallback GSP page is visited directly (internal normal)', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/gsp-blog/redirect-dest-_', {
appPort, retryWaitHydration: true,
'/gsp-blog/redirect-dest-_', })
true,
true
)
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
...@@ -235,12 +238,9 @@ const runTests = (isDev) => { ...@@ -235,12 +238,9 @@ const runTests = (isDev) => {
if (!isDev) { if (!isDev) {
it('should apply redirect when fallback GSP page is visited directly (internal normal) 2nd visit', async () => { it('should apply redirect when fallback GSP page is visited directly (internal normal) 2nd visit', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/gsp-blog/redirect-dest-_', {
appPort, retryWaitHydration: true,
'/gsp-blog/redirect-dest-_', })
true,
true
)
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
...@@ -254,8 +254,9 @@ const runTests = (isDev) => { ...@@ -254,8 +254,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog/redirect-dest-_missing', '/gsp-blog/redirect-dest-_missing',
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -275,8 +276,9 @@ const runTests = (isDev) => { ...@@ -275,8 +276,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gsp-blog/redirect-dest-external', '/gsp-blog/redirect-dest-external',
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -292,8 +294,9 @@ const runTests = (isDev) => { ...@@ -292,8 +294,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gssp-blog/redirect-dest-external', '/gssp-blog/redirect-dest-external',
true, {
true retryWaitHydration: true,
}
) )
await check( await check(
...@@ -323,8 +326,9 @@ const runTests = (isDev) => { ...@@ -323,8 +326,9 @@ const runTests = (isDev) => {
const browser = await webdriver( const browser = await webdriver(
appPort, appPort,
'/gssp-blog/redirect-dest-_gssp-blog_first', '/gssp-blog/redirect-dest-_gssp-blog_first',
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('#gssp') await browser.waitForElementByCss('#gssp')
...@@ -338,7 +342,9 @@ const runTests = (isDev) => { ...@@ -338,7 +342,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSSP page is navigated to client-side (internal normal)', async () => { it('should apply redirect when GSSP page is navigated to client-side (internal normal)', async () => {
const browser = await webdriver(appPort, '/', true, true) const browser = await webdriver(appPort, '/', {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_another') window.next.router.push('/gssp-blog/redirect-dest-_another')
...@@ -351,7 +357,9 @@ const runTests = (isDev) => { ...@@ -351,7 +357,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSSP page is navigated to client-side (external)', async () => { it('should apply redirect when GSSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, '/', true, true) const browser = await webdriver(appPort, '/', {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first') window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first')
...@@ -368,7 +376,9 @@ const runTests = (isDev) => { ...@@ -368,7 +376,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSP page is navigated to client-side (internal)', async () => { it('should apply redirect when GSP page is navigated to client-side (internal)', async () => {
const browser = await webdriver(appPort, '/', true, true) const browser = await webdriver(appPort, '/', {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_another') window.next.router.push('/gsp-blog/redirect-dest-_another')
...@@ -381,7 +391,9 @@ const runTests = (isDev) => { ...@@ -381,7 +391,9 @@ const runTests = (isDev) => {
}) })
it('should apply redirect when GSP page is navigated to client-side (external)', async () => { it('should apply redirect when GSP page is navigated to client-side (external)', async () => {
const browser = await webdriver(appPort, '/', true, true) const browser = await webdriver(appPort, '/', {
retryWaitHydration: true,
})
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first') window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first')
...@@ -398,12 +410,9 @@ const runTests = (isDev) => { ...@@ -398,12 +410,9 @@ const runTests = (isDev) => {
}) })
it('should not replace history of the origin page when GSSP page is navigated to client-side (internal normal)', async () => { it('should not replace history of the origin page when GSSP page is navigated to client-side (internal normal)', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/another?mark_as=root', {
appPort, retryWaitHydration: true,
'/another?mark_as=root', })
true,
true
)
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/') window.next.router.push('/')
...@@ -425,12 +434,9 @@ const runTests = (isDev) => { ...@@ -425,12 +434,9 @@ const runTests = (isDev) => {
}) })
it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/another?mark_as=root', {
appPort, retryWaitHydration: true,
'/another?mark_as=root', })
true,
true
)
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/') window.next.router.push('/')
...@@ -452,12 +458,9 @@ const runTests = (isDev) => { ...@@ -452,12 +458,9 @@ const runTests = (isDev) => {
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/another?mark_as=root', {
appPort, retryWaitHydration: true,
'/another?mark_as=root', })
true,
true
)
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/') window.next.router.push('/')
...@@ -479,12 +482,9 @@ const runTests = (isDev) => { ...@@ -479,12 +482,9 @@ const runTests = (isDev) => {
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => {
const browser = await webdriver( const browser = await webdriver(appPort, '/another?mark_as=root', {
appPort, retryWaitHydration: true,
'/another?mark_as=root', })
true,
true
)
await browser.eval(`(function () { await browser.eval(`(function () {
window.next.router.push('/') window.next.router.push('/')
......
...@@ -2354,12 +2354,9 @@ export function runTests(ctx) { ...@@ -2354,12 +2354,9 @@ export function runTests(ctx) {
}) })
it('should render 404 for fallback page that returned 404 on client transition', async () => { it('should render 404 for fallback page that returned 404 on client transition', async () => {
const browser = await webdriver( const browser = await webdriver(ctx.appPort, `${ctx.basePath}/en`, {
ctx.appPort, retryWaitHydration: true,
`${ctx.basePath}/en`, })
true,
true
)
await browser.eval(`(function() { await browser.eval(`(function() {
next.router.push('/not-found/fallback/first') next.router.push('/not-found/fallback/first')
})()`) })()`)
...@@ -2395,8 +2392,9 @@ export function runTests(ctx) { ...@@ -2395,8 +2392,9 @@ export function runTests(ctx) {
const browser = await webdriver( const browser = await webdriver(
ctx.appPort, ctx.appPort,
`${ctx.basePath}/en/not-found/fallback/first`, `${ctx.basePath}/en/not-found/fallback/first`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('h1') await browser.waitForElementByCss('h1')
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
...@@ -2427,12 +2425,9 @@ export function runTests(ctx) { ...@@ -2427,12 +2425,9 @@ export function runTests(ctx) {
}) })
it('should render 404 for blocking fallback page that returned 404 on client transition', async () => { it('should render 404 for blocking fallback page that returned 404 on client transition', async () => {
const browser = await webdriver( const browser = await webdriver(ctx.appPort, `${ctx.basePath}/en`, {
ctx.appPort, retryWaitHydration: true,
`${ctx.basePath}/en`, })
true,
true
)
await browser.eval(`(function() { await browser.eval(`(function() {
next.router.push('/not-found/blocking-fallback/first') next.router.push('/not-found/blocking-fallback/first')
})()`) })()`)
...@@ -2468,8 +2463,9 @@ export function runTests(ctx) { ...@@ -2468,8 +2463,9 @@ export function runTests(ctx) {
const browser = await webdriver( const browser = await webdriver(
ctx.appPort, ctx.appPort,
`${ctx.basePath}/en/not-found/blocking-fallback/first`, `${ctx.basePath}/en/not-found/blocking-fallback/first`,
true, {
true retryWaitHydration: true,
}
) )
await browser.waitForElementByCss('h1') await browser.waitForElementByCss('h1')
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
......
...@@ -62,7 +62,9 @@ describe('react-virtualized wrapping next/image', () => { ...@@ -62,7 +62,9 @@ describe('react-virtualized wrapping next/image', () => {
it('should not cancel requests for images', async () => { it('should not cancel requests for images', async () => {
// TODO: this test doesnt work unless we can set `disableCache: true` // TODO: this test doesnt work unless we can set `disableCache: true`
let browser = await webdriver(appPort, '/', undefined, undefined, true) let browser = await webdriver(appPort, '/', {
disableCache: true,
})
expect(cancelCount).toBe(0) expect(cancelCount).toBe(0)
await browser.eval('window.scrollTo({ top: 100, behavior: "smooth" })') await browser.eval('window.scrollTo({ top: 100, behavior: "smooth" })')
await waitFor(100) await waitFor(100)
......
...@@ -156,11 +156,15 @@ describe('pnpm support', () => { ...@@ -156,11 +156,15 @@ describe('pnpm support', () => {
await renderViaHTTP(appPort, '/') await renderViaHTTP(appPort, '/')
browser = await webdriver(appPort, '/', false) browser = await webdriver(appPort, '/', {
waitHydration: false,
})
expect(await browser.waitForElementByCss('#world').text()).toBe('World') expect(await browser.waitForElementByCss('#world').text()).toBe('World')
await browser.close() await browser.close()
browser = await webdriver(appPort, '/about', false) browser = await webdriver(appPort, '/about', {
waitHydration: false,
})
expect(await browser.waitForElementByCss('#world').text()).toBe('World') expect(await browser.waitForElementByCss('#world').text()).toBe('World')
await browser.close() await browser.close()
} finally { } finally {
......
...@@ -30,6 +30,25 @@ export default function (context, { runtime, env }) { ...@@ -30,6 +30,25 @@ export default function (context, { runtime, env }) {
expect(homeHTML).toContain('foo.client') expect(homeHTML).toContain('foo.client')
}) })
it('should reuse the inline flight response without sending extra requests', async () => {
let hasFlightRequest = false
let requestsCount = 0
await webdriver(context.appPort, '/', {
beforePageLoad(page) {
page.on('request', (request) => {
requestsCount++
const url = request.url()
if (/__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
},
})
expect(requestsCount).toBeGreaterThan(0)
expect(hasFlightRequest).toBe(false)
})
it('should support multi-level server component imports', async () => { it('should support multi-level server component imports', async () => {
const html = await renderViaHTTP(context.appPort, '/multi') const html = await renderViaHTTP(context.appPort, '/multi')
expect(html).toContain('bar.server.js:') expect(html).toContain('bar.server.js:')
...@@ -127,14 +146,13 @@ export default function (context, { runtime, env }) { ...@@ -127,14 +146,13 @@ export default function (context, { runtime, env }) {
const content = 'custom-404-page' const content = 'custom-404-page'
const page404HTML = await renderViaHTTP(context.appPort, '/404') const page404HTML = await renderViaHTTP(context.appPort, '/404')
const pageUnknownHTML = await renderViaHTTP(context.appPort, '/no.where') const pageUnknownHTML = await renderViaHTTP(context.appPort, '/no.where')
let browser = await webdriver(context.appPort, '/404')
const hydrated404Content = await browser.waitForElementByCss(id).text()
browser = await webdriver(context.appPort, '/no.where')
const hydratedUnknownContent = await browser.waitForElementByCss(id).text()
const page404Browser = await webdriver(context.appPort, '/404') expect(hydrated404Content).toBe(content)
const pageUnknownBrowser = await webdriver(context.appPort, '/no.where') expect(hydratedUnknownContent).toBe(content)
expect(await page404Browser.waitForElementByCss(id).text()).toBe(content)
expect(await pageUnknownBrowser.waitForElementByCss(id).text()).toBe(
content
)
expect(getNodeBySelector(page404HTML, id).text()).toBe(content) expect(getNodeBySelector(page404HTML, id).text()).toBe(content)
expect(getNodeBySelector(pageUnknownHTML, id).text()).toBe(content) expect(getNodeBySelector(pageUnknownHTML, id).text()).toBe(content)
......
...@@ -72,7 +72,10 @@ export class BrowserInterface { ...@@ -72,7 +72,10 @@ export class BrowserInterface {
} }
on(event: Event, cb: (...args: any[]) => void) {} on(event: Event, cb: (...args: any[]) => void) {}
off(event: Event, cb: (...args: any[]) => void) {} off(event: Event, cb: (...args: any[]) => void) {}
async loadPage(url: string, { disableCache: boolean }): Promise<any> {} async loadPage(
url: string,
{ disableCache: boolean, beforePageLoad: Function }
): Promise<void> {}
async get(url: string): Promise<void> {} async get(url: string): Promise<void> {}
async getValue(): Promise<any> {} async getValue(): Promise<any> {}
......
...@@ -64,7 +64,10 @@ class Playwright extends BrowserInterface { ...@@ -64,7 +64,10 @@ class Playwright extends BrowserInterface {
return page.goto(url) as any return page.goto(url) as any
} }
async loadPage(url: string, opts?: { disableCache: boolean }) { async loadPage(
url: string,
opts?: { disableCache: boolean; beforePageLoad?: (...args: any[]) => void }
) {
if (this.activeTrace) { if (this.activeTrace) {
const traceDir = path.join(__dirname, '../../traces') const traceDir = path.join(__dirname, '../../traces')
const traceOutputPath = path.join( const traceOutputPath = path.join(
...@@ -136,6 +139,8 @@ class Playwright extends BrowserInterface { ...@@ -136,6 +139,8 @@ class Playwright extends BrowserInterface {
}) })
}) })
opts?.beforePageLoad?.(page)
if (tracePlaywright) { if (tracePlaywright) {
await context.tracing.start({ await context.tracing.start({
screenshots: true, screenshots: true,
......
...@@ -40,19 +40,33 @@ if (typeof afterAll === 'function') { ...@@ -40,19 +40,33 @@ if (typeof afterAll === 'function') {
* *
* @param appPort can either be the port or the full URL * @param appPort can either be the port or the full URL
* @param url the path/query to append when using appPort * @param url the path/query to append when using appPort
* @param waitHydration whether to wait for react hydration to finish * @param options.waitHydration whether to wait for react hydration to finish
* @param retryWaitHydration allow retrying hydration wait if reload occurs * @param options.retryWaitHydration allow retrying hydration wait if reload occurs
* @param options.disableCache disable cache for page load
* @param options.beforePageLoad the callback receiving page instance before loading page
* @returns thenable browser instance * @returns thenable browser instance
*/ */
export default async function webdriver( export default async function webdriver(
appPortOrUrl: string | number, appPortOrUrl: string | number,
url: string, url: string,
waitHydration = true, options?: {
retryWaitHydration = false, waitHydration?: boolean
disableCache = false retryWaitHydration?: boolean
disableCache?: boolean
beforePageLoad?: (page: any) => void
}
): Promise<BrowserInterface> { ): Promise<BrowserInterface> {
let CurrentInterface: typeof BrowserInterface let CurrentInterface: typeof BrowserInterface
const defaultOptions = {
waitHydration: true,
retryWaitHydration: false,
disableCache: false,
}
options = Object.assign(defaultOptions, options)
const { waitHydration, retryWaitHydration, disableCache, beforePageLoad } =
options
// we import only the needed interface // we import only the needed interface
if ( if (
process.env.LEGACY_SAFARI || process.env.LEGACY_SAFARI ||
...@@ -81,7 +95,7 @@ export default async function webdriver( ...@@ -81,7 +95,7 @@ export default async function webdriver(
console.log(`\n> Loading browser with ${fullUrl}\n`) console.log(`\n> Loading browser with ${fullUrl}\n`)
await browser.loadPage(fullUrl, { disableCache }) await browser.loadPage(fullUrl, { disableCache, beforePageLoad })
console.log(`\n> Loaded browser with ${fullUrl}\n`) console.log(`\n> Loaded browser with ${fullUrl}\n`)
// Wait for application to hydrate // Wait for application to hydrate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment