diff --git a/extends.js b/extends.js new file mode 100644 index 00000000..b2004c86 --- /dev/null +++ b/extends.js @@ -0,0 +1,47 @@ +/* global jestPlaywright, browserName */ +const DEBUG_OPTIONS = { + launchType: 'LAUNCH', + launchOptions: { + headless: false, + devtools: true, + }, +} + +it.jestPlaywrightDebug = (...args) => { + // TODO: + // 1. Add input validation + // 2. Unite jestPlaywrightDebug and jestPlaywrightConfig in one function + // 3. Check out passing config to jestPlaywright._configSeparateEnv + it(args[0], async () => { + const { browser, context, page } = await jestPlaywright._configSeparateEnv( + DEBUG_OPTIONS, + ) + try { + await args[1]({ browser, context, page }) + } finally { + await browser.close() + } + }) +} + +it.jestPlaywrightConfig = (playwrightOptions, ...args) => { + if (playwrightOptions.browser && playwrightOptions.browser !== browserName) { + it.skip(...args) + } else { + it(args[0], async () => { + const { + browser, + context, + page, + } = await jestPlaywright._configSeparateEnv({ + ...DEBUG_OPTIONS, + playwrightOptions, + }) + try { + await args[1]({ browser, context, page }) + } finally { + await browser.close() + } + }) + } +} diff --git a/jest-preset.json b/jest-preset.json index b77507c1..84f1ebcb 100644 --- a/jest-preset.json +++ b/jest-preset.json @@ -5,6 +5,7 @@ "runner": "jest-playwright-preset/runner.js", "testRunner": "jest-circus/runner", "setupFilesAfterEnv": [ - "expect-playwright" + "expect-playwright", + "jest-playwright-preset/extends.js" ] } diff --git a/package-lock.json b/package-lock.json index d433a923..4f04fabd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1744,15 +1744,6 @@ } } }, - "@typescript-eslint/visitor-keys": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-3.6.1.tgz", - "integrity": "sha512-qC8Olwz5ZyMTZrh4Wl3K4U6tfms0R/mzU4/5W3XeUZptVraGVmbptJbn6h2Ey6Rb3hOs3zWoAUebZk8t47KGiQ==", - "dev": true, - "requires": { - "eslint-visitor-keys": "^1.1.0" - } - }, "abab": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.3.tgz", diff --git a/src/PlaywrightEnvironment.ts b/src/PlaywrightEnvironment.ts index b24178a4..20a81b54 100644 --- a/src/PlaywrightEnvironment.ts +++ b/src/PlaywrightEnvironment.ts @@ -25,6 +25,12 @@ import { } from './utils' import { saveCoverageOnPage, saveCoverageToFile } from './coverage' +type ConfigParams = { + browser: Browser | BrowserContext | null + context: BrowserContext + page: Page +} + const handleError = (error: Error): void => { process.emit('uncaughtException', error) } @@ -134,21 +140,15 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => { } this.global.browserName = browserType this.global.deviceName = deviceName - this.global.browser = - launchType === PERSISTENT - ? null - : await getBrowserPerProcess( - playwrightInstance, - browserType, - this._jestPlaywrightConfig, - ) + const browserOrContext = await getBrowserPerProcess( + playwrightInstance, + browserType, + this._jestPlaywrightConfig, + ) + this.global.browser = launchType === PERSISTENT ? null : browserOrContext this.global.context = launchType === PERSISTENT - ? await getBrowserPerProcess( - playwrightInstance, - browserType, - this._jestPlaywrightConfig, - ) + ? browserOrContext : await this.global.browser.newContext(contextOptions) if (collectCoverage) { ;(this.global.context as BrowserContext).exposeFunction( @@ -180,6 +180,30 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => { this.global.it = it this.global.test = test }, + _configSeparateEnv: async ( + config: JestPlaywrightConfig, + ): Promise => { + const { contextOptions, launchType } = config + const browserOrContext = await getBrowserPerProcess( + playwrightInstance, + browserType, + { + ...this._jestPlaywrightConfig, + ...config, + }, + ) + const browser = launchType === PERSISTENT ? null : browserOrContext + const newContextOptions = getBrowserOptions( + browserName, + contextOptions, + ) + const context = + launchType === PERSISTENT + ? (browserOrContext as BrowserContext) + : await (browser as Browser)!.newContext(newContextOptions) + const page = await context!.newPage() + return { browser, context, page } + }, resetPage: async (): Promise => { const { context, page } = this.global if (page) { diff --git a/src/types.d.ts b/src/types.d.ts index f761a452..1263c0f3 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -36,8 +36,6 @@ export type DeviceType = CustomDeviceType | string | null export type WsEndpointType = string | null -export type Packages = Partial> - export type GenericBrowser = PlaywrightBrowserType< WebKitBrowser | ChromiumBrowser | FirefoxBrowser >