Repository navigation
fix(selenium-devtools): apply port, hostname and capture options in configure() #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@wdio/selenium-devtools': patch | ||
| --- | ||
|
|
||
| `DevTools.configure()` now applies `port`, `hostname`, `captureScreenshots`, `filmstrip` and `emitArtifactsManifest`. They were documented as configure options but silently ignored, so the defaults always applied. `port`/`hostname` passed after the backend start has begun are ignored with a warning, and an invalid port, a `null` value or a value of the wrong type (for example the string `'false'` for a boolean option) is refused with a warning rather than applied. A rerun keeps its parent's backend whatever they say. | ||
This file contains hidden or 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
118 changes: 118 additions & 0 deletions
118
packages/selenium-devtools/src/helpers/configure-options.ts
This file contains hidden or 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,118 @@ | ||
| import type { DevToolsOptions, ServerLock } from '../types.js' | ||
|
|
||
| type OptionKind = 'boolean' | 'choice' | 'port' | 'host' | ||
|
|
||
| // A Record over every option, so a key added to DevToolsOptions fails to compile here instead of being silently ignored. | ||
| const OPTION_KIND = { | ||
| openUi: 'boolean', | ||
| headless: 'boolean', | ||
| captureScreenshots: 'boolean', | ||
| captureAssertions: 'boolean', | ||
| filmstrip: 'boolean', | ||
| emitArtifactsManifest: 'boolean', | ||
| mode: 'choice', | ||
| traceFormat: 'choice', | ||
| traceGranularity: 'choice', | ||
| tracePolicy: 'choice', | ||
| screenshot: 'choice', | ||
| video: 'choice', | ||
| port: 'port', | ||
| hostname: 'host' | ||
| } as const satisfies Record< | ||
| Exclude<keyof DevToolsOptions, 'screencast' | 'rerunCommand'>, | ||
| OptionKind | ||
| > | ||
|
|
||
| const EXPECTED: Record<OptionKind, string> = { | ||
| boolean: 'a boolean', | ||
| choice: 'a non-empty string', | ||
| port: 'an integer between 1 and 65535', | ||
| host: 'a non-empty string' | ||
| } | ||
|
|
||
| type ConfigurableKey = keyof typeof OPTION_KIND | ||
|
|
||
| export type ConfigurableOptions = Pick<DevToolsOptions, ConfigurableKey> | ||
|
|
||
| export function serverLock(state: { | ||
| isReuse: boolean | ||
| backendStarting: boolean | ||
| }): ServerLock | undefined { | ||
| if (state.isReuse) { | ||
| return 'reuse' | ||
| } | ||
| return state.backendStarting ? 'started' : undefined | ||
| } | ||
|
|
||
| const CONFIGURABLE_KEYS = Object.keys(OPTION_KIND) as ConfigurableKey[] | ||
|
|
||
| function isServerKey(key: ConfigurableKey): boolean { | ||
| const kind: OptionKind = OPTION_KIND[key] | ||
| return kind === 'port' || kind === 'host' | ||
| } | ||
|
|
||
| // JS callers bypass the types (e.g. `headless: process.env.HEADLESS`), and the string 'false' is truthy. | ||
| function hasExpectedShape(kind: OptionKind, value: unknown): boolean { | ||
| switch (kind) { | ||
| case 'boolean': | ||
| return typeof value === 'boolean' | ||
| case 'port': | ||
| return ( | ||
| typeof value === 'number' && | ||
| Number.isInteger(value) && | ||
| value > 0 && | ||
| value < 65536 | ||
| ) | ||
| case 'choice': | ||
| case 'host': | ||
| return typeof value === 'string' && value !== '' | ||
| } | ||
| } | ||
|
|
||
| function describeValue(value: unknown): string { | ||
| return typeof value === 'string' ? `'${value}'` : String(value) | ||
| } | ||
|
|
||
| function copyOption<K extends ConfigurableKey>( | ||
| target: ConfigurableOptions, | ||
| opts: DevToolsOptions, | ||
| key: K | ||
| ): void { | ||
| target[key] = opts[key] | ||
| } | ||
|
|
||
| /** Returns one warning per refused option; a reused backend's port/hostname are skipped silently, as that is the rerun working. */ | ||
| export function applyConfigureOptions( | ||
| target: ConfigurableOptions, | ||
| opts: DevToolsOptions, | ||
| lock?: ServerLock | ||
| ): string[] { | ||
| const warnings: string[] = [] | ||
| const accepted: ConfigurableKey[] = [] | ||
| for (const key of CONFIGURABLE_KEYS) { | ||
| const value: unknown = opts[key] | ||
| if (value === undefined || value === null) { | ||
| continue | ||
| } | ||
| const kind: OptionKind = OPTION_KIND[key] | ||
| if (hasExpectedShape(kind, value)) { | ||
| accepted.push(key) | ||
| } else { | ||
| warnings.push( | ||
| `configure: ignoring ${key}: expected ${EXPECTED[kind]}, got ${describeValue(value)}` | ||
| ) | ||
| } | ||
| } | ||
| const server = accepted.filter(isServerKey) | ||
| if (server.length > 0 && lock === 'started') { | ||
| warnings.push( | ||
| `configure: ignoring ${server.join('/')}: the backend start has already begun` | ||
| ) | ||
| } | ||
| for (const key of accepted) { | ||
| if (!(lock && isServerKey(key))) { | ||
| copyOption(target, opts, key) | ||
| } | ||
| } | ||
| return warnings | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.