Configures Flutter Driver for app interaction and converts MCP actions into permanent integration tests. Use when adding integration testing to a project, exploring UI components via MCP, or automating user flows with the integration_test package.
pubspec.yaml:
flutter pub add 'dev:integration_test:{"sdk":"flutter"}'
flutter pub add 'dev:flutter_test:{"sdk":"flutter"}'
lib/main.dart or a dedicated lib/main_test.dart):
package:flutter_driver/driver_extension.dart.enableFlutterDriverExtension(); before runApp().Key parameters (e.g., ValueKey('login_button')) to critical widgets in the application code to ensure reliable targeting during tests.launch_app with target: "lib/main_test.dart" to start the application and acquire the DTD URI.get_widget_tree to discover available Keys, Text nodes, and widget Types.tap, enter_text, and scroll to simulate user flows.waitFor or verify state with get_health when navigating or triggering animations.SliverList or ListView. Execute scroll or scrollIntoView to force the widget to mount before interacting with it.flutter_test API paradigm.integration_test/ directory at the project root.<name>_test.dart convention.IntegrationTestWidgetsFlutterBinding.ensureInitialized(); at the start of main().await tester.pumpWidget(MyApp());.await tester.pumpAndSettle(); after interactions like tester.tap().expect(find.byKey(ValueKey('foo')), findsOneWidget); or findsNothing.await tester.scrollUntilVisible(itemFinder, 500.0, scrollable: listFinder);.flutter_driver:flutter_driver tests, use driver.waitFor(), driver.waitForAbsent(), driver.tap(), and driver.scroll() instead of the WidgetTester APIs.flutter drive command. Require a host driver script located in test_driver/integration_test.dart that calls integrationDriver().chromedriver --port=4444 in a separate terminal, then run:
flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d chrome-d web-server.flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart.flutter build apk --debug./gradlew app:assembleAndroidTestintegration_test and flutter_test to pubspec.yaml.enableFlutterDriverExtension() into the app entry point.ValueKeys to target widgets.launch_app via MCP.get_widget_tree.tap, enter_text).integration_test/app_test.dart.WidgetTester APIs.test_driver/integration_test.dart with integrationDriver().flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart.PumpAndSettleTimedOutException occurs, check for infinite animations -> If widget not found, add scrollUntilVisible -> Re-run test until passing.integration_test/app_test.dart)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';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('End-to-end test', () {
testWidgets('tap on the floating action button, verify counter', (tester) async {
// Load app widget.
await tester.pumpWidget(const MyApp());
// Verify the counter starts at 0.
expect(find.text('0'), findsOneWidget);
// Find the floating action button to tap on.
final fab = find.byKey(const ValueKey('increment'));
// Emulate a tap on the floating action button.
await tester.tap(fab);
// Trigger a frame and wait for animations.
await tester.pumpAndSettle();
// Verify the counter increments by 1.
expect(find.text('1'), findsOneWidget);
});
});
}
test_driver/integration_test.dart)import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
test_driver/perf_driver.dart)binding.traceAction() to capture performance metrics.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) {
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,
);
}
},
);
}