Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await finishPerfRecord(proc, false);
if (await fs.exists(localPath)) {
await fs.rimraf(localPath);
}
delete runningRecorders[this.opts.device.udid];
}
let instrumentsPath;
try {
instrumentsPath = await fs.which('instruments');
} catch (e) {
log.errorAndThrow(`Cannot start performance recording, because 'instruments' ` +
`tool cannot be found in PATH. Are Xcode development tools installed?`);
}
const localPath = await tempDir.path({
prefix: `appium_perf_${profileName}_${Date.now()}`.replace(/\W/g, '_'),
suffix: DEFAULT_EXT,
});
// https://help.apple.com/instruments/mac/current/#/devb14ffaa5
const args = [
'-w', this.opts.device.udid,
'-t', profileName,
'-D', localPath,
'-l', timeout,
];
if (pid) {
if (`${pid}`.toLowerCase() === 'current') {
const appInfo = await this.proxyCommand('/wda/activeAppInfo', 'GET');
args.push('-p', appInfo.pid);
} else {
args.push('-p', pid);
before(async function () {
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
picturePath = await tempDir.path({prefix: 'pixel', suffix: '.png'});
await fs.writeFile(picturePath, Buffer.from(BASE64_PNG, 'base64').toString('binary'), 'binary');
});
after(async function () {
videoQuality = DEFAULT_QUALITY,
videoFps = DEFAULT_FPS,
videoFilters,
videoScale,
forceRestart,
pixelFormat
} = options;
let result = '';
if (!forceRestart) {
log.info(`Checking if there is/was a previous screen recording. ` +
`Set 'forceRestart' option to 'true' if you'd like to skip this step.`);
result = await this.stopRecordingScreen(options);
}
const videoPath = await tempDir.path({
prefix: `appium_${Math.random().toString(16).substring(2, 8)}`,
suffix: MP4_EXT,
});
const wdaBaseUrl = this.opts.wdaBaseUrl || WDA_BASE_URL;
const screenRecorder = new ScreenRecorder(this.opts.device.udid, videoPath, {
remotePort: this.opts.mjpegServerPort || DEFAULT_MJPEG_SERVER_PORT,
remoteUrl: wdaBaseUrl,
usePortForwarding: this.isRealDevice() && isLocalHost(wdaBaseUrl),
videoType,
videoFilters,
videoScale,
videoFps,
pixelFormat
});
if (!await screenRecorder.interrupt(true)) {
async function generateXcodeConfigFile (orgId, signingId) {
log.debug(`Generating xcode config file for orgId '${orgId}' and signingId ` +
`'${signingId}'`);
const contents = `DEVELOPMENT_TEAM = ${orgId}
CODE_SIGN_IDENTITY = ${signingId}
`;
const xcconfigPath = await tempDir.path('appium-temp.xcconfig');
log.debug(`Writing xcode config file to ${xcconfigPath}`);
await fs.writeFile(xcconfigPath, contents, 'utf8');
return xcconfigPath;
}
beforeEach(async function () {
let temp = await tempDir.path();
tmpPlist = path.resolve(temp, 'sample.plist');
await fs.copyFile(plist, tmpPlist);
});
await this.initZipAlign();
try {
await exec(this.binaries.zipalign, ['-c', '4', apk]);
log.debug(`${apk}' is already zip-aligned. Doing nothing`);
return false;
} catch (e) {
log.debug(`'${apk}' is not zip-aligned. Aligning`);
}
try {
await fs.access(apk, _fs.W_OK);
} catch (e) {
throw new Error(`The file at '${apk}' is not writeable. ` +
`Please grant write permissions to this file or to its parent folder '${path.dirname(apk)}' ` +
`for the Appium process, so it can zip-align the file`);
}
const alignedApk = await tempDir.path({prefix: 'appium', suffix: '.tmp'});
await mkdirp(path.dirname(alignedApk));
try {
await exec(this.binaries.zipalign, ['-f', '4', apk, alignedApk]);
await fs.mv(alignedApk, apk, { mkdirp: true });
return true;
} catch (e) {
if (await fs.exists(alignedApk)) {
await fs.unlink(alignedApk);
}
throw new Error(`zipAlignApk failed. Original error: ${e.message}. Stdout: '${e.stdout}'; Stderr: '${e.stderr}'`);
}
};
commands.pullFolder = async function pullFolder (remotePath) {
let localFolder = await tempDir.path({prefix: 'appium'});
await this.adb.pull(remotePath, localFolder);
return (await zip.toInMemoryZip(localFolder)).toString('base64');
};
async function getScreenshot (udid) {
let pathToScreenshotPng = await tempDir.path({prefix: `screenshot-${udid}`, suffix: '.png'});
await simExec('io', 0, [udid, 'screenshot', pathToScreenshotPng]);
let screenshotImg = await fs.readFile(pathToScreenshotPng);
await fs.rimraf(pathToScreenshotPng);
return screenshotImg.toString('base64');
}
}
let tmpDestination = null;
if (remotePath.startsWith(CONTAINER_PATH_MARKER)) {
const [packageId, pathInContainer] = parseContainerPath(remotePath);
log.info(`Parsed package identifier '${packageId}' from '${remotePath}'. Will get the data from '${pathInContainer}'`);
tmpDestination = `/data/local/tmp/${path.posix.basename(pathInContainer)}`;
try {
await this.adb.shell(['run-as', packageId, `chmod 777 '${pathInContainer.replace(/'/g, '\\\'')}'`]);
await this.adb.shell(['cp', '-f', pathInContainer, tmpDestination]);
} catch (e) {
log.errorAndThrow(`Cannot access the container of '${packageId}' application. ` +
`Is the application installed and has 'debuggable' build option set to true? ` +
`Original error: ${e.message}`);
}
}
const localFile = await tempDir.path({prefix: 'appium', suffix: '.tmp'});
try {
await this.adb.pull(_.isString(tmpDestination) ? tmpDestination : remotePath, localFile);
const data = await fs.readFile(localFile);
return Buffer.from(data).toString('base64');
} finally {
if (await fs.exists(localFile)) {
await fs.unlink(localFile);
}
if (_.isString(tmpDestination)) {
await this.adb.shell(['rm', '-f', tmpDestination]);
}
}
};
async function patchApksigner (originalPath) {
const originalContent = await fs.readFile(originalPath, 'ascii');
const patchedContent = originalContent.replace('-Djava.ext.dirs="%frameworkdir%"',
'-cp "%frameworkdir%\\*"');
if (patchedContent === originalContent) {
return originalPath;
}
log.debug(`Patching '${originalPath}...`);
const patchedPath = await tempDir.path({prefix: 'apksigner', suffix: '.bat'});
await mkdirp(path.dirname(patchedPath));
await fs.writeFile(patchedPath, patchedContent, 'ascii');
return patchedPath;
}