Deleting app during XCUITest generates "failed to launch" error

We have the following code that we are using to remove out app in between tests.

let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
springboardApp.activate()

let appIcon = springboard.icons.matching(identifier: "xxxxxx").firstMatch
appIcon.press(forDuration: 1.0)

let removeApp = springboard.buttons["Remove App"]
removeApp.waitForExistance(timeout: 5.0)
removeApp.tap()

let removeAppConfirmDeleteAppButton = springboard.alerts.buttons["Delete App"].firstMatch
removeAppConfirmDeleteAppButton.waitForExistance(timeout: 5.0)
removeAppConfirmDeleteAppButton.tap()

let deleteAppConfirmDeleteButton = springboard.alerts.buttons["Delete"].firstMatch
deleteAppConfirmDeleteButton.waitForExistance(timeout: 5.0)
deleteAppConfirmDeleteButton.tap()

The reason that we are deleting the app is to ensure that tests start from a consistent app state.

We run our tests via fastlane scan. The resulting command looks something like this:

[23:19:30]: $ set -o pipefail && env NSUnbufferedIO=YES xcodebuild -scheme xxxxxTests -project ./xxxxx.xcodeproj -derivedDataPath /Users/vagrant/Library/Developer/Xcode/DerivedData/xxxxx-cjzmgsxnvpzlmjfynrqokkhojhug -destination 'platform=iOS Simulator,id=85C115EF-1FF9-4871-82CC-364D3BAF2098' -resultBundlePath 'build/ui_test_reports/xxxxxTests/xxxxxTests.xcresult' -testPlan 'xxxxxTests' -test-iterations 3 -retry-tests-on-failure test-without-building | tee '/Users/vagrant/git/ios-consumer/build/ui_test_reports/xxxxxTests/xxxxx-xxxxxTests.log' | xcbeautify

The problem is that we get the following error

[10:03:25]: ▸ 2022-12-16 10:03:25.529 xcodebuild[86412:7339751] iOSSimulator: 215F2A17-86F7-4612-9A71-C672FE680813: Failed to launch app with identifier: com.xxxxxx.xxxxxx and options: {
[10:03:25]: ▸   "activate_suspended" = 0;
[10:03:25]: ▸   arguments =   (
[10:03:25]: ▸     "-key1,
[10:03:25]: ▸     true,
[10:03:25]: ▸     "-key2",
[10:03:25]: ▸     "AAAAAAAA",
[10:03:25]: ▸     "-key3",
[10:03:25]: ▸     "BBBBBBBBBB"
[10:03:25]: ▸   );
[10:03:25]: ▸   environment =   {
[10:03:25]: ▸     "CA_ASSERT_MAIN_THREAD_TRANSACTIONS" = 0;
[10:03:25]: ▸     "CA_DEBUG_TRANSACTIONS" = 0;
[10:03:25]: ▸     "DYLD_FRAMEWORK_PATH" = "/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator:/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator/PackageFrameworks";
[10:03:25]: ▸     "DYLD_INSERT_LIBRARIES" = "/Applications/Xcode-14.1.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib";
[10:03:25]: ▸     "DYLD_LIBRARY_PATH" = "/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator";
[10:03:25]: ▸     "LLVM_PROFILE_FILE" = "/var/folders/y2/rp02hkm94x59s7k472g592wh0000gp/T/com.xxxxxx.xxxxxx/94D6D959-1594-4801-9C26-0AD824A89EEB-86412-00000F3C6FAE4044/FB9B0C24-BB22-4EBF-9525-E8A2FED8691A-%p%c.profraw";
[10:03:25]: ▸     "MTC_CRASH_ON_REPORT" = 1;
[10:03:25]: ▸     NSApplicationQuitWithoutSuddenTermination = YES;
[10:03:25]: ▸     NSUnbufferedIO = YES;
[10:03:25]: ▸     "OS_ACTIVITY_MODE" = info;
[10:03:25]: ▸     "__XCODE_BUILT_PRODUCTS_DIR_PATHS" = "/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator";
[10:03:25]: ▸     "__XPC_DYLD_FRAMEWORK_PATH" = "/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator";
[10:03:25]: ▸     "__XPC_DYLD_LIBRARY_PATH" = "/AAAAA/AAAAAAA/Library/Developer/Xcode/DerivedData/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/Build/Products/Debug-iphonesimulator";
[10:03:25]: ▸     "__XPC_LLVM_PROFILE_FILE" = "/var/folders/y2/rp02hkm94x59s7k472g592wh0000gp/T/com.xxxxxx.xxxxxx/94D6D959-1594-4801-9C26-0AD824A89EEB-86412-00000F3C6FAE4044/FB9B0C24-BB22-4EBF-9525-E8A2FED8691A-%p%c.profraw";
[10:03:25]: ▸   };
[10:03:25]: ▸   stderr = "/dev/ttys013";
[10:03:25]: ▸   stdout = "/dev/ttys013";
[10:03:25]: ▸   "terminate_running_process" = 1;
[10:03:25]: ▸   "wait_for_debugger" = 0;
[10:03:25]: ▸ } (error = Error Domain=FBSOpenApplicationServiceErrorDomain Code=4 "The request to open "com.xxxxxx.xxxxxx" failed." UserInfo={NSLocalizedDescription=The request to open "com.xxxxxx.xxxxxx" failed., BSErrorCodeDescription=InvalidRequest, FBSOpenApplicationRequestID=0x87a7, NSUnderlyingError=0x600002a85b00 {Error Domain=FBSOpenApplicationErrorDomain Code=4 "Application info provider (FBSApplicationLibrary) returned nil for "com.xxxxxx.xxxxxx"" UserInfo={BSErrorCodeDescription=NotFound, NSLocalizedFailureReason=Application info provider (FBSApplicationLibrary) returned nil for "com.xxxxxx.xxxxxx"}}})

The error makes it look like the test did not even run, but the tests are running. If I remove the code that deletes the app, the error goes away. I have tried using different approaches to launching our app like using activate() instead of launch(). I've tried putting the delete in the setup and teardown. I've put the delete in the test. I've added delays, before and after the delete, nothing can work around the error.

My first question is what is the thing that is checking for the presence of the app and failing? My second question is there anything I can try that I have not already tried.

Same error. Any workaround?

It seems like the launch app keeps a reference to the app we removed, so it tries to launch it, but the id that test is using does not exist (because the app is removed), so it throws an error and reinstalls the app.

Yes, the test flow ended and all works as expected, but ... an error is shown on the result.

Hey there! Did you find any workaround around this?

There is not a solution from Apple at this time.

There are not currently any workarounds using the xcuitest framework and runner. If you made your own test runner, you might be able to run tests individually and clean up at the end / between test runs.

Some other test frameworks have solutions like that built in.

@jbentonGx and any way to catch the error? I'm using fastlane multi_scan to run my uitests, which has a retry mechanism for the failed tests. The fact that the error described above happened along the batch, makes the multi_scan interpreted everything as failed and loses information around the already passed tests. It assumes a build issue occured, so either fails or I can order to try again but it repeats all the tests again. If this error does not occur only the failing tests are ran again

@silval3 there is not any way that I know of to catch the error. 😢 It is happening in the framework part of running the xcuitests. It would require a fix to the framework by Apple.

Deleting app during XCUITest generates "failed to launch" error
 
 
Q