Skip to content

Commit

Permalink
tune
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jul 20, 2024
1 parent db7a643 commit 78b83b8
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/chromedriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,49 +513,49 @@ export class Chromedriver extends events.EventEmitter {

/**
* Sync the WebDriver protocol if current on going protocol is W3C or MJSONWP.
* Does nothing if this.driverVersion was null.
* Does nothing if this.driverVersion is null.
*
* @returns {typeof PROTOCOLS[keyof typeof PROTOCOLS]}
*/
syncProtocol() {
if (!this.driverVersion) {
// Keep the default protocol if the driverVersion was unsure.
return;
return this.desiredProtocol;
}

this.desiredProtocol = PROTOCOLS.MJSONWP;
const coercedVersion = semver.coerce(this.driverVersion);
if (!coercedVersion || coercedVersion.major < MIN_CD_VERSION_WITH_W3C_SUPPORT) {
this.log.debug(
this.log.info(
`The ChromeDriver v. ${this.driverVersion} does not fully support ${PROTOCOLS.W3C} protocol. ` +
`Defaulting to ${PROTOCOLS.MJSONWP}`,
);
return;
return this.desiredProtocol;
}

// Check only chromeOptions for now.
const chromeOptions = getCapValue(this.capabilities, 'chromeOptions', {});
if (chromeOptions.w3c === false) {
this.log.info(
`The ChromeDriver v. ${this.driverVersion} supports ${PROTOCOLS.W3C} protocol, ` +
`but ${PROTOCOLS.MJSONWP} one has been explicitly requested`,
);
return;
return this.desiredProtocol;
}

this.desiredProtocol = PROTOCOLS.W3C;
this.log.info(`Set ChromeDriver communication protocol to ${PROTOCOLS.W3C}`);
// given caps might not be properly prefixed
// so we try to fix them in order to properly init
// the new W3C session
this.capabilities = toW3cCapNames(this.capabilities);
return this.desiredProtocol;
}

/**
* Sync the protocol by reading the given output
*
* @param {string} line The output of WebDriver process start
* @param {string} line The output of ChromeDriver process
* @returns {typeof PROTOCOLS[keyof typeof PROTOCOLS] | null}
*/
detectWebDriverProtocol(line) {
if (this.driverVersion) {
this.syncProtocol();
return;
return this.syncProtocol();
}

// also print chromedriver version to logs
Expand All @@ -568,7 +568,7 @@ export class Chromedriver extends events.EventEmitter {
this.log.debug(`${match[1]} version: '${match[2]}'`);
this.driverVersion = match[2];
try {
this.syncProtocol();
return this.syncProtocol();
} catch (e) {
this.driverVersion = null;
this.log.error(`Stopping the chromedriver process. Cannot determinate the protocol: ${e}`);
Expand All @@ -577,6 +577,7 @@ export class Chromedriver extends events.EventEmitter {
// Does not print else condition log since the log could be
// very noisy when this.verbose option is true.
}
return null;
}

/**
Expand Down Expand Up @@ -618,6 +619,7 @@ export class Chromedriver extends events.EventEmitter {
let processIsAlive = false;
/** @type {string|undefined} */
let webviewVersion;
let didDetectProtocol = false;
try {
const chromedriverPath = await this.initChromedriverPath();
await this.killAll();
Expand Down Expand Up @@ -646,7 +648,16 @@ export class Chromedriver extends events.EventEmitter {
}
}

this.detectWebDriverProtocol(line);
if (!didDetectProtocol) {
const proto = this.detectWebDriverProtocol(line);
if (proto === PROTOCOLS.W3C) {
// given caps might not be properly prefixed
// so we try to fix them in order to properly init
// the new W3C session
this.capabilities = toW3cCapNames(this.capabilities);
}
didDetectProtocol = true;
}

if (this.verbose) {
// give the output if it is requested
Expand Down

0 comments on commit 78b83b8

Please sign in to comment.