Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getApksignerOutput = async (apksignerPath) => {
let binaryPath = apksignerPath;
if (system.isWindows() && util.isSubPath(binaryPath, originalFolder)) {
// Workaround for https://github.com/nodejs/node-v0.x-archive/issues/25895
binaryPath = path.basename(binaryPath);
}
const {stdout, stderr} = await exec(binaryPath, args, {
cwd: originalFolder
});
for (let [name, stream] of [['stdout', stdout], ['stderr', stderr]]) {
if (!stream) {
continue;
}
if (name === 'stdout') {
// Make the output less talkative
stream = stream.split('\n')
.filter((line) => !line.includes('WARNING:'))
.join('\n');
}
log.debug(`apksigner ${name}: ${stream}`);
}
return stdout;
await finishPerfRecord(proc, true);
if (!await fs.exists(localPath)) {
log.errorAndThrow(`There is no .trace file found for performance profile '${profileName}' ` +
`and device ${this.opts.device.udid}. ` +
`Make sure the profile is supported on this device. ` +
`You can use 'instruments -s' command to see the list of all available profiles.`);
}
const zipPath = `${localPath}.zip`;
const zipArgs = [
'-9', '-r', zipPath,
path.basename(localPath),
];
log.info(`Found perf trace record '${localPath}'. Compressing it with 'zip ${zipArgs.join(' ')}'`);
try {
await exec('zip', zipArgs, {
cwd: path.dirname(localPath),
});
return await uploadTrace(zipPath, remotePath, {user, pass, method});
} finally {
delete runningRecorders[this.opts.device.udid];
if (await fs.exists(localPath)) {
await fs.rimraf(localPath);
}
}
};
async cleanupObsoleteProcesses () {
const obsoletePids = await getPIDsListeningOnPort(this.url.port,
(cmdLine) => cmdLine.includes('/WebDriverAgentRunner') &&
!cmdLine.toLowerCase().includes(this.device.udid.toLowerCase()));
if (_.isEmpty(obsoletePids)) {
log.debug(`No obsolete cached processes from previous WDA sessions ` +
`listening on port ${this.url.port} have been found`);
return;
}
log.info(`Detected ${obsoletePids.length} obsolete cached process${obsoletePids.length === 1 ? '' : 'es'} ` +
`from previous WDA sessions. Cleaning them up`);
try {
await exec('kill', obsoletePids);
} catch (e) {
log.warn(`Failed to kill obsolete cached process${obsoletePids.length === 1 ? '' : 'es'} '${obsoletePids}'. ` +
`Original error: ${e.message}`);
}
}
systemCallMethods.getConnectedDevices = async function () {
log.debug("Getting connected devices...");
try {
let { stdout } = await exec(this.executable.path, ['devices']);
let startingIndex = stdout.indexOf("List of devices attached");
stdout = stdout.slice(startingIndex);
if (stdout.length < 1) {
throw new Error("Could not find device.");
} else {
let devices = [];
for (let line of stdout.split("\n")) {
if (line.trim() !== "" && line.indexOf("List of devices") === -1) {
let lineInfo = line.split("\t");
devices.push({ udid: lineInfo[0].trim(), state: lineInfo[1].trim(), platform: lineInfo[2].trim() });
}
}
log.debug(`${devices.length} device(s) connected`);
return devices;
}
async function getDeveloperRoot () {
const {stdout} = await exec('xcode-select', ['-p']);
return stdout.trim();
}
async function setUserDefault (domain, key, value) {
await exec('defaults', ['write', 'Apple Global Domain', domain, '-dict-add', key, typeof value === 'undefined' ? 'nil' : value]);
}
async clear () {
log.debug('Clearing logcat logs from device');
try {
const args = this.adb.defaultArgs.concat(['logcat', '-c']);
log.debug(`Running '${this.adb.path} ${args.join(' ')}'`);
await exec(this.adb.path, args);
} catch (err) {
log.warn(`Failed to clear logcat logs: ${err.message}`);
}
}
}
const {stdout} = await exec(apkanalyzerPath, ['manifest', 'target-sdk', appPath], {
shell: true,
cwd: path.dirname(apkanalyzerPath),
});
if (isNaN(_.trim(stdout))) {
throw new Error(`Cannot parse the minimum SDK version from '${stdout}'`);
}
return parseInt(_.trim(stdout), 10);
} catch (e) {
log.info(`Cannot extract targetSdkVersion using apkanalyzer. Falling back to aapt. ` +
`Original error: ${e.message}`);
await this.initAapt();
const args = ['dump', 'badging', appPath];
let output;
try {
const {stdout} = await exec(this.binaries.aapt, args);
output = stdout;
} catch (e) {
throw new Error(`Fetching targetSdkVersion from '${originalAppPath}' failed. ` +
`Original error: ${e.message}`);
}
const targetSdkVersion = new RegExp(/targetSdkVersion:'([^']+)'/g).exec(output);
if (!targetSdkVersion) {
throw new Error(`targetSdkVersion is not specified in the '${originalAppPath}' application`);
}
return parseInt(targetSdkVersion[1], 10);
}
};
return await UI_CLIENT_ACCESS_GUARD.acquire(this.simulatorApp, async () => {
try {
const {stdout} = await exec('osascript', ['-e', resultScript]);
return stdout;
} catch (err) {
log.errorAndThrow(`Could not complete operation. Make sure Simulator UI is running and the parent Appium application (e. g. Appium.app or Terminal.app) ` +
`is present in System Preferences > Security & Privacy > Privacy > Accessibility list. If the operation is still unsuccessful then ` +
`it is not supported by this Simulator. ` +
`Original error: ${err.message}`);
}
});
}
async killUIClient (opts = {}) {
let {
pid,
signal = 2,
} = opts;
pid = pid || await this.getUIClientPid();
if (!pid) {
return false;
}
log.debug(`Sending ${signal} kill signal to Simulator UI client with PID ${pid}`);
try {
await exec('kill', [`-${signal}`, pid]);
return true;
} catch (e) {
if (e.code === 1) {
return false;
}
throw new Error(`Cannot kill the Simulator UI client. Original error: ${e.message}`);
}
}