Skip to content

Commit

Permalink
refactor webdriver, add options
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 15, 2022
1 parent 7a844f7 commit 0e43682
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 50 deletions.
61 changes: 39 additions & 22 deletions test/integration/gssp-redirect-base-path/test/index.test.js
Expand Up @@ -132,8 +132,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.waitForElementByCss('#gsp')
Expand All @@ -154,8 +155,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.waitForElementByCss('#gsp')
Expand All @@ -178,8 +180,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-_`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.waitForElementByCss('#index')
Expand All @@ -194,8 +197,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-_`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.waitForElementByCss('#index')
Expand All @@ -210,8 +214,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-_missing`,
true,
true
{
retryWaitHydration: true,
}
)

await check(
Expand All @@ -231,8 +236,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-external`,
true,
true
{
retryWaitHydration: true,
}
)

await check(
Expand All @@ -248,8 +254,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gssp-blog/redirect-dest-external`,
true,
true
{
retryWaitHydration: true,
}
)

await check(
Expand Down Expand Up @@ -279,8 +286,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/gssp-blog/redirect-dest-_gssp-blog_first`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.waitForElementByCss('#gssp')
Expand All @@ -294,7 +302,9 @@ const runTests = (isDev) => {
})

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 () {
window.next.router.push('/gssp-blog/redirect-dest-_another')
Expand All @@ -307,7 +317,9 @@ const runTests = (isDev) => {
})

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 () {
window.next.router.push('/gssp-blog/redirect-dest-_gssp-blog_first')
Expand All @@ -324,7 +336,9 @@ const runTests = (isDev) => {
})

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 () {
window.next.router.push('/gsp-blog/redirect-dest-_another')
Expand All @@ -337,7 +351,9 @@ const runTests = (isDev) => {
})

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 () {
window.next.router.push('/gsp-blog/redirect-dest-_gsp-blog_first')
Expand All @@ -357,8 +373,9 @@ const runTests = (isDev) => {
const browser = await webdriver(
appPort,
`${basePath}/another?mark_as=root`,
true,
true
{
retryWaitHydration: true,
}
)

await browser.eval(`(function () {
Expand Down
28 changes: 12 additions & 16 deletions test/integration/i18n-support/test/shared.js
Expand Up @@ -2354,12 +2354,9 @@ export function runTests(ctx) {
})

it('should render 404 for fallback page that returned 404 on client transition', async () => {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/en`,
true,
true
)
const browser = await webdriver(ctx.appPort, `${ctx.basePath}/en`, {
retryWaitHydration: true,
})
await browser.eval(`(function() {
next.router.push('/not-found/fallback/first')
})()`)
Expand Down Expand Up @@ -2395,8 +2392,9 @@ export function runTests(ctx) {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/en/not-found/fallback/first`,
true,
true
{
retryWaitHydration: true,
}
)
await browser.waitForElementByCss('h1')
await browser.eval('window.beforeNav = 1')
Expand Down Expand Up @@ -2427,12 +2425,9 @@ export function runTests(ctx) {
})

it('should render 404 for blocking fallback page that returned 404 on client transition', async () => {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/en`,
true,
true
)
const browser = await webdriver(ctx.appPort, `${ctx.basePath}/en`, {
retryWaitHydration: true,
})
await browser.eval(`(function() {
next.router.push('/not-found/blocking-fallback/first')
})()`)
Expand Down Expand Up @@ -2468,8 +2463,9 @@ export function runTests(ctx) {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/en/not-found/blocking-fallback/first`,
true,
true
{
retryWaitHydration: true,
}
)
await browser.waitForElementByCss('h1')
await browser.eval('window.beforeNav = 1')
Expand Down
Expand Up @@ -62,7 +62,9 @@ describe('react-virtualized wrapping next/image', () => {

it('should not cancel requests for images', async () => {
// 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)
await browser.eval('window.scrollTo({ top: 100, behavior: "smooth" })')
await waitFor(100)
Expand Down
8 changes: 6 additions & 2 deletions test/integration/pnpm-support/test/index.test.js
Expand Up @@ -156,11 +156,15 @@ describe('pnpm support', () => {

await renderViaHTTP(appPort, '/')

browser = await webdriver(appPort, '/', false)
browser = await webdriver(appPort, '/', {
waitHydration: false,
})
expect(await browser.waitForElementByCss('#world').text()).toBe('World')
await browser.close()

browser = await webdriver(appPort, '/about', false)
browser = await webdriver(appPort, '/about', {
waitHydration: false,
})
expect(await browser.waitForElementByCss('#world').text()).toBe('World')
await browser.close()
} finally {
Expand Down
Expand Up @@ -32,12 +32,15 @@ export default function (context, { runtime, env }) {

it('should reuse the inline flight response without sending extra requests', async () => {
let hasFlightRequest = false
const browser = await webdriver(context.appPort, '/')
browser.on('request', (request) => {
const url = request.url()
if (/__flight__=1/.test(url)) {
hasFlightRequest = true
}
await webdriver(context.appPort, '/', true, false, false, {
beforePageLoad(_browser) {
_browser.on('request', (request) => {
const url = request.url()
if (/__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
},
})
expect(hasFlightRequest).toBe(false)
})
Expand Down
21 changes: 18 additions & 3 deletions test/lib/next-webdriver.ts
Expand Up @@ -42,17 +42,31 @@ if (typeof afterAll === 'function') {
* @param url the path/query to append when using appPort
* @param waitHydration whether to wait for react hydration to finish
* @param retryWaitHydration allow retrying hydration wait if reload occurs
* @param disableCache disable cache for page load
* @param
* @returns thenable browser instance
*/
export default async function webdriver(
appPortOrUrl: string | number,
url: string,
waitHydration = true,
retryWaitHydration = false,
disableCache = false
opts?: {
waitHydration?: boolean
retryWaitHydration?: boolean
disableCache?: boolean
beforePageLoad?: (browser: any) => void
}
): Promise<BrowserInterface> {
let CurrentInterface: typeof BrowserInterface

const defaultOptions = {
waitHydration: true,
retryWaitHydration: false,
disableCache: false,
}
opts = Object.assign(opts, defaultOptions)
const { waitHydration, retryWaitHydration, disableCache, beforePageLoad } =
opts

// we import only the needed interface
if (
process.env.LEGACY_SAFARI ||
Expand Down Expand Up @@ -81,6 +95,7 @@ export default async function webdriver(

console.log(`\n> Loading browser with ${fullUrl}\n`)

await beforePageLoad?.(browser)
await browser.loadPage(fullUrl, { disableCache })
console.log(`\n> Loaded browser with ${fullUrl}\n`)

Expand Down

0 comments on commit 0e43682

Please sign in to comment.