Skip to content

Commit

Permalink
Add a possibility to shutdown other running Simulators on session sta…
Browse files Browse the repository at this point in the history
…rtup (appium#600)

* Add a possibility to shutdown other running Simulators on session startup

* Tune logging
  • Loading branch information
Mykola Mokhnach authored Jan 12, 2018
1 parent bbea235 commit 46d20b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Differences noted here
|`absoluteWebLocations`|This capability will direct the `Get Element Location` command, when used within webviews, to return coordinates which are relative to the origin of the page, rather than relative to the current scroll offset. This capability has no effect outside of webviews. Default `false`.|e.g., `true`|
|`simulatorWindowCenter`|Allows to explicitly set the coordinates of Simulator window center for Xcode9+ SDK. This capability only has an effect if Simulator window has not been opened yet for the current session before it started.|e.g. `{-100.0,100.0}` or `{500,500}`, spaces are not allowed|
|`useJSONSource`|Get JSON source from WDA and parse into XML on Appium server. This can be much faster, especially on large devices. Defaults to `false`.|e.g., `true`|
|`shutdownOtherSimulators`|If this capability set to `true` and the current device under test is an iOS Simulator then Appium will try to shutdown all the other running Simulators before to start a new session. This might be useful while executing webview tests on different devices, since only one device can be debugged remotely at once due to an Apple bug. The capability only has an effect if `--relaxed-security` command line argument is provided to the server. Defaults to `false`.|e.g. `true`|

## Development<a id="development"></a>

Expand Down
5 changes: 4 additions & 1 deletion lib/desired-caps.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ let desiredCapConstraints = _.defaults({
},
useJSONSource: {
isBoolean: true
}
},
shutdownOtherSimulators: {
isBoolean: true
},
}, iosDesiredCapConstraints);

export { desiredCapConstraints };
Expand Down
11 changes: 10 additions & 1 deletion lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import _ from 'lodash';
import { launch } from 'node-simctl';
import WebDriverAgent from './wda/webdriveragent';
import log from './logger';
import { createSim, getExistingSim, runSimulatorReset, installToSimulator } from './simulator-management';
import { createSim, getExistingSim, runSimulatorReset, installToSimulator,
shutdownOtherSimulators } from './simulator-management';
import { simExists, getSimulator, installSSLCert, uninstallSSLCert } from 'appium-ios-simulator';
import { retryInterval } from 'asyncbox';
import { settings as iosSettings, defaultServerCaps, appUtils, IWDP } from 'appium-ios-driver';
Expand Down Expand Up @@ -296,6 +297,14 @@ class XCUITestDriver extends BaseDriver {
log.info(`Setting up ${this.isRealDevice() ? 'real device' : 'simulator'}`);

if (this.isSimulator()) {
if (this.opts.shutdownOtherSimulators) {
if (!this.relaxedSecurityEnabled) {
log.errorAndThrow(`Appium server must have relaxed security flag set in order ` +
`for 'shutdownOtherSimulators' capability to work`);
}
await shutdownOtherSimulators(this.opts.device);
}

this.localeConfig = await iosSettings.setLocale(this.opts.device, this.opts, {}, this.isSafari());
await iosSettings.setPreferences(this.opts.device, this.opts, this.isSafari());
// Cleanup of installd cache helps to save disk space while running multiple tests
Expand Down
16 changes: 14 additions & 2 deletions lib/simulator-management.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import path from 'path';
import { getSimulator } from 'appium-ios-simulator';
import { createDevice, getDevices, terminate } from 'node-simctl';
import { createDevice, getDevices, terminate, shutdown } from 'node-simctl';
import { resetXCTestProcesses } from './utils';
import _ from 'lodash';
import log from './logger';
import B from 'bluebird';

// returns sim for desired caps
async function createSim (caps, sessionId) {
Expand Down Expand Up @@ -94,5 +95,16 @@ async function installToSimulator (device, app, bundleId, noReset = true) {
log.debug('The app has been installed successfully.');
}

async function shutdownOtherSimulators (currentDevice) {
const allDevices = await getDevices();
const otherBootedDevices = allDevices.filter((device) => device.udid !== currentDevice.udid && device.state === 'Booted');
if (!otherBootedDevices.length) {
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...`);
await B.each(otherBootedDevices, async (device) => await shutdown(device.udid));
}

export { createSim, getExistingSim, runSimulatorReset, installToSimulator };
export { createSim, getExistingSim, runSimulatorReset, installToSimulator, shutdownOtherSimulators };

0 comments on commit 46d20b5

Please sign in to comment.