Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('with udid booted: uses sim and leaves it afterwards', async function () {
// before
const udid = await createDevice();
let sim = await getSimulator(udid, {
platform: 'iOS',
checkExistence: false,
});
await sim.run();
await B.delay(2000);
// test
let caps = _.defaults({
udid,
noReset: true
}, UICATALOG_SIM_CAPS);
(await sim.isRunning()).should.be.true;
let simsBefore = await getNumSims();
await initSession(caps);
if (this.opts.platformVersion !== devicePlatform) {
this.opts.platformVersion = devicePlatform;
log.info(`Set platformVersion to '${devicePlatform}' to match the device with given UDID`);
}
await setupVersionCaps();
return {device, realDevice: false, udid: device.udid};
}
} else {
// make sure it is a connected device. If not, the udid passed in is invalid
const devices = await getConnectedDevices();
log.debug(`Available devices: ${devices.join(', ')}`);
if (!devices.includes(this.opts.udid)) {
// check for a particular simulator
log.debug(`No real device with udid '${this.opts.udid}'. Looking for simulator`);
try {
const device = await getSimulator(this.opts.udid);
return {device, realDevice: false, udid: this.opts.udid};
} catch (ign) {
throw new Error(`Unknown device or simulator UDID: '${this.opts.udid}'`);
}
}
}
const device = await getRealDeviceObj(this.opts.udid);
return {device, realDevice: true, udid: this.opts.udid};
}
// Now we know for sure the device will be a Simulator
await setupVersionCaps();
if (this.opts.enforceFreshSimulatorCreation) {
log.debug(`New simulator is requested. If this is not wanted, set 'enforceFreshSimulatorCreation' capability to false`);
} else {
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();
address = `http://localhost:${port}`;
// we don't know if there needs to be changes a priori, so change first.
// sometimes the shutdown process changes the settings, so reset them,
// knowing that the sim is already shut
await iosSettings.setLocaleAndPreferences(sim, this.opts, this.isSafari());
});
await this.startSim();
if (this.opts.customSSLCert) {
if (await hasSSLCert(this.opts.customSSLCert, this.opts.udid)) {
log.info(`SSL cert '${_.truncate(this.opts.customSSLCert, {length: 20})}' already installed`);
} else {
log.info(`Installing ssl cert '${_.truncate(this.opts.customSSLCert, {length: 20})}'`);
await shutdownSimulator(this.opts.device);
await installSSLCert(this.opts.customSSLCert, this.opts.udid);
log.info(`Restarting Simulator so that SSL certificate installation takes effect`);
await this.startSim();
this.logEvent('customCertInstalled');
}
}
if (this.opts.launchWithIDB && this.isSimulator()) {
try {
const idb = new IDB({udid});
await idb.connect();
this.opts.device.idb = idb;
} catch (e) {
log.info(`idb will not be used for Simulator interaction. Original error: ${e.message}`);
}
}
this.logEvent('simStarted');
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();
}
}
}
this.localConfig = await iosSettings.setLocaleAndPreferences(this.opts.device, this.opts, this.isSafari(), async (sim) => {
await shutdownSimulator(sim);
// we don't know if there needs to be changes a priori, so change first.
// sometimes the shutdown process changes the settings, so reset them,
// knowing that the sim is already shut
await iosSettings.setLocaleAndPreferences(sim, this.opts, this.isSafari());
});
await this.startSim();
if (this.opts.customSSLCert) {
if (await hasSSLCert(this.opts.customSSLCert, this.opts.udid)) {
log.info(`SSL cert '${_.truncate(this.opts.customSSLCert, {length: 20})}' already installed`);
} else {
log.info(`Installing ssl cert '${_.truncate(this.opts.customSSLCert, {length: 20})}'`);
await shutdownSimulator(this.opts.device);
await installSSLCert(this.opts.customSSLCert, this.opts.udid);
log.info(`Restarting Simulator so that SSL certificate installation takes effect`);
await this.startSim();
this.logEvent('customCertInstalled');
}
}
if (this.opts.launchWithIDB && this.isSimulator()) {
try {
const idb = new IDB({udid});
await idb.connect();
this.opts.device.idb = idb;
} catch (e) {
if (!this.xcodeVersion) {
logger.debug('Setting Xcode version');
this.xcodeVersion = await utils.getAndCheckXcodeVersion(this.opts);
logger.debug(`Xcode version set to ${this.xcodeVersion.versionString}`);
}
logger.debug('Setting iOS SDK Version');
this.iosSdkVersion = await utils.getAndCheckIosSdkVersion();
logger.debug(`iOS SDK Version set to ${this.iosSdkVersion}`);
let timeout = _.isObject(this.opts.launchTimeout) ? this.opts.launchTimeout.global : this.opts.launchTimeout;
let availableDevices = await retry(3, instrumentsUtils.getAvailableDevices, timeout);
let iosSimUdid = await checkSimulatorAvailable(this.opts, this.iosSdkVersion, availableDevices);
this.sim = await getSimulator(iosSimUdid, this.xcodeVersion.versionString);
await moveBuiltInApp(this.sim);
this.opts.localizableStrings = await utils.parseLocalizableStrings(this.opts);
await utils.setBundleIdFromApp(this.opts);
await this.createInstruments();
{
// previously setDeviceInfo()
this.shouldPrelaunchSimulator = utils.shouldPrelaunchSimulator(this.opts, this.iosSdkVersion);
let dString = await getAdjustedDeviceName(this.opts);
if (this.caps.app) {
await utils.setDeviceTypeInInfoPlist(this.opts.app, dString);
}
await this.createInstruments();
{
// previously setDeviceInfo()
this.shouldPrelaunchSimulator = utils.shouldPrelaunchSimulator(this.opts, this.iosSdkVersion);
let dString = await getAdjustedDeviceName(this.opts);
if (this.caps.app) {
await utils.setDeviceTypeInInfoPlist(this.opts.app, dString);
}
}
await runSimulatorReset(this.sim, this.opts, this.keepAppToRetainPrefs);
if (this.caps.customSSLCert && !this.isRealDevice()) {
await installSSLCert(this.caps.customSSLCert, this.sim.udid);
}
if (this.opts.enableAsyncExecuteFromHttps && !this.isRealDevice()) {
// await this.sim.shutdown();
await this.startHttpsAsyncServer();
}
await isolateSimulatorDevice(this.sim, this.opts);
this.localConfig = await setLocaleAndPreferences(this.sim, this.opts, this.isSafari(), endSimulator);
await this.setUpLogCapture();
await this.prelaunchSimulator();
await this.startInstruments();
await this.onInstrumentsLaunch();
await this.configureBootstrap();
await this.setBundleId();
await this.setInitialOrientation();
let address = this.opts.callbackAddress || this.opts.address;
let port = this.opts.callbackPort || this.opts.port;
let {sslServer, pemCertificate, httpsPort} = await startHttpsServer(port, address);
this.opts.sslServer = sslServer;
this.opts.httpsServerCertificate = pemCertificate;
this.opts.httpsCallbackPort = httpsPort;
this.opts.httpsCallbackAddress = 'localhost';
let udid;
if (this.sim) {
// ios driver
udid = this.sim.udid;
} else {
// xcuitest driver
udid = this.opts.udid;
}
await installSSLCert(this.opts.httpsServerCertificate, udid);
};
} catch (err) {
log.error(`Error launching instruments: ${err.message}`);
let errIsCatchable = err.message === ERR_NEVER_CHECKED_IN ||
err.message === ERR_CRASHED_ON_STARTUP;
if (!errIsCatchable) {
throw err;
}
if (launchTries <= this.flakeyRetries) {
if (this.gotFBSOpenApplicationError) {
log.debug('Got the FBSOpenApplicationError, not killing the ' +
'sim but leaving it open so the app will launch');
this.gotFBSOpenApplicationError = false; // clear out for next launch
await B.delay(1000);
} else {
if (!this.realDevice) {
await killAllSimulators();
}
await B.delay(5000);
}
} else {
log.errorAndThrow('We exceeded the number of retries allowed for ' +
'instruments to successfully start; failing launch');
}
}
} while (true);
}