Skip to content

Commit

Permalink
chore(refactor:test): unify environment variables for setting headles…
Browse files Browse the repository at this point in the history
…s modes (#24499)

- Three environment variables are now recognized:
    - `SELENIUM_HEADLESS`: Explicitly set headless mode for Selenium
    - `PLAYWRIGHT_HEADLESS`: Explicitly set headless mode for Playwright
    - `HEADLESS`: Set headless mode wherevere supported
- More consistent and lenient parsing of boolean value
    - `"true"` (case-insensitive) || `"1"` = `true`
- `"false"` (case-insensitive) || `"0"` || (any falsey value) = `false`
    - Any other value throws an error.
- Behavior should be retained for all previously recognized values
- Default is `false`, as previously
  • Loading branch information
legobeat authored Jun 7, 2024
1 parent ee3841d commit 55f1a8e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
5 changes: 3 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';
import dotenv from 'dotenv';
import { isHeadless } from './test/helpers/env';

dotenv.config({ path: './test/e2e/mmi/.env' });
const logOutputFolder = './public/playwright/playwright-reports';
Expand Down Expand Up @@ -42,8 +43,8 @@ const config: PlaywrightTestConfig = {
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on',
video: 'off',
// Run tests headless in local
headless: process.env.HEADLESS === 'true',
/* Run tests headless in local */
headless: isHeadless('PLAYWRIGHT'),
},

/* Configure projects for major browsers */
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/mmi/helpers/extension-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import { test as base, chromium } from '@playwright/test';

import { isHeadless } from '../../../helpers/env';

const extensionPath = path.join(__dirname, '../../../../dist/chrome');

export const test = base.extend({
Expand All @@ -10,7 +12,7 @@ export const test = base.extend({
headless: false,
args: [`--disable-extensions-except=${extensionPath}`],
};
if (process.env.HEADLESS === 'true') {
if (isHeadless('PLAYWRIGHT')) {
launchOptions.args.push('--headless=new');
}
const context = await chromium.launchPersistentContext('', launchOptions);
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/webdriver/chrome.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const { ThenableWebDriver } = require('selenium-webdriver'); // eslint-disable-line no-unused-vars -- this is imported for JSDoc
const { isHeadless } = require('../../helpers/env');

/**
* Proxy host to use for HTTPS requests
Expand Down Expand Up @@ -42,7 +43,7 @@ class ChromeDriver {
args.push('--disable-gpu');
}

if (process.env.SELENIUM_HEADLESS) {
if (isHeadless('SELENIUM')) {
// TODO: Remove notice and consider non-experimental when results are consistent
console.warn(
'*** Running e2e tests in headless mode is experimental and some tests are known to fail for unknown reasons',
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
} = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
const { retry } = require('../../../development/lib/retry');
const { isHeadless } = require('../../helpers/env');

/**
* The prefix for temporary Firefox profiles. All Firefox profiles used for e2e tests
Expand Down Expand Up @@ -58,7 +59,7 @@ class FirefoxDriver {
if (process.env.CI === 'true') {
options.setBinary('/opt/firefox/firefox');
}
if (process.env.SELENIUM_HEADLESS) {
if (isHeadless('SELENIUM')) {
// TODO: Remove notice and consider non-experimental when results are consistent
console.warn(
'*** Running e2e tests in headless mode is experimental and some tests are known to fail for unknown reasons',
Expand Down
36 changes: 36 additions & 0 deletions test/helpers/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { env } from 'process';

type HeadlessCapableServiceName = 'SELENIUM' | 'PLAYWRIGHT';

export function isHeadless(serviceName: HeadlessCapableServiceName): boolean {
if (serviceName) {
const serviceKey = `${serviceName}_HEADLESS`;
if (env[serviceKey]) {
return parseBoolean(env[serviceKey]);
}
}
return Boolean(env.HEADLESS) && parseBoolean(env.HEADLESS);
}

export function parseBoolean(value: undefined | string): boolean {
if (!value) {
return false;
}
if (typeof value === 'boolean') {
return value;
}
if (typeof value !== 'string') {
throw new Error(`Not-a-Boolean: '${value}'`);
}
switch (value.toLowerCase().trim()) {
case 'false':
case '0':
case '':
return false;
case 'true':
case '1':
return true;
default:
throw new Error(`Not-a-Boolean: '${value}'`);
}
}

0 comments on commit 55f1a8e

Please sign in to comment.