simulator.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import _ from 'lodash';
  2. import { Simctl } from 'node-simctl';
  3. import { retryInterval } from 'asyncbox';
  4. import { killAllSimulators as simKill } from 'appium-ios-simulator';
  5. import { resetTestProcesses } from '../../../lib/utils';
  6. async function killAllSimulators () {
  7. if (process.env.CLOUD) {
  8. return;
  9. }
  10. const simctl = new Simctl();
  11. const allDevices = _.flatMap(_.values(await simctl.getDevices()));
  12. const bootedDevices = allDevices.filter((device) => device.state === 'Booted');
  13. for (const {udid} of bootedDevices) {
  14. // It is necessary to stop the corresponding xcodebuild process before killing
  15. // the simulator, otherwise it will be automatically restarted
  16. await resetTestProcesses(udid, true);
  17. simctl.udid = udid;
  18. await simctl.shutdownDevice();
  19. }
  20. await simKill();
  21. }
  22. async function shutdownSimulator (device) {
  23. // stop XCTest processes if running to avoid unexpected side effects
  24. await resetTestProcesses(device.udid, true);
  25. await device.shutdown();
  26. }
  27. async function deleteDeviceWithRetry (udid) {
  28. const simctl = new Simctl({udid});
  29. try {
  30. await retryInterval(10, 1000, simctl.deleteDevice.bind(simctl));
  31. } catch {}
  32. }
  33. export { killAllSimulators, shutdownSimulator, deleteDeviceWithRetry };