Skip to content

Commit

Permalink
Allow configure launch browser type (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Jul 4, 2020
1 parent cb17290 commit 55570a2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
53 changes: 39 additions & 14 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type {
JestPlaywrightJestConfig,
SkipOption,
} from './types'
import { CHROMIUM, IMPORT_KIND_PLAYWRIGHT } from './constants'
import {
CHROMIUM,
IMPORT_KIND_PLAYWRIGHT,
LAUNCH,
PERSISTENT,
} from './constants'
import {
getBrowserOptions,
getBrowserType,
Expand All @@ -33,22 +38,31 @@ const getBrowserPerProcess = async (
playwrightInstance: GenericBrowser,
browserType: BrowserType,
config: JestPlaywrightConfig,
): Promise<Browser> => {
const { launchOptions, connectOptions } = config
): Promise<Browser | BrowserContext> => {
const { launchType, userDataDir, launchOptions, connectOptions } = config

if (connectOptions) {
const options = getBrowserOptions(browserType, connectOptions)
return playwrightInstance.connect(options)
} else {
if (launchType === LAUNCH || launchType === PERSISTENT) {
// https://github.com/mmarkelov/jest-playwright/issues/42#issuecomment-589170220
if (browserType !== CHROMIUM && launchOptions && launchOptions.args) {
launchOptions.args = launchOptions.args.filter(
(item: string) => item !== '--no-sandbox',
)
}

const options = getBrowserOptions(browserType, launchOptions)
return playwrightInstance.launch(options)

if (launchType === LAUNCH) {
return playwrightInstance.launch(options)
}

if (launchType === PERSISTENT) {
// @ts-ignore
return playwrightInstance.launchPersistentContext(userDataDir!, options)
}
}

const options = getBrowserOptions(browserType, connectOptions)
return playwrightInstance.connect(options)
}

export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
Expand Down Expand Up @@ -80,6 +94,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
exitOnPageError,
selectors,
collectCoverage,
launchType,
} = this._jestPlaywrightConfig
let contextOptions = getBrowserOptions(
browserName,
Expand Down Expand Up @@ -117,12 +132,22 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
}
this.global.browserName = browserType
this.global.deviceName = deviceName
this.global.browser = await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
this.global.context = await this.global.browser.newContext(contextOptions)
this.global.browser =
launchType === PERSISTENT
? null
: await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
this.global.context =
launchType === PERSISTENT
? await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
: await this.global.browser.newContext(contextOptions)
if (collectCoverage) {
;(this.global.context as BrowserContext).exposeFunction(
'reportCodeCoverage',
Expand Down
18 changes: 11 additions & 7 deletions src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Config as JestConfig } from '@jest/types'
import type {
BrowserType,
DeviceType,
WsEndpointType,
JestPlaywrightTest,
JestPlaywrightConfig,
} from './types'
Expand All @@ -23,14 +24,14 @@ import {
getPlaywrightInstance,
getBrowserOptions,
} from './utils'
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT } from './constants'
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT, SERVER } from './constants'
import { BrowserServer } from 'playwright-core'
import { setupCoverage, mergeCoverage } from './coverage'

const getBrowserTest = (
test: JestPlaywrightTest,
browser: BrowserType,
wsEndpoint: string,
wsEndpoint: WsEndpointType,
device: DeviceType,
): JestPlaywrightTest => {
const { displayName } = test.context.config
Expand Down Expand Up @@ -71,19 +72,22 @@ class PlaywrightRunner extends JestRunner {
}

async getTests(tests: Test[], config: JestPlaywrightConfig): Promise<Test[]> {
const { browsers, devices, launchOptions } = config
const { browsers, devices, launchType, launchOptions } = config
const pwTests: Test[] = []
for (const test of tests) {
for (const browser of browsers) {
checkBrowserEnv(browser)
const { devices: availableDevices, instance } = getPlaywrightInstance(
browser,
)
if (!this.browser2Server[browser]) {
const options = getBrowserOptions(browser, launchOptions)
this.browser2Server[browser] = await instance.launchServer(options)
let wsEndpoint: WsEndpointType = null
if (launchType === SERVER) {
if (!this.browser2Server[browser]) {
const options = getBrowserOptions(browser, launchOptions)
this.browser2Server[browser] = await instance.launchServer(options)
}
wsEndpoint = this.browser2Server[browser]!.wsEndpoint()
}
const wsEndpoint = this.browser2Server[browser]!.wsEndpoint()

if (devices && devices.length) {
devices.forEach((device: DeviceType) => {
Expand Down
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ export const CHROMIUM = 'chromium'
export const FIREFOX = 'firefox'
export const WEBKIT = 'webkit'

export const LAUNCH = 'LAUNCH'
export const PERSISTENT = 'PERSISTENT'
export const SERVER = 'SERVER'

export const DEFAULT_CONFIG: JestPlaywrightConfig = {
launchType: SERVER,
launchOptions: {},
contextOptions: {},
browsers: [CHROMIUM],
devices: [],
exitOnPageError: true,
collectCoverage: false,
}
Expand Down
18 changes: 16 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import type { Config as JestConfig } from '@jest/types'
import type { Context } from 'jest-runner/build/types'
import type { Test } from 'jest-runner'
import type { JestProcessManagerOptions } from 'jest-process-manager'
import { CHROMIUM, FIREFOX, IMPORT_KIND_PLAYWRIGHT, WEBKIT } from './constants'
import {
CHROMIUM,
FIREFOX,
IMPORT_KIND_PLAYWRIGHT,
LAUNCH,
PERSISTENT,
SERVER,
WEBKIT,
} from './constants'

export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT

Expand All @@ -26,6 +34,8 @@ export type CustomDeviceType = BrowserContextOptions & {

export type DeviceType = CustomDeviceType | string | null

export type WsEndpointType = string | null

export type Packages = Partial<Record<BrowserType, BrowserType>>

export type GenericBrowser = PlaywrightBrowserType<
Expand All @@ -45,14 +55,18 @@ export interface Playwright {
devices: typeof devices
}

type LaunchType = typeof LAUNCH | typeof SERVER | typeof PERSISTENT

type Options<T> = T & Partial<Record<BrowserType, T>>

type ConnectOptions = Parameters<GenericBrowser['connect']>[0]

export interface JestPlaywrightConfig {
launchType?: LaunchType
launchOptions?: Options<LaunchOptions>
connectOptions?: Options<ConnectOptions>
contextOptions?: Options<BrowserContextOptions>
userDataDir?: string
exitOnPageError: boolean
browsers: BrowserType[]
devices?: (string | CustomDeviceType)[]
Expand All @@ -63,7 +77,7 @@ export interface JestPlaywrightConfig {

export interface JestPlaywrightJestConfig extends JestConfig.ProjectConfig {
browserName: BrowserType
wsEndpoint: string
wsEndpoint: WsEndpointType
device: DeviceType
}

Expand Down

0 comments on commit 55570a2

Please sign in to comment.