'How to disable SwiftUI animations in UITests?

I tried to disable animation in UITests with the following code:

let app = XCUIApplication()
app.launchEnvironment = ["DISABLE_ANIMATIONS": "1"]

I also tried:

UIView.setAnimationsEnabled(false)

But it doesn't disable animation when I run UITests on simulator.

Is it because I'm using SwiftUI ?

The animation I want to disable is a view transition from one SwiftUI View to another one. Here is how I coded the transition:

NavigationLink(destination: MapView(), isActive: $viewModel.isDataLoaded) {
     EmptyView()
}

Is there another way to disable animation in UITests when using SwiftUI ?



Solution 1:[1]

It is needed to be done explicitly (by-code) in main application, because UITests run in different process, ie. it should be like

struct YourApp: App {
    init() {
        let env = ProcessInfo.processInfo.environment
        if env["DISABLE_ANIMATIONS"] == "1" {          // << here !!
            UIView.setAnimationsEnabled(false)
        }
    }

    var body: some Scene {
      // ... scene here
    }
}

and then it can be used

let app = XCUIApplication()
app.launchEnvironment = ["DISABLE_ANIMATIONS": "1"]

Tested with Xcode 13.3 / iOS 15.4

backup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1