appium-geckodriver
Version:
Appium driver for Gecko-based browsers and web views
155 lines • 5.72 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.GeckoDriver = void 0;
const driver_1 = require("appium/driver");
const gecko_1 = require("./gecko");
const desired_caps_1 = require("./desired-caps");
const constants_1 = require("./constants");
const findCommands = __importStar(require("./commands/find"));
const utils_1 = require("./utils");
const NO_PROXY = [
['GET', new RegExp('^/session/[^/]+/appium')],
['POST', new RegExp('^/session/[^/]+/appium')],
['POST', new RegExp('^/session/[^/]+/element/[^/]+/elements?$')],
['POST', new RegExp('^/session/[^/]+/elements?$')],
];
class GeckoDriver extends driver_1.BaseDriver {
proxyReqRes = null;
findElOrEls = findCommands.findElOrEls;
isProxyActive = false;
_gecko = null;
_bidiProxyUrl = null;
constructor(opts = {}) {
super(opts);
this.desiredCapConstraints = structuredClone(desired_caps_1.desiredCapConstraints);
this.locatorStrategies = [
'xpath',
'tag name',
'link text',
'partial link text',
'css selector',
// Let these two reach Gecko Driver and fail there with a proper error message
'id',
'name',
];
this.resetState();
}
get gecko() {
if (!this._gecko) {
throw new Error('Gecko driver is not initialized');
}
return this._gecko;
}
get bidiProxyUrl() {
return this._bidiProxyUrl;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
proxyActive(sessionId) {
return this.isProxyActive;
}
getProxyAvoidList() {
return NO_PROXY;
}
canProxy() {
return true;
}
validateDesiredCaps(caps) {
const isValid = super.validateDesiredCaps(caps);
if (!isValid) {
return false;
}
if (caps.geckodriverExecutable &&
!this.isFeatureEnabled(constants_1.INSECURE_FEAT_CUSTOM_GECKODRIVER_EXECUTABLE)) {
throw new driver_1.errors.SessionNotCreatedError(`The 'geckodriverExecutable' capability requires the ` +
`'${constants_1.INSECURE_FEAT_CUSTOM_GECKODRIVER_EXECUTABLE}' insecure feature to be enabled ` +
`on the Appium server.`);
}
return true;
}
async createSession(w3cCaps1, w3cCaps2, ...args) {
const [sessionId, processedCaps] = await super.createSession(w3cCaps1, w3cCaps2, ...args);
this._gecko = new gecko_1.GeckoDriverServer(this.log, processedCaps);
let response;
try {
response = await this._gecko.start((0, utils_1.formatCapsForServer)(processedCaps), {
reqBasePath: this.basePath,
});
}
catch (e) {
await this.deleteSession();
throw e;
}
this.proxyReqRes = this._gecko.proxy.proxyReqRes.bind(this._gecko.proxy);
this._bidiProxyUrl = this._extractWebSocketUrl(response);
if (this._bidiProxyUrl) {
this.log.info(`Set proxy BiDi URL to ${this._bidiProxyUrl}`);
}
this.isProxyActive = true;
return [sessionId, processedCaps];
}
async deleteSession() {
this.log.info('Ending Gecko Driver session');
await this._gecko?.stop();
this.resetState();
await super.deleteSession();
}
resetState() {
this._gecko = null;
this.proxyReqRes = null;
this.isProxyActive = false;
this._bidiProxyUrl = null;
}
_extractWebSocketUrl(response) {
const webSocketUrl = response?.capabilities?.webSocketUrl;
if (typeof webSocketUrl !== 'string' || webSocketUrl.length === 0) {
return null;
}
try {
const asUrl = new URL(webSocketUrl);
return asUrl.hostname !== gecko_1.GECKO_SERVER_HOST
? webSocketUrl.replace(asUrl.host, gecko_1.GECKO_SERVER_HOST)
: webSocketUrl;
}
catch (e) {
const msg = e instanceof Error ? e.message : String(e);
this.log.warn(`Failed to parse WebSocket URL from '${webSocketUrl}': ${msg}`);
return null;
}
}
}
exports.GeckoDriver = GeckoDriver;
exports.default = GeckoDriver;
//# sourceMappingURL=driver.js.map