Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before(async function () {
udid = await simctl.createDevice('ios-simulator testing',
deviceType.device,
deviceType.version,
{timeout: 20000});
// just need a little more space in the logs
console.log('\n\n'); // eslint-disable-line no-console
});
after(async function () {
try {
await killAllSimulators();
for (const udid of _.keys(simulatorsMapping)) {
try {
await simctl.deleteDevice(udid);
} catch (err) {
console.log(`Error deleting simulator '${udid}': ${err.message}`); // eslint-disable-line
}
}
} finally {
simulatorsMapping = {};
}
});
beforeEach(killAllSimulators);
it('should find both a data and bundle directory for TestApp', async function () {
let sim = await getSimulator(udid);
await sim.run({startupTimeout: LONG_TIMEOUT});
// install & launch test app
await installApp(sim, app);
await simctl.launch(udid, BUNDLE_ID);
let dirs = await sim.getAppDirs('TestApp', BUNDLE_ID);
dirs.should.have.length(2);
dirs[0].should.contain('/Data/');
dirs[1].should.contain('/Bundle/');
await sim.getUserInstalledBundleIdsByBundleName('TestApp').should.eventually.not.empty;
});
console.log('Application installed'); // eslint-disable-line no-console
(await sim.isAppInstalled(BUNDLE_ID)).should.be.true;
// this remains somewhat flakey
await retryInterval(5, 1000, async () => {
await simctl.launch(udid, BUNDLE_ID, 1);
});
console.log('Application launched'); // eslint-disable-line no-console
await sim.removeApp(BUNDLE_ID);
// should not be able to launch anymore
await simctl.launch(udid, BUNDLE_ID, 1)
.should.eventually.be.rejectedWith(error);
(await sim.isAppInstalled(BUNDLE_ID)).should.be.false;
console.log('Test case finished'); // eslint-disable-line no-console
});
async function shutdownOtherSimulators (currentDevice) {
const allDevices = _.flatMap(_.values(await getDevices()));
const otherBootedDevices = allDevices.filter((device) => device.udid !== currentDevice.udid && device.state === 'Booted');
if (_.isEmpty(otherBootedDevices)) {
log.info('No other running simulators have been detected');
return;
}
log.info(`Detected ${otherBootedDevices.length} other running Simulator${otherBootedDevices.length === 1 ? '' : 's'}.` +
`Shutting ${otherBootedDevices.length === 1 ? 'it' : 'them'} down...`);
for (const {udid} of otherBootedDevices) {
// It is necessary to stop the corresponding xcodebuild process before killing
// the simulator, otherwise it will be automatically restarted
await resetTestProcesses(udid, true);
await shutdown(udid);
}
}
async function killAllSimulators () {
if (process.env.CLOUD) {
return;
}
const allDevices = _.flatMap(_.values(await getDevices()));
const bootedDevices = allDevices.filter((device) => device.state === 'Booted');
for (const {udid} of bootedDevices) {
// It is necessary to stop the corresponding xcodebuild process before killing
// the simulator, otherwise it will be automatically restarted
await resetTestProcesses(udid, true);
await shutdown(udid);
}
await simKill();
}
async function getExistingSim (opts) {
let appiumTestDevice;
for (const device of _.values(await getDevices(opts.platformVersion))) {
if (device.name === opts.deviceName) {
return await getSimulator(device.udid, {
platform: device.platform,
checkExistence: false,
});
}
if (device.name.startsWith(APPIUM_SIM_PREFIX) && device.name.endsWith(opts.deviceName)) {
appiumTestDevice = device;
// choose the first booted simulator
if (device.state === 'Booted') {
break;
}
}
}
before(async function () {
sim = await getExistingSim(DEVICE_NAME, PLATFORM_VERSION);
if (!sim) {
const udid = await createDevice(SIM_NAME, DEVICE_NAME, PLATFORM_VERSION);
sim = await getSimulator(udid);
simCreated = true;
}
// on certain system, particularly Xcode 11 on Travis, starting the sim fails
await retry(4, async function () {
try {
await sim.run({
startupTimeout: 60000,
});
} catch (err) {
await sim.shutdown();
throw err;
}
});
const port = await startHttpServer();
before(async function () {
const udid = await createDevice(
SIM_DEVICE_NAME,
translateDeviceName(UICATALOG_SIM_CAPS.platformVersion, UICATALOG_SIM_CAPS.deviceName),
UICATALOG_SIM_CAPS.platformVersion
);
baseCaps = Object.assign({}, UICATALOG_SIM_CAPS, {udid});
caps = Object.assign({
usePrebuiltWDA: true,
agentPath: path.join(TEMP_FOLDER, 'WebDriverAgent.xcodeproj'),
derivedDataPath: path.join(TEMP_FOLDER, 'DerivedData', 'WebDriverAgent')
}, baseCaps);
// copy existing WebDriverAgent to the selected derivedDataPath folder
const wda_path = path.join(process.cwd(), 'WebDriverAgent');
fs.symlinkSync(wda_path, TEMP_FOLDER);
});
after(async function () {
async function deleteSimulator (udid, version) {
// only want to get rid of the device if it is present
let devices = await simctl.getDevices();
if (!devices[version]) {
return;
}
let devicePresent = devices[version]
.filter((device) => {
return device.udid === udid;
}).length > 0;
if (devicePresent) {
await simctl.deleteDevice(udid);
}
}