diff --git a/code-push-plugin-testing-framework/script/platform.js b/code-push-plugin-testing-framework/script/platform.js index 64bc3003..4a4ff4b8 100644 --- a/code-push-plugin-testing-framework/script/platform.js +++ b/code-push-plugin-testing-framework/script/platform.js @@ -406,8 +406,13 @@ var IOSEmulatorManager = (function () { /** * Ends a running application given its app id. */ - IOSEmulatorManager.prototype.endRunningApplication = function (appId) { - return testUtil_1.TestUtil.getProcessOutput("xcrun simctl terminate booted " + appId, undefined).then(function () { return null; }) + IOSEmulatorManager.prototype.endRunningApplication = async function (appId) { + const isAppRunning = await isAppRunningOnIOS(appId); + if (!isAppRunning) { + return null; + } + await testUtil_1.TestUtil.getProcessOutput("xcrun simctl terminate booted " + appId, undefined); + return null; }; /** * Restarts an already installed application by app id. @@ -466,6 +471,18 @@ var IOSEmulatorManager = (function () { }()); exports.IOSEmulatorManager = IOSEmulatorManager; +/** + * Returns whether the app is currently running on the booted simulator. + * + * "xcrun simctl terminate" fails when the app is not running, and it reports that the same way it reports + * a genuinely broken simulator, so the running state has to be established up front instead. Running apps + * appear in launchctl's job list as "UIKitApplication:[...]" with a live pid. + */ +async function isAppRunningOnIOS(appId) { + const output = await testUtil_1.TestUtil.getProcessOutput("xcrun simctl spawn booted launchctl list", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true }); + return output.includes(appId); +} + function commandWithCheckAppExistence(command, appId) { return testUtil_1.TestUtil.getProcessOutput("adb shell pm list packages", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true }) .then((output) => { diff --git a/code-push-plugin-testing-framework/script/testBuilder.js b/code-push-plugin-testing-framework/script/testBuilder.js index 8b324eb5..286be44f 100644 --- a/code-push-plugin-testing-framework/script/testBuilder.js +++ b/code-push-plugin-testing-framework/script/testBuilder.js @@ -34,7 +34,11 @@ function describeInternal(func, description, spec, scenarioPath) { ServerUtil.testMessageResponse = undefined; }); beforeEach(function () { - return TestContext.targetPlatform.getEmulatorManager().prepareEmulatorForTest(TestConfig.TestNamespace); + return TestContext.targetPlatform.getEmulatorManager().prepareEmulatorForTest(TestConfig.TestNamespace) + .catch(function (error) { + console.error("Failed to prepare emulator/simulator for the test."); + throw error; + }); }); if (scenarioPath) { before(function () {