Skip to content

Commit

Permalink
Add enforceFreshSimulatorCreation capability (appium#859)
Browse files Browse the repository at this point in the history
* Add useNewSimulator capability

* Change cap name to 'enforceFreshSimulatorCreation'

* Move platformVersion check in determinDevice
  • Loading branch information
imurchie authored Jan 19, 2019
1 parent 48375f4 commit f7cfa84
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
3 changes: 3 additions & 0 deletions lib/desired-caps.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ let desiredCapConstraints = _.defaults({
useJSONSource: {
isBoolean: true
},
enforceFreshSimulatorCreation: {
isBoolean: true
},
shutdownOtherSimulators: {
isBoolean: true
},
Expand Down
26 changes: 12 additions & 14 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,31 +734,29 @@ class XCUITestDriver extends BaseDriver {
return {device, realDevice: true, udid: this.opts.udid};
}

// figure out the correct simulator to use, given the desired capabilities
let device = await getExistingSim(this.opts);

// check for an existing simulator
if (device) {
return {device, realDevice: false, udid: device.udid};
}

// no device of this type exists, so create one
log.info('Simulator udid not provided, using desired caps to create a new simulator');
if (!this.opts.platformVersion && this.iosSdkVersion) {
log.info(`No platformVersion specified. Using latest version Xcode supports: '${this.iosSdkVersion}' ` +
`This may cause problems if a simulator does not exist for this platform version.`);
this.opts.platformVersion = this.iosSdkVersion;
}

if (this.opts.noReset) {
// Check for existing simulator just with correct capabilities
let device = await getExistingSim(this.opts);
if (this.opts.enforceFreshSimulatorCreation) {
log.debug(`New simulator is requested. If this is not wanted, set 'enforceFreshSimulatorCreation' capability to false`);
} else {
// figure out the correct simulator to use, given the desired capabilities
const device = await getExistingSim(this.opts);

// check for an existing simulator
if (device) {
return {device, realDevice: false, udid: device.udid};
}

log.info('Simulator udid not provided');
}

device = await this.createSim();
// no device of this type exists, or they request new sim, so create one
log.info('Using desired caps to create a new simulator');
const device = await this.createSim();
return {device, realDevice: false, udid: device.udid};
}

Expand Down
4 changes: 3 additions & 1 deletion lib/simulator-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { resetXCTestProcesses } from './utils';
import _ from 'lodash';
import log from './logger';
import { tempDir, fs, mkdirp } from 'appium-support';
import UUID from 'uuid-js';


const INSTALL_DAEMON_CACHE = 'com.apple.mobile.installd.staging';

Expand All @@ -17,7 +19,7 @@ const INSTALL_DAEMON_CACHE = 'com.apple.mobile.installd.staging';
* @returns {object} Simulator object associated with the udid passed in.
*/
async function createSim (caps) {
const appiumTestDeviceName = `appiumTest-${caps.deviceName}`;
const appiumTestDeviceName = `appiumTest-${UUID.create().toString().toUpperCase()}-${caps.deviceName}`;
const udid = await createDevice(appiumTestDeviceName, caps.deviceName, caps.platformVersion);
return await getSimulator(udid);
}
Expand Down
20 changes: 10 additions & 10 deletions test/functional/driver/driver-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const SIM_DEVICE_NAME = 'xcuitestDriverTest';
const should = chai.should();
chai.use(chaiAsPromised);

const getNumSims = async () => {
async function getNumSims () {
return (await getDevices())[UICATALOG_SIM_CAPS.platformVersion].length;
};
const deleteDeviceWithRetry = async function (udid) {
}

async function deleteDeviceWithRetry (udid) {
try {
await retryInterval(10, 1000, deleteDevice, udid);
} catch (ign) {}
};
}

describe('XCUITestDriver', function () {
this.timeout(MOCHA_TIMEOUT);
Expand Down Expand Up @@ -146,17 +147,16 @@ describe('XCUITestDriver', function () {
});
});

it.skip('default: creates sim and deletes it afterwards', async function () {
let caps = UICATALOG_SIM_CAPS;
it('default: creates sim and deletes it afterwards', async function () {
const caps = Object.assign({}, UICATALOG_SIM_CAPS, {enforceFreshSimulatorCreation: true});

await killAllSimulators();
let simsBefore = await getNumSims();
const simsBefore = await getNumSims();
await initSession(caps);

let simsDuring = await getNumSims();
const simsDuring = await getNumSims();

await deleteSession();
let simsAfter = await getNumSims();
const simsAfter = await getNumSims();

simsDuring.should.equal(simsBefore + 1);
simsAfter.should.equal(simsBefore);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/simulator-management-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('simulator management', function () {
await createSim(caps);

createDeviceStub.calledOnce.should.be.true;
createDeviceStub.firstCall.args[0].should.eql('appiumTest-iPhone 6');
/appiumTest-[\w-]{36}-iPhone 6/.test(createDeviceStub.firstCall.args[0]).should.be.true;
createDeviceStub.firstCall.args[1].should.eql('iPhone 6');
createDeviceStub.firstCall.args[2].should.eql('10.1');
getSimulatorStub.calledOnce.should.be.true;
Expand Down

0 comments on commit f7cfa84

Please sign in to comment.