Skip to content

Commit 07c4d72

Browse files
authored
Add ability to configure playwright in test file (#233)
* Add ability to configurate playwright in test file * Add ability to configure playwright in test file * Some changes in logic, extends global test object * Add ability to skip test inside jestPlaywrightConfig * Add TODO list
1 parent 61d6528 commit 07c4d72

File tree

5 files changed

+86
-25
lines changed

5 files changed

+86
-25
lines changed

extends.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* global jestPlaywright, browserName */
2+
const DEBUG_OPTIONS = {
3+
launchType: 'LAUNCH',
4+
launchOptions: {
5+
headless: false,
6+
devtools: true,
7+
},
8+
}
9+
10+
it.jestPlaywrightDebug = (...args) => {
11+
// TODO:
12+
// 1. Add input validation
13+
// 2. Unite jestPlaywrightDebug and jestPlaywrightConfig in one function
14+
// 3. Check out passing config to jestPlaywright._configSeparateEnv
15+
it(args[0], async () => {
16+
const { browser, context, page } = await jestPlaywright._configSeparateEnv(
17+
DEBUG_OPTIONS,
18+
)
19+
try {
20+
await args[1]({ browser, context, page })
21+
} finally {
22+
await browser.close()
23+
}
24+
})
25+
}
26+
27+
it.jestPlaywrightConfig = (playwrightOptions, ...args) => {
28+
if (playwrightOptions.browser && playwrightOptions.browser !== browserName) {
29+
it.skip(...args)
30+
} else {
31+
it(args[0], async () => {
32+
const {
33+
browser,
34+
context,
35+
page,
36+
} = await jestPlaywright._configSeparateEnv({
37+
...DEBUG_OPTIONS,
38+
playwrightOptions,
39+
})
40+
try {
41+
await args[1]({ browser, context, page })
42+
} finally {
43+
await browser.close()
44+
}
45+
})
46+
}
47+
}

jest-preset.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"runner": "jest-playwright-preset/runner.js",
66
"testRunner": "jest-circus/runner",
77
"setupFilesAfterEnv": [
8-
"expect-playwright"
8+
"expect-playwright",
9+
"jest-playwright-preset/extends.js"
910
]
1011
}

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PlaywrightEnvironment.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import {
2525
} from './utils'
2626
import { saveCoverageOnPage, saveCoverageToFile } from './coverage'
2727

28+
type ConfigParams = {
29+
browser: Browser | BrowserContext | null
30+
context: BrowserContext
31+
page: Page
32+
}
33+
2834
const handleError = (error: Error): void => {
2935
process.emit('uncaughtException', error)
3036
}
@@ -134,21 +140,15 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
134140
}
135141
this.global.browserName = browserType
136142
this.global.deviceName = deviceName
137-
this.global.browser =
138-
launchType === PERSISTENT
139-
? null
140-
: await getBrowserPerProcess(
141-
playwrightInstance,
142-
browserType,
143-
this._jestPlaywrightConfig,
144-
)
143+
const browserOrContext = await getBrowserPerProcess(
144+
playwrightInstance,
145+
browserType,
146+
this._jestPlaywrightConfig,
147+
)
148+
this.global.browser = launchType === PERSISTENT ? null : browserOrContext
145149
this.global.context =
146150
launchType === PERSISTENT
147-
? await getBrowserPerProcess(
148-
playwrightInstance,
149-
browserType,
150-
this._jestPlaywrightConfig,
151-
)
151+
? browserOrContext
152152
: await this.global.browser.newContext(contextOptions)
153153
if (collectCoverage) {
154154
;(this.global.context as BrowserContext).exposeFunction(
@@ -180,6 +180,30 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
180180
this.global.it = it
181181
this.global.test = test
182182
},
183+
_configSeparateEnv: async (
184+
config: JestPlaywrightConfig,
185+
): Promise<ConfigParams> => {
186+
const { contextOptions, launchType } = config
187+
const browserOrContext = await getBrowserPerProcess(
188+
playwrightInstance,
189+
browserType,
190+
{
191+
...this._jestPlaywrightConfig,
192+
...config,
193+
},
194+
)
195+
const browser = launchType === PERSISTENT ? null : browserOrContext
196+
const newContextOptions = getBrowserOptions(
197+
browserName,
198+
contextOptions,
199+
)
200+
const context =
201+
launchType === PERSISTENT
202+
? (browserOrContext as BrowserContext)
203+
: await (browser as Browser)!.newContext(newContextOptions)
204+
const page = await context!.newPage()
205+
return { browser, context, page }
206+
},
183207
resetPage: async (): Promise<void> => {
184208
const { context, page } = this.global
185209
if (page) {

src/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ export type DeviceType = CustomDeviceType | string | null
3636

3737
export type WsEndpointType = string | null
3838

39-
export type Packages = Partial<Record<BrowserType, BrowserType>>
40-
4139
export type GenericBrowser = PlaywrightBrowserType<
4240
WebKitBrowser | ChromiumBrowser | FirefoxBrowser
4341
>

0 commit comments

Comments
 (0)