-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(refactor:test): unify environment variables for setting headles…
…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
Showing
5 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'`); | ||
} | ||
} |