2021-09-13 14:36:25 +02:00
|
|
|
import { getFullUrl } from 'next-test-utils'
|
|
|
|
import os from 'os'
|
|
|
|
import { BrowserInterface } from './browsers/base'
|
|
|
|
|
2021-10-25 08:21:57 +02:00
|
|
|
if (!process.env.TEST_FILE_PATH) {
|
|
|
|
process.env.TEST_FILE_PATH = module.parent.filename
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:36:25 +02:00
|
|
|
let deviceIP: string
|
|
|
|
const isBrowserStack = !!process.env.BROWSERSTACK
|
|
|
|
;(global as any).browserName = process.env.BROWSER_NAME || 'chrome'
|
|
|
|
|
|
|
|
if (isBrowserStack) {
|
|
|
|
const nets = os.networkInterfaces()
|
|
|
|
for (const key of Object.keys(nets)) {
|
|
|
|
let done = false
|
|
|
|
|
|
|
|
for (const item of nets[key]) {
|
|
|
|
if (item.family === 'IPv4' && !item.internal) {
|
|
|
|
deviceIP = item.address
|
|
|
|
done = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (done) break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 01:29:55 +02:00
|
|
|
let browserQuit: () => Promise<void>
|
2021-09-13 14:36:25 +02:00
|
|
|
|
|
|
|
if (typeof afterAll === 'function') {
|
|
|
|
afterAll(async () => {
|
|
|
|
if (browserQuit) {
|
|
|
|
await browserQuit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:39:49 +02:00
|
|
|
export const USE_SELENIUM = Boolean(
|
|
|
|
process.env.LEGACY_SAFARI ||
|
|
|
|
process.env.BROWSER_NAME === 'internet explorer' ||
|
|
|
|
process.env.SKIP_LOCAL_SELENIUM_SERVER
|
|
|
|
)
|
|
|
|
|
2021-09-13 14:36:25 +02:00
|
|
|
/**
|
|
|
|
*
|
2022-05-20 21:41:11 +02:00
|
|
|
* @param appPortOrUrl can either be the port or the full URL
|
2021-09-13 14:36:25 +02:00
|
|
|
* @param url the path/query to append when using appPort
|
2022-02-15 19:53:45 +01:00
|
|
|
* @param options.waitHydration whether to wait for react hydration to finish
|
|
|
|
* @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
|
2021-09-13 14:36:25 +02:00
|
|
|
* @returns thenable browser instance
|
|
|
|
*/
|
|
|
|
export default async function webdriver(
|
|
|
|
appPortOrUrl: string | number,
|
|
|
|
url: string,
|
2022-02-15 19:53:45 +01:00
|
|
|
options?: {
|
|
|
|
waitHydration?: boolean
|
|
|
|
retryWaitHydration?: boolean
|
|
|
|
disableCache?: boolean
|
|
|
|
beforePageLoad?: (page: any) => void
|
2022-04-07 13:39:49 +02:00
|
|
|
locale?: string
|
2022-02-15 19:53:45 +01:00
|
|
|
}
|
2021-09-13 14:36:25 +02:00
|
|
|
): Promise<BrowserInterface> {
|
|
|
|
let CurrentInterface: typeof BrowserInterface
|
|
|
|
|
2022-02-15 19:53:45 +01:00
|
|
|
const defaultOptions = {
|
|
|
|
waitHydration: true,
|
|
|
|
retryWaitHydration: false,
|
|
|
|
disableCache: false,
|
|
|
|
}
|
|
|
|
options = Object.assign(defaultOptions, options)
|
2022-04-07 13:39:49 +02:00
|
|
|
const {
|
|
|
|
waitHydration,
|
|
|
|
retryWaitHydration,
|
|
|
|
disableCache,
|
|
|
|
beforePageLoad,
|
|
|
|
locale,
|
|
|
|
} = options
|
2022-02-15 19:53:45 +01:00
|
|
|
|
2021-09-13 14:36:25 +02:00
|
|
|
// we import only the needed interface
|
2022-04-07 13:39:49 +02:00
|
|
|
if (USE_SELENIUM) {
|
2022-08-17 01:29:55 +02:00
|
|
|
const { Selenium, quit } = await import('./browsers/selenium')
|
|
|
|
CurrentInterface = Selenium
|
|
|
|
browserQuit = quit
|
2021-09-13 14:36:25 +02:00
|
|
|
} else {
|
2022-08-17 01:29:55 +02:00
|
|
|
const { Playwright, quit } = await import('./browsers/playwright')
|
|
|
|
CurrentInterface = Playwright
|
|
|
|
browserQuit = quit
|
2021-09-13 14:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const browser = new CurrentInterface()
|
|
|
|
const browserName = process.env.BROWSER_NAME || 'chrome'
|
2022-04-07 13:39:49 +02:00
|
|
|
await browser.setup(browserName, locale)
|
2021-09-13 14:36:25 +02:00
|
|
|
;(global as any).browserName = browserName
|
|
|
|
|
|
|
|
const fullUrl = getFullUrl(
|
|
|
|
appPortOrUrl,
|
|
|
|
url,
|
|
|
|
isBrowserStack ? deviceIP : 'localhost'
|
|
|
|
)
|
|
|
|
|
|
|
|
console.log(`\n> Loading browser with ${fullUrl}\n`)
|
|
|
|
|
2022-02-15 19:53:45 +01:00
|
|
|
await browser.loadPage(fullUrl, { disableCache, beforePageLoad })
|
2021-09-13 14:36:25 +02:00
|
|
|
console.log(`\n> Loaded browser with ${fullUrl}\n`)
|
|
|
|
|
|
|
|
// Wait for application to hydrate
|
|
|
|
if (waitHydration) {
|
|
|
|
console.log(`\n> Waiting hydration for ${fullUrl}\n`)
|
|
|
|
|
|
|
|
const checkHydrated = async () => {
|
|
|
|
await browser.evalAsync(function () {
|
|
|
|
var callback = arguments[arguments.length - 1]
|
|
|
|
|
|
|
|
// if it's not a Next.js app return
|
|
|
|
if (
|
2022-09-06 19:29:09 +02:00
|
|
|
document.documentElement.innerHTML.indexOf('__NEXT_DATA__') === -1 &&
|
|
|
|
// @ts-ignore next exists on window if it's a Next.js page.
|
|
|
|
typeof (window as any).next?.version === 'undefined'
|
2021-09-13 14:36:25 +02:00
|
|
|
) {
|
|
|
|
console.log('Not a next.js page, resolving hydrate check')
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
|
2021-10-23 10:21:44 +02:00
|
|
|
// TODO: should we also ensure router.isReady is true
|
|
|
|
// by default before resolving?
|
2021-09-13 14:36:25 +02:00
|
|
|
if ((window as any).__NEXT_HYDRATED) {
|
|
|
|
console.log('Next.js page already hydrated')
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
var timeout = setTimeout(callback, 10 * 1000)
|
|
|
|
;(window as any).__NEXT_HYDRATED_CB = function () {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
console.log('Next.js hydrate callback fired')
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await checkHydrated()
|
|
|
|
} catch (err) {
|
|
|
|
if (retryWaitHydration) {
|
|
|
|
// re-try in case the page reloaded during check
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
|
|
await checkHydrated()
|
|
|
|
} else {
|
|
|
|
console.error('failed to check hydration')
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`\n> Hydration complete for ${fullUrl}\n`)
|
|
|
|
}
|
|
|
|
return browser
|
|
|
|
}
|