How to disable SwiftUI animations in UITests?
Asked Answered
C

1

15

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 ?

Coronary answered 12/3, 2020 at 17:12 Comment(0)
O
3

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

Onlybegotten answered 10/5, 2022 at 17:30 Comment(5)
Correct answer. The Asker should mark it as accepted.Cletacleti
This does not work for SwiftUI-native animations, unfortunately: Neither UIView.setAnimationsEnabled(false) nor window.layer.speed = 100 seem to have any effect on animations that use withAnimation. I haven't found a solution yet that really covers SwiftUI and not only the parts that are backed by UIKit.Haematoxylin
I have tried transaction.disablesAnimations = true on the top level ContentView(), but it does not propagate all the way down the view hierarchy, either.Mitzvah
@DanielSeither did you eventually find a solution to this?Substantive
No, unfortunately not, but I'd still be interested in any approaches that change the animation speed or switch off animations globally.Haematoxylin

© 2022 - 2024 — McMap. All rights reserved.