Sub topic 10
Using flutter drive and a test driver
This chapter assumes you can already write and run an integration_test file with flutter test integration_test/app_test.dart -d <device_id>. It also assumes you know what IntegrationTestWidgetsFlutterBinding does and how pumpAndSettle works. Here you will learn what flutter drive adds, how the driver file works, and when you need it.
Why a second way to run tests exists
On Android, iOS and desktop, flutter test integration_test/app_test.dart -d <device_id> builds the app, installs it, runs the tests and reports the result. For many projects that is all you need.
flutter drive exists for cases where that single command is not enough:
- Web. Browsers cannot be launched and controlled by
flutter testthe same way a phone can. Web integration tests must go throughflutter driveand a browser driver such as ChromeDriver. - Performance data. A timeline recorded with
traceActionhas to be transferred from the app to a process that can write it to disk. The driver is that process. - Custom post-processing. Screenshots, response data and other artifacts can be handled in your own Dart code on the host machine.
The test code inside the app is the same in both cases. Only the way it is launched and observed differs.
Two programs, two places
flutter drive runs two Dart programs:
- The target is your integration test file, for example
integration_test/app_test.dart. It is compiled into the app and runs on the device, emulator, or browser. - The driver is a small file, usually
test_driver/integration_test.dart. It runs on your development machine (the host) in the normal Dart VM. It connects to the app, waits for the tests to finish, and reports the outcome.
The driver never taps buttons or finds widgets. It only collects results. This is different from the older flutter_driver style, where the driver script sent every tap to the app. With integration_test, the interaction code lives in the target.
The minimal driver
The driver file is only a few lines. Create test_driver/integration_test.dart:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();integrationDriver() connects to the running app, waits until all tests in the target have completed, prints the results, and exits with a nonzero code if any test failed. That exit code is what makes it useful in CI, because the pipeline step fails when a test fails.
The file name is a convention. You can place drivers anywhere and name them anything, as long as you pass the path with --driver. Many teams keep one driver and reuse it for every target.
Running a test through the driver
The command takes the driver, the target and a device:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d <device_id>Get the device id from flutter devices. The directory layout looks like this:
my_app/
lib/
integration_test/
app_test.dart
test_driver/
integration_test.dart
pubspec.yamlBoth flags matter. If you leave out --driver, Flutter looks for a default driver file based on the target name, and the error you get when it is missing can be confusing. Always pass both explicitly.
Android, iOS and desktop
On these targets, flutter drive builds the app in debug mode by default, installs it on the device and runs the target. Add --profile or --release when you need realistic performance numbers (see below). Debug builds are much slower than profile builds, so timings from them are not meaningful.
Web
Web needs a WebDriver server. For Chrome, install a ChromeDriver version that matches your Chrome, start it, then run drive against the web device:
chromedriver --port=4444In a second terminal:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d web-serverUsing -d web-server serves the app without opening a visible browser window itself, and the driver connects through ChromeDriver. To watch the browser, you can use -d chrome instead. If the versions of Chrome and ChromeDriver do not match, the session fails to start. Check that first when a web run fails before any test runs.
Collecting performance timelines
In the previous chapter outline you saw that traceAction records a timeline while an interaction runs. Here is the target side again:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('scrolling performance', (tester) async {
app.main();
await tester.pumpAndSettle();
final list = find.byType(Scrollable).first;
await binding.traceAction(() async {
await tester.fling(list, const Offset(0, -500), 10000);
await tester.pumpAndSettle();
}, reportKey: 'scrolling_timeline');
});
}The reportKey names the recorded data. The target stores it in the binding's report data, and the app sends it to the driver when the tests finish. With the minimal integrationDriver() the data arrives, but nothing writes it to a file. To save it, pass a responseDataCallback. This also requires flutter_driver as an SDK dev dependency, because the timeline classes live there:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
flutter_driver:
sdk: flutterThen change the driver:
import 'package:flutter_driver/flutter_driver.dart' as driver;
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() {
return integrationDriver(
responseDataCallback: (data) async {
if (data == null) return;
final timeline = driver.Timeline.fromJson(
data['scrolling_timeline'] as Map<String, dynamic>,
);
final summary = driver.TimelineSummary.summarize(timeline);
await summary.writeTimelineToFile(
'scrolling_timeline',
pretty: true,
includeSummary: true,
);
},
);
}The key 'scrolling_timeline' in data[...] must match the reportKey in the test. Run it in profile mode:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
--profile \
-d <device_id>When the run finishes, look in the build/ directory. You should find scrolling_timeline.timeline.json and scrolling_timeline.timeline_summary.json. The summary file includes values such as average and worst frame build time and rasterizer time, and the count of missed frames. The raw timeline can be opened in a trace viewer such as the Chrome tracing UI or Perfetto.
If you record several timelines in one run, each needs a different reportKey. The callback receives a map with all of them, so loop over the entries and write one file per key.
Choosing between flutter test and flutter drive
| Situation | Use |
|---|---|
| Android, iOS or desktop, pass/fail result only | `flutter test integration_test/...` |
| Web | `flutter drive` with ChromeDriver |
| Saving timelines or other report data to disk | `flutter drive` with a custom `responseDataCallback` |
| Needing a custom host-side step after tests | `flutter drive` |
| Fast local iteration | `flutter test`, which has fewer moving parts |
A reasonable rule: start with flutter test, and move a test to flutter drive only when you need one of the rows above. Keeping performance tests in separate files lets you run them through drive while functional tests stay on the simpler command.
Common errors
- Driver file not found. The path in
--driveris relative to the project root. Run the command from the directory that containspubspec.yaml. - Timeline data is
nullor the key is missing. ThereportKeyin the test and the key you read in the callback do not match, or notraceActionran because the test failed earlier. flutter_drivernot found. Add it as an SDK dev dependency as shown above, then runflutter pub get.- Web session fails to start. ChromeDriver is not running on port 4444, or its version does not match the installed Chrome.
- Unrealistic frame times. The run was in debug mode, on an emulator, or on a device that was throttled by heat or low battery.
- Tests pass locally but the CI job hangs. The device is not connected, is locked, or shows a system dialog. The driver waits for an app that never reports back. Set a job timeout so a hang fails the build instead of stalling it.
Summary of the workflow
- Write the target in
integration_test/as usual. - Add
test_driver/integration_test.dartwithintegrationDriver(). - Run
flutter drivewith--driver,--targetand-d. - For performance, add
flutter_driver, wrap the interaction intraceAction, handle the data inresponseDataCallback, and use--profile. - For web, start ChromeDriver first.
Exercise
A teammate records a scroll timeline with binding.traceAction(..., reportKey: 'list_scroll') and runs flutter drive with the one-line integrationDriver() driver. The tests pass, but no JSON files appear in build/. What two changes are needed to get a timeline summary file, and which run flag gives meaningful numbers?
Show answer
The driver must pass a responseDataCallback to integrationDriver. That callback reads data['list_scroll'] (the same key as the reportKey), builds a Timeline with Timeline.fromJson, summarizes it with TimelineSummary.summarize, and calls writeTimelineToFile. This needs flutter_driver added as an SDK dev dependency in pubspec.yaml. For meaningful numbers, run on a real device with --profile, not in debug mode.
Exercise
You want to run your existing integration test in Chrome. List the tools and commands you need, and say why flutter test integration_test/app_test.dart -d chrome is not the approach to use.
Show answer
Start ChromeDriver (matching your Chrome version) with chromedriver --port=4444, create test_driver/integration_test.dart containing integrationDriver(), then run flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d web-server (or -d chrome). Web integration tests need the host-side driver and a WebDriver server to control the browser, so they go through flutter drive rather than plain flutter test.