From 76f41d9b69a8d97fbdfbcce9e1f676bed6c41102 Mon Sep 17 00:00:00 2001 From: mmarkelov Date: Sat, 1 Feb 2020 17:59:18 +0300 Subject: [PATCH] Add ability for configuration through jest-playwright.config.js file --- src/PlaywrightEnvironment.js | 14 ++++++------ src/constants.js | 7 ++++++ src/global.js | 11 +++++----- src/utils.js | 41 +++++++++++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/PlaywrightEnvironment.js b/src/PlaywrightEnvironment.js index f9f14b6b..42220b8c 100644 --- a/src/PlaywrightEnvironment.js +++ b/src/PlaywrightEnvironment.js @@ -2,6 +2,7 @@ import fs from 'fs' import NodeEnvironment from 'jest-environment-node' import playwright from 'playwright' import { WS_ENDPOINT_PATH } from './constants' +import { checkBrowserEnv, getBrowserType, readConfig } from "./utils"; const handleError = error => { process.emit('uncaughtException', error) @@ -9,7 +10,6 @@ const handleError = error => { class PlaywrightEnvironment extends NodeEnvironment { async teardown() { - console.log('Teardown Test Environment.'); await super.teardown() } @@ -18,11 +18,13 @@ class PlaywrightEnvironment extends NodeEnvironment { if (!wsEndpoint) { throw new Error('wsEndpoint not found') } - const browserType = process.env.BROWSER; - this.global.browser = await playwright[browserType].connect({ - browserWSEndpoint: wsEndpoint, - }); - this.global.context = await this.global.browser.newContext(); + const config = await readConfig(); + const browserType = getBrowserType(config); + checkBrowserEnv(browserType); + const { connect, context } = config; + const connectOptions = Object.assign({}, { browserWSEndpoint: wsEndpoint }, connect ); + this.global.browser = await playwright[browserType].connect(connectOptions); + this.global.context = await this.global.browser.newContext(context); this.global.page = await this.global.context.newPage(); this.global.page.on('pageerror', handleError) } diff --git a/src/constants.js b/src/constants.js index 1bb58a96..5d888faf 100644 --- a/src/constants.js +++ b/src/constants.js @@ -7,3 +7,10 @@ export const WS_ENDPOINT_PATH = path.join(DIR, 'wsEndpoint'); export const CHROMIUM = 'chromium'; export const FIREFOX = 'firefox'; export const WEBKIT = 'webkit'; + +export const DEFAULT_CONFIG = { + launchBrowserApp: { webSocket: true}, + context: {}, + browser: CHROMIUM, + exitOnPageError: true, +}; diff --git a/src/global.js b/src/global.js index a18f5df1..08b00d01 100644 --- a/src/global.js +++ b/src/global.js @@ -2,23 +2,22 @@ import fs from 'fs' import rimraf from 'rimraf' import playwright from 'playwright' import { DIR, WS_ENDPOINT_PATH } from './constants' -import checkBrowserEnv from "./utils"; +import { checkBrowserEnv, readConfig, getBrowserType } from "./utils"; let browser; export async function setup() { - const browserType = process.env.BROWSER; + const config = await readConfig(); + const browserType = getBrowserType(config); checkBrowserEnv(browserType); - console.log('Setup Playwright'); - const OPTIONS = { webSocket: true}; - browser = await playwright[browserType].launchBrowserApp(OPTIONS); + const { launchBrowserApp } = config; + browser = await playwright[browserType].launchBrowserApp(launchBrowserApp); // Instead, we expose the connection details via file system to be used in tests fs.mkdirSync(DIR, { recursive: true }); fs.writeFileSync(WS_ENDPOINT_PATH, browser.wsEndpoint()) } export async function teardown() { - console.log('Teardown Playwright'); await browser.close() rimraf.sync(DIR) } diff --git a/src/utils.js b/src/utils.js index e870fc1c..d6a36e2c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,9 +1,44 @@ -import { CHROMIUM, FIREFOX, WEBKIT } from './constants'; +import fs from 'fs'; +import path from 'path'; +import { promisify } from 'util'; +import { CHROMIUM, FIREFOX, WEBKIT, DEFAULT_CONFIG } from './constants'; -function checkBrowserEnv(param) { +const exists = promisify(fs.exists); + +export function checkBrowserEnv(param) { if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) { throw new Error(`Wrong browser type. Should be one of [${CHROMIUM}, ${FIREFOX}, ${WEBKIT}], but got ${param}`) } } -export default checkBrowserEnv +export function getBrowserType(config) { + const processBrowser = process.env.BROWSER; + if (processBrowser) { + return processBrowser + } else { + return config.browser || CHROMIUM + } +} + +export async function readConfig() { + const defaultConfig = DEFAULT_CONFIG; + + const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG; + const configPath = + process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js'; + const absConfigPath = path.resolve(process.cwd(), configPath) + const configExists = await exists(absConfigPath); + + if (hasCustomConfigPath && !configExists) { + throw new Error( + `Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${configPath}`, + ) + } + + if (!hasCustomConfigPath && !configExists) { + return defaultConfig + } + + const localConfig = await require(absConfigPath); + return Object.assign({}, defaultConfig, localConfig); +}