appium-chromedriver
Version:
Node.js wrapper around chromedriver.
130 lines • 5.56 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.toW3cCapName = toW3cCapName;
exports.getCapValue = getCapValue;
exports.toW3cCapNames = toW3cCapNames;
exports.startSession = startSession;
exports.syncProtocol = syncProtocol;
exports.changeState = changeState;
const base_driver_1 = require("@appium/base-driver");
const support_1 = require("@appium/support");
const utils_1 = require("../utils");
const constants_1 = require("../constants");
const semver = __importStar(require("semver"));
const MIN_CD_VERSION_WITH_W3C_SUPPORT = 75;
const W3C_PREFIX = 'goog:';
/**
* Converts a capability name to W3C format by adding the 'goog:' prefix if needed.
*/
function toW3cCapName(capName) {
return typeof capName === 'string' && !capName.includes(':') && !(0, base_driver_1.isStandardCap)(capName)
? `${W3C_PREFIX}${capName}`
: capName;
}
/**
* Gets a capability value from a capabilities object, handling both standard and W3C format names.
*/
function getCapValue(allCaps = {}, rawCapName, defaultValue) {
for (const [capName, capValue] of Object.entries(allCaps)) {
if (toW3cCapName(capName) === toW3cCapName(rawCapName)) {
return capValue;
}
}
return defaultValue;
}
/**
* Converts all capability names in an object to W3C format.
*/
function toW3cCapNames(originalCaps = {}) {
return Object.fromEntries(Object.entries(originalCaps).map(([key, value]) => [toW3cCapName(key), value]));
}
/**
* Creates a new Chromedriver session using the negotiated downstream protocol.
*/
async function startSession() {
const sessionCaps = this._desiredProtocol === base_driver_1.PROTOCOLS.W3C
? { capabilities: { alwaysMatch: toW3cCapNames(this.capabilities) } }
: { desiredCapabilities: this.capabilities };
this.log.info(`Starting ${this._desiredProtocol} Chromedriver session with capabilities: ` +
JSON.stringify(sessionCaps, null, 2));
const response = (await this.jwproxy.command('/session', 'POST', sessionCaps));
this.log.prefix = (0, utils_1.generateLogPrefix)(this, this.jwproxy.sessionId);
changeState.call(this, constants_1.CHROMEDRIVER_STATES.ONLINE);
return response?.capabilities ?? response;
}
/**
* Chooses W3C or MJSONWP protocol based on driver/capability constraints.
*/
function syncProtocol() {
if (this.driverVersion) {
const coercedVersion = semver.coerce(this.driverVersion);
if (!coercedVersion || coercedVersion.major < MIN_CD_VERSION_WITH_W3C_SUPPORT) {
this.log.info(`The ChromeDriver v. ${this.driverVersion} does not fully support ${base_driver_1.PROTOCOLS.W3C} protocol. ` +
`Defaulting to ${base_driver_1.PROTOCOLS.MJSONWP}`);
this._desiredProtocol = base_driver_1.PROTOCOLS.MJSONWP;
return this._desiredProtocol;
}
}
const statusMsg = this._onlineStatus?.message;
const isOperaDriver = typeof statusMsg === 'string' && statusMsg.includes('OperaDriver');
const chromeOptions = getCapValue(this.capabilities, 'chromeOptions');
if (support_1.util.isPlainObject(chromeOptions) && chromeOptions.w3c === false) {
this.log.info(`The ChromeDriver v. ${this.driverVersion} supports ${base_driver_1.PROTOCOLS.W3C} protocol, ` +
`but ${base_driver_1.PROTOCOLS.MJSONWP} one has been explicitly requested`);
this._desiredProtocol = base_driver_1.PROTOCOLS.MJSONWP;
return this._desiredProtocol;
}
else if (isOperaDriver) {
// OperaDriver requires explicit W3C request or it falls back to JWP.
if (support_1.util.isPlainObject(chromeOptions)) {
chromeOptions.w3c = true;
}
else {
this.capabilities[toW3cCapName('chromeOptions')] = { w3c: true };
}
}
this._desiredProtocol = base_driver_1.PROTOCOLS.W3C;
return this._desiredProtocol;
}
/**
* Updates driver state and emits state-change event payload.
*/
function changeState(state) {
this.state = state;
this.log.debug(`Changed state to '${state}'`);
this.emit(constants_1.CHROMEDRIVER_EVENTS.CHANGED, { state });
}
//# sourceMappingURL=session.js.map