Skip to content
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

fix: improve capabilities matching #2464

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bidiMapper/BidiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type BidiServerEvent = {
};

export type MapperOptions = {
acceptInsecureCerts: boolean;
acceptInsecureCerts?: boolean;
unhandledPromptBehavior?: Session.UserPromptHandler;
};

Expand Down
52 changes: 38 additions & 14 deletions src/bidiMapper/modules/session/SessionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,40 @@ export class SessionProcessor {
return {ready: false, message: 'already connected'};
}

#getMapperOptions(capabilities: any): MapperOptions {
const acceptInsecureCerts =
capabilities?.alwaysMatch?.acceptInsecureCerts ?? false;
const unhandledPromptBehavior = this.#getUnhandledPromptBehavior(
capabilities?.alwaysMatch?.unhandledPromptBehavior
#mergeCapabilities(
capabilitiesRequest: Session.CapabilitiesRequest
): Session.CapabilityRequest {
// Roughly following https://www.w3.org/TR/webdriver2/#dfn-capabilities-processing.
// Validations should already be done by the parser.

const mergedCapabilities = [];

for (const first of capabilitiesRequest.firstMatch ?? [{}]) {
const result = {
...capabilitiesRequest.alwaysMatch,
};
for (const key of Object.keys(first)) {
if (result[key] !== undefined) {
throw new InvalidArgumentException(
`Capability ${key} in firstMatch is already defined in alwaysMatch`
);
}
result[key] = first[key];
}

mergedCapabilities.push(result);
}

const match =
mergedCapabilities.find((c) => c.browserName === 'chrome') ??
mergedCapabilities[0] ??
{};

match.unhandledPromptBehavior = this.#getUnhandledPromptBehavior(
match.unhandledPromptBehavior
);

return {acceptInsecureCerts, unhandledPromptBehavior};
return match;
}

#getUnhandledPromptBehavior(
Expand Down Expand Up @@ -93,21 +119,19 @@ export class SessionProcessor {
throw new Error('Session has been already created.');
}
this.#created = true;
// Since mapper exists, there is a session already.
// Still the mapper can handle capabilities for us.
// Currently, only Puppeteer calls here but, eventually, every client
// should delegrate capability processing here.
await this.#initConnection(this.#getMapperOptions(params.capabilities));

const matchedCapabitlites = this.#mergeCapabilities(params.capabilities);

await this.#initConnection(matchedCapabitlites);

const version =
await this.#browserCdpClient.sendCommand('Browser.getVersion');

return {
sessionId: 'unknown',
capabilities: {
acceptInsecureCerts: Boolean(
params.capabilities.alwaysMatch?.acceptInsecureCerts
),
...matchedCapabitlites,
acceptInsecureCerts: matchedCapabitlites.acceptInsecureCerts ?? false,
browserName: version.product,
browserVersion: version.revision,
platformName: '',
Expand Down
Loading