Unverified Commit 61432971 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Use new `browserWithResponse()` method in `empty-fallback-shells` tests (#80406)

In e2e tests, we sometimes want to assert on the response headers and on
the rendered HTML within the same test. For the former, we usually use
`next.fetch()`, but this doesn't allow us to use the Playwright browser
API to assert on the rendered HTML, which is available through
`next.browser()`. This is especially tricky when a PPR shell is resumed,
where asserting on the response text is error-prone, as the streamed-in
HTML may change the elements that we want to assert on.

To avoid the maintenance and runtime overhead of splitting the test into
two separate tests, we introduce a new `next.browserWithResponse()`
method that allows us to use the Playwright browser API while also
exposing the response to check the headers and other properties.

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 7bb72e50
import { getSentinelValue } from './sentinel'
export async function LastModified({ params }) { export async function LastModified({ params }) {
const { slug } = await params const { slug } = await params
return ( return (
<p data-testid={`page-${slug}`}> <p id="last-modified">
Page /{slug} last modified: {new Date().toISOString()} Page /{slug} last modified: {new Date().toISOString()} (
{getSentinelValue()})
</p> </p>
) )
} }
......
...@@ -7,8 +7,8 @@ export default async function Layout({ children }) { ...@@ -7,8 +7,8 @@ export default async function Layout({ children }) {
return ( return (
<html> <html>
<body> <body>
<div data-testid={`layout-${getSentinelValue()}`}> <div id="layout">
Layout: {new Date().toISOString()} Layout: {new Date().toISOString()} ({getSentinelValue()})
</div> </div>
<Suspense fallback={<p>Loading...</p>}>{children}</Suspense> <Suspense fallback={<p>Loading...</p>}>{children}</Suspense>
</body> </body>
......
...@@ -6,8 +6,8 @@ export default async function Layout({ children }) { ...@@ -6,8 +6,8 @@ export default async function Layout({ children }) {
return ( return (
<html> <html>
<body> <body>
<div data-testid={`layout-${getSentinelValue()}`}> <div id="layout">
Layout: {new Date().toISOString()} Layout: {new Date().toISOString()} ({getSentinelValue()})
</div> </div>
{children} {children}
</body> </body>
......
export default async function Page({ params }) { export default async function Page({ params }) {
const { slug } = await params const { slug } = await params
return <div data-testid={`hello-${slug}`}>Hello /{slug}</div> return <div id="slug">Hello /{slug}</div>
} }
export async function generateStaticParams() { export async function generateStaticParams() {
......
...@@ -13,6 +13,7 @@ import cheerio from 'cheerio' ...@@ -13,6 +13,7 @@ import cheerio from 'cheerio'
import { once } from 'events' import { once } from 'events'
import { Playwright } from 'next-webdriver' import { Playwright } from 'next-webdriver'
import escapeStringRegexp from 'escape-string-regexp' import escapeStringRegexp from 'escape-string-regexp'
import { Page, Response } from 'playwright'
type Event = 'stdout' | 'stderr' | 'error' | 'destroy' type Event = 'stdout' | 'stderr' | 'error' | 'destroy'
export type InstallCommand = export type InstallCommand =
...@@ -664,7 +665,7 @@ export class NextInstance { ...@@ -664,7 +665,7 @@ export class NextInstance {
} }
/** /**
* Create new browser window for the Next.js app. * Create a new browser window for the Next.js app.
*/ */
public async browser( public async browser(
...args: Parameters<OmitFirstArgument<typeof webdriver>> ...args: Parameters<OmitFirstArgument<typeof webdriver>>
...@@ -672,6 +673,49 @@ export class NextInstance { ...@@ -672,6 +673,49 @@ export class NextInstance {
return webdriver(this.url, ...args) return webdriver(this.url, ...args)
} }
/**
* Create a new browser window for the Next.js app, and also return the page's
* response.
*/
public async browserWithResponse(
...args: Parameters<OmitFirstArgument<typeof webdriver>>
): Promise<{ browser: Playwright; response: Response }> {
const [url, options = {}] = args
let resolveResponse: (response: Response) => void
const responsePromise = new Promise<Response>((resolve, reject) => {
const timer = setTimeout(() => {
reject(`Timed out waiting for the response of ${url}`)
}, 10_000)
resolveResponse = (response: Response) => {
clearTimeout(timer)
resolve(response)
}
})
const absoluteUrl = new URL(url, this.url).href
const [browser, response] = await Promise.all([
webdriver(this.url, url, {
...options,
beforePageLoad(page: Page) {
options.beforePageLoad?.(page)
page.on('response', async (response) => {
if (response.url() === absoluteUrl) {
resolveResponse(response)
}
})
},
}),
responsePromise,
])
return { browser, response }
}
/** /**
* Fetch the HTML for the provided page. This is a shortcut for `renderViaHTTP().then(html => cheerio.load(html))`. * Fetch the HTML for the provided page. This is a shortcut for `renderViaHTTP().then(html => cheerio.load(html))`.
*/ */
......
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