Repository: flutter
Issue: Unable to complete any iOS UI tests that lasts longer than ~6 minutes
Labels: platform-ios,framework,f: integration_test,has reproducible steps,P2,team-ios,triaged-ios,found in release: 3.19,found in release: 3.21

### Steps to reproduce

Add the following to `pubspec.yaml`:
```
  integration_test:
    sdk: flutter
```

Setup project for UI testing:
https://github.com/flutter/flutter/tree/main/packages/integration_test#ios-device-testing

Add a UI test that takes longer than 6 minutes to run. I provided a code sample for this.

**TEST CASE:**
Use **Xcode** to run the UI tests on a **physical** device.

The test will run for a while but exit after a certain time before completion. In my test cases it always happened around 5 minutes and 43 seconds.

My tests consist of a simple loop of pressing a button and increasing the count with `await Future.delayed(const Duration(seconds: 1));` within the loop. 
But our production tests show the same result. Unrelated to what tests run and in what order it always interrupts around that time mark.

**Notes:**
- It works on older Xcode versions (14.2 for example)
- It works when running through Flutter with debug or run from vscode
- It works on a simulator from Xcode or Flutter in vscode
- It works on Xcode 15 (or later) while using Swift for a simple UI test with a similar setup. 
- It **does not** work on Xcode 15 (or later) from a Flutter project on a physical device
That's why I think it's Flutter related.

### Expected results

Test completes after 600 prints.

### Actual results

Test fails after ~330 prints (somewhere before 6 minutes)

### Code sample

<details open><summary>Code sample</summary>

`main.dart` (default generated file, comments removed)
```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

```

Add custom UI test file `widget_test.dart` in /integration_test with the following content:
```dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:bug/main.dart';

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(const MyApp());

    // Verify that our counter starts at 0.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    // Verify that our counter has incremented.
    for (var i = 0; i < 600; i++) {
      await tester.tap(find.byIcon(Icons.add));
      await tester.pump();
      debugPrint("${i + 1}");

      await Future.delayed(const Duration(seconds: 1));

      expect(find.text((i + 1).toString()), findsOneWidget);
    }
  });
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

This is the output in text from `.xcresult`

```console
Runner (3702) encountered an error
Test runner never began executing tests after launching
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.2.1 23C71 darwin-arm64, locale en-NL)
    • Flutter version 3.19.3 on channel stable at /Users/myname/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (7 days ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/myname/Library/Android/sdk
    • Platform android-34, build-tools 33.0.2
    • ANDROID_HOME = /Users/myname/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (4 available)
    • MartiniPhone Pro (mobile) • _hidden_            • ios            • iOS 17.4 21E219
    • iPhone 15 Pro (mobile)    • 97C25645-A5DD-4BAC-8646-2AFED3D23A94 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator)
    • macOS (desktop)           • macos                                • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)              • chrome                               • web-javascript • Google Chrome 122.0.6261.129

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>