appium-geckodriver
Version:
Appium driver for Gecko-based browsers and web views
142 lines • 5.11 kB
JavaScript
"use strict";
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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeckoDriver = void 0;
const lodash_1 = __importDefault(require("lodash"));
const driver_1 = require("appium/driver");
const gecko_1 = require("./gecko");
const desired_caps_1 = require("./desired-caps");
const findCommands = __importStar(require("./commands/find"));
const utils_1 = require("./utils");
/** @type {import('@appium/types').RouteMatcher[]} */
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 {
constructor(opts = {}) {
// @ts-ignore TODO: make args typed
super(opts);
this.findElOrEls = findCommands.findElOrEls;
this.desiredCapConstraints = 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();
}
resetState() {
// @ts-ignore It's ok
this.gecko = null;
this.proxyReqRes = null;
this.isProxyActive = false;
this._bidiProxyUrl = null;
}
proxyActive() {
return this.isProxyActive;
}
getProxyAvoidList() {
return NO_PROXY;
}
canProxy() {
return true;
}
// @ts-ignore TODO: make args typed
async createSession(...args) {
// @ts-ignore TODO: make args typed
const [sessionId, caps] = await super.createSession(...args);
this.gecko = new gecko_1.GeckoDriverServer(this.log, caps);
/** @type {import('@appium/types').StringRecord | null} */
let response = null;
try {
response = await this.gecko.start((0, utils_1.formatCapsForServer)(caps), {
reqBasePath: this.basePath,
});
}
catch (e) {
await this.deleteSession();
throw e;
}
this.proxyReqRes = this.gecko.proxy.proxyReqRes.bind(this.gecko.proxy);
this._bidiProxyUrl = this._processCreateSessionResponse(response);
if (this._bidiProxyUrl) {
this.log.info(`Set proxy BiDi URL to ${this._bidiProxyUrl}`);
}
this.isProxyActive = true;
return [sessionId, caps];
}
/**
* @override
* @returns {string | null}
*/
get bidiProxyUrl() {
return this._bidiProxyUrl;
}
async deleteSession() {
this.log.info('Ending Gecko Driver session');
await this.gecko?.stop();
this.resetState();
await super.deleteSession();
}
/**
*
* @param {import('@appium/types').StringRecord} response
* @returns {string | null}
*/
_processCreateSessionResponse(response) {
const webSocketUrl = response?.capabilities?.webSocketUrl;
if (lodash_1.default.isEmpty(webSocketUrl) || !lodash_1.default.isString(webSocketUrl)) {
return null;
}
const asUrl = new URL(webSocketUrl);
return asUrl.hostname !== gecko_1.GECKO_SERVER_HOST
? webSocketUrl.replace(asUrl.host, gecko_1.GECKO_SERVER_HOST)
: webSocketUrl;
}
}
exports.GeckoDriver = GeckoDriver;
exports.default = GeckoDriver;
//# sourceMappingURL=driver.js.map