Skip to content

Commit

Permalink
Add ability for configuration through jest-playwright.config.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Feb 1, 2020
1 parent 0b8e833 commit 76f41d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/PlaywrightEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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)
};

class PlaywrightEnvironment extends NodeEnvironment {
async teardown() {
console.log('Teardown Test Environment.');
await super.teardown()
}

Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
11 changes: 5 additions & 6 deletions src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
41 changes: 38 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 76f41d9

Please sign in to comment.