While running my UI tests for a UI refactor, I noticed that two of them failed repeatedly.

I wasn’t sure what was going wrong until I remembered replacing the normal buttons with a conditional view that changed the text.

And I added a subtle animation when the condition changes.

The tests finish before the animation is completed, and the text isn’t changed, leading to the test being failed.

func testNextButton() {
  let evaluateButton = app.buttons["evaluate"]
  /// Cannot find the "next" text
  let nextButton = app.buttons["next"]

The solution to this was disabling both normal and core animations.

I added a launch argument to detect that UI tests are running, and disable the animation accordingly.

struct LaunchArguments {
  static let isRunningUITests = "isRunningUITests"

In the test class, I appended the launch argument before the app launches:

class Chroma_GameUITests: XCTestCase {
  let app = XCUIApplication()

  override func setUpWithError() throws {
    continueAfterFailure = false

And check for this launch argument in the App:

extension ChromaApp {
  private func checkForUITests() {
    if ProcessInfo.processInfo.arguments.contains(LaunchArguments.isRunningUITests) {
      if let scene = UIApplication.shared.connectedScenes.first,
         let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
         let window = windowSceneDelegate.window {
        // Disable Core Animations
        window?.layer.speed = 0
      // Disable UIView animations

Running Tests Repeatedly

In Xcode 13, there’s a new feature where you can right-click the diamond icon and run the tests repeatedly:

Tests options after clicking the diamond button

Then, you can configure the maximum repetitions, when the test stops, and if you want to relaunch the test after each repetition:

Options for repeating the tests

To be 100% sure, I ran the conflicting test 100 times, both before disabling animations and afterward. It always fails when the animation isn’t disabled and passes when the animation is disabled.

This solves the problem I initially didn’t have but was created by adding that subtle animation on the button.

If you have got a better approach, then tag me on @rudrankriyam on Twitter!

Thanks for reading!