appium-chromedriver
Version:
Node.js wrapper around chromedriver.
115 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildChromedriverArgs = buildChromedriverArgs;
exports.getStatus = getStatus;
exports.waitForOnline = waitForOnline;
exports.killAll = killAll;
const support_1 = require("@appium/support");
const asyncbox_1 = require("asyncbox");
const teen_process_1 = require("teen_process");
const constants_1 = require("../constants");
const VERSION_PATTERN = /([\d.]+)/;
/**
* Builds command-line arguments for the Chromedriver subprocess.
*/
function buildChromedriverArgs() {
const args = [`--port=${this.proxyPort}`];
if (this.adb?.adbPort) {
args.push(`--adb-port=${this.adb.adbPort}`);
}
if (Array.isArray(this.cmdArgs)) {
args.push(...this.cmdArgs);
}
if (this.logPath) {
args.push(`--log-path=${this.logPath}`);
}
if (this.disableBuildCheck) {
args.push('--disable-build-check');
}
args.push('--verbose');
return args;
}
/**
* Retrieves Chromedriver `/status` payload through the active proxy.
*/
async function getStatus() {
return await this.jwproxy.command('/status', 'GET');
}
/**
* Polls Chromedriver until it reports ready and captures runtime metadata.
*/
async function waitForOnline() {
const self = this;
let chromedriverStopped = false;
await (0, asyncbox_1.retryInterval)(20, 200, async () => {
if (this.state === constants_1.CHROMEDRIVER_STATES.STOPPED) {
chromedriverStopped = true;
return;
}
const status = await self.getStatus();
if (!support_1.util.isPlainObject(status) || !status.ready) {
throw new Error(`The response to the /status API is not valid: ${JSON.stringify(status)}`);
}
const statusPayload = status;
this._onlineStatus = statusPayload;
const versionMatch = VERSION_PATTERN.exec(statusPayload.build?.version ?? '');
if (versionMatch) {
this._driverVersion = versionMatch[1];
this.log.info(`Chromedriver version: ${this._driverVersion}`);
}
else {
this.log.info('Chromedriver version cannot be determined from the /status API response');
}
});
if (chromedriverStopped) {
throw new Error('ChromeDriver crashed during startup.');
}
}
/**
* Cleans up stale Chromedriver processes and leftover adb forwarded ports.
*/
async function killAll() {
const cmd = support_1.system.isWindows() ? 'wmic' : 'pkill';
const args = support_1.system.isWindows()
? [
'process',
'where',
`commandline like '%chromedriver.exe%--port=${this.proxyPort}%'`,
'delete',
]
: ['-15', '-f', `${this.chromedriver}.*--port=${this.proxyPort}`];
this.log.debug(`Killing any old chromedrivers, running: ${cmd} ${args.join(' ')}`);
try {
await (0, teen_process_1.exec)(cmd, args);
this.log.debug('Successfully cleaned up old chromedrivers');
}
catch {
this.log.info('No old chromedrivers seem to exist');
}
if (this.adb) {
const udidIndex = this.adb.executable.defaultArgs.findIndex((item) => item === '-s');
const udid = udidIndex > -1 ? this.adb.executable.defaultArgs[udidIndex + 1] : null;
if (udid) {
this.log.debug(`Cleaning this device's adb forwarded port socket connections: ${udid}`);
}
else {
this.log.debug(`Cleaning any old adb forwarded port socket connections`);
}
try {
for (const conn of await this.adb.getForwardList()) {
if (!(conn.includes('webview_devtools') && (!udid || conn.includes(udid)))) {
continue;
}
const params = conn.split(/\s+/);
if (params.length > 1) {
await this.adb.removePortForward(params[1].replace(/[\D]*/, ''));
}
}
}
catch (e) {
const err = e;
this.log.warn(`Unable to clean forwarded ports. Error: '${err.message}'. Continuing.`);
}
}
}
//# sourceMappingURL=process.js.map