diff --git a/lib/devices/ios/ios.js b/lib/devices/ios/ios.js index 5b6041f3b..4f5d166e1 100644 --- a/lib/devices/ios/ios.js +++ b/lib/devices/ios/ios.js @@ -622,27 +622,45 @@ IOS.prototype.setDeviceAndLaunchSimulator = function(cb) { cb(new Error(msg)); } else { - this.endSimulator(function(err) { - if (err) return cb(err); + var cleanup = function(cb) { + if (this.reset) { + this.cleanupAppState(cb); + } else { + cb(); + } + }.bind(this); + + var setDeviceInfo = function(cb) { var iosDeviceString = this.getDeviceString(); var isiPhone = iosDeviceString.toLowerCase().indexOf("ipad") === -1; - var iosSimArgs = ["-SimulateDevice", iosDeviceString]; logger.debug("Launching device: " + iosDeviceString); - this.setDeviceTypeInInfoPlist(isiPhone ? 1 : 2, function() { - this.iosSimProcess = spawn(iosSimPath, iosSimArgs); - var waitForSimulatorLogs = function(countdown) { - if (countdown <= 0 || this.logs.syslog.getAllLogs().length > 0 || this.logs.crashlog.getAllLogs().length > 0) { - logger.info(countdown > 0 ? "Simulator is now ready." : "Waited 10 seconds for simulator to start."); - cb(); - } else { - setTimeout(function() { - waitForSimulatorLogs(countdown-1); - } ,1000); - } - }.bind(this); - waitForSimulatorLogs(10); - }.bind(this)); - }.bind(this)); + this.setDeviceTypeInInfoPlist(isiPhone ? 1 : 2, cb); + }.bind(this); + + var startSim = function(cb) { + var iosSimArgs = ["-SimulateDevice", this.getDeviceString()]; + this.iosSimProcess = spawn(iosSimPath, iosSimArgs); + var waitForSimulatorLogs = function(countdown) { + if (countdown <= 0 || this.logs.syslog.getAllLogs().length > 0 || this.logs.crashlog.getAllLogs().length > 0) { + logger.info(countdown > 0 ? "Simulator is now ready." : "Waited 10 seconds for simulator to start."); + cb(); + } else { + setTimeout(function() { + waitForSimulatorLogs(countdown-1); + } ,1000); + } + }.bind(this); + waitForSimulatorLogs(10); + }.bind(this); + + async.series([ + this.endSimulator.bind(this), + cleanup, + setDeviceInfo, + startSim + ], function(err) { + cb(err); + }); } } };