Sub topic 5
Finding widgets and simulating user input
This chapter assumes that you can write a basic Flutter widget test and understand widgets, keys, and the widget tree. An integration test uses the same WidgetTester API, but it drives your complete application on a device or emulator. The test should interact with the app as a user would: find a visible control, tap it, enter text, and check the resulting screen.
Find widgets with finders
A finder is an object that identifies widgets in the widget tree. The most reliable finder usually uses a key that you deliberately add for testing.
await tester.tap(find.byKey(const Key('increment')));The expression find.byKey searches for the widget with the key increment. Add keys to important controls in the application:
ElevatedButton(
key: const Key('increment'),
onPressed: _increment,
child: const Text('Increment'),
)Other useful finders include:
| Finder | Use |
|---|---|
| `find.byKey` | Find a widget with a stable key |
| `find.text` | Find text currently visible on screen |
| `find.byType` | Find a widget by its Dart type |
| `find.widgetWithText` | Find a widget that contains specific text |
Keys are usually safer than text or widget types. Text can change because of localization or copy edits. A screen can also contain several widgets of the same type. Give a key to a control when the test needs to identify it consistently.
You can verify how many widgets matched a finder:
expect(find.byKey(const Key('increment')), findsOneWidget);Useful matchers include findsOneWidget, findsNothing, and findsNWidgets(2). An assertion about the match count makes a test fail clearly when the screen structure is different from what the test expects.
Simulate taps and text entry
After finding a widget, use WidgetTester to perform an interaction. Common actions include:
tappresses a button, link, or other tappable widget.enterTextreplaces the contents of a text field.flingperforms a fast drag, often to scroll a list.longPressholds a pointer on a widget.dragmoves a pointer by a specified distance.
This test launches the real app, enters an email address, submits a form, and checks the next screen:
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() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('login form accepts input', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('email_field')),
'[email protected]',
);
await tester.tap(find.byKey(const Key('submit_button')));
await tester.pumpAndSettle();
expect(find.text('Welcome'), findsOneWidget);
});
}enterText focuses the field and inserts the supplied value. It does not prove that a real keyboard can produce every possible input method, so use a physical-device test for keyboard-specific behavior when that behavior matters.
Understand the interaction flow
A test normally follows this sequence:
A finder does not perform an action by itself. find.byKey(...) only describes a target. The tap or enterText call performs the interaction. If the finder matches no widget, the test fails. If it matches multiple widgets, the action may fail because the target is ambiguous.
Use explicit keys for repeated or dynamic controls. For example, a list of products can assign a key based on a stable product identifier:
ListTile(
key: Key('product_$productId'),
title: Text(productName),
onTap: () => openProduct(productId),
)Avoid keys based on list indexes when items can be inserted, removed, or reordered. An index can identify a different product on the next test run.
Check the result after interaction
A successful tap may start navigation, an animation, or an asynchronous request. Call pump when you want to advance a known amount of simulated time. Call pumpAndSettle when you want Flutter to process frames until no scheduled frames remain.
Use a specific assertion about the user-visible result:
expect(find.byKey(const Key('checkout_screen')), findsOneWidget);
expect(find.text('Order placed'), findsOneWidget);Do not assert only that the button exists after tapping it. That proves the starting screen was present, not that the user flow worked. Keep test data controlled as well. A fixed account, local fake service, or predictable fixture prevents a changing backend response from changing the result.
Exercise
Why is find.byKey usually a better choice than find.text for submitting a login form?
Show answer
A stable key identifies the intended control even when its label changes because of copy edits, localization, or accessibility requirements.