Sub topic 10
Tapping, entering text and pumping frames
Assume you know basic Dart async and await, can build Flutter widgets, and have run a test with flutter test. This chapter uses widget tests and the WidgetTester object.
A widget test does not display your app in a real device window. Flutter builds widgets in a test environment. Your test then finds widgets, performs actions, and pumps frames so Flutter can process the results.
Build a widget and interact with it
Consider a login-style form with a text field and a button. The button displays the entered name after it is tapped:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class GreetingPage extends StatefulWidget {
const GreetingPage({super.key});
@override
State<GreetingPage> createState() => _GreetingPageState();
}
class _GreetingPageState extends State<GreetingPage> {
final _nameController = TextEditingController();
String _message = 'Enter your name';
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
void _greet() {
setState(() {
_message = 'Hello, ${_nameController.text}!';
});
}
@override
Widget build(BuildContext context) {
return Material(
child: Column(
children: [
Text(_message),
TextField(
controller: _nameController,
key: const Key('nameField'),
),
ElevatedButton(
onPressed: _greet,
child: const Text('Greet'),
),
],
),
);
}
}
void main() {
testWidgets('entering a name displays a greeting', (tester) async {
await tester.pumpWidget(const GreetingPage());
expect(find.text('Enter your name'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('nameField')),
'Ada',
);
await tester.tap(find.text('Greet'));
await tester.pump();
expect(find.text('Hello, Ada!'), findsOneWidget);
});
}pumpWidget builds the initial widget tree. It also pumps a frame, so the first screen is ready for assertions.
enterText replaces the contents of an editable widget. The finder identifies the TextField that should receive the text.
tap sends a tap to the widget found by the finder. Tapping usually schedules a rebuild rather than changing the screen immediately.
pump processes a frame. It lets setState, layout, painting, and other scheduled work take effect. A common interaction pattern is:
- Find a control.
- Perform an action.
- Pump a frame.
- Assert the result.
`pump` and `pumpAndSettle`
Use pump when you want precise control over frames. You can give it a duration to move an animation or timer forward:
await tester.pump(const Duration(milliseconds: 300));Use pumpAndSettle when an action starts animations or several rebuilds and you want to wait until no more frames are scheduled:
await tester.tap(find.text('Open'));
await tester.pumpAndSettle();
expect(find.text('Details'), findsOneWidget);pumpAndSettle can wait indefinitely when an animation repeats forever, a loading spinner never stops, or a timer continually schedules frames. In those cases, pump known durations instead and assert the intermediate state.
| Method | Best use |
|---|---|
| `pumpWidget` | Build the initial screen |
| `pump` | Process one frame or advance a known duration |
| `pumpAndSettle` | Wait for finite animations and queued rebuilds |
Follow the frame sequence
Common interaction errors
- Calling
enterTextwith a finder that matches no editable widget. - Tapping before calling
pumpWidget. - Asserting immediately after
tapwhen the change occurs during the next frame. - Using
find.textfor text that is split across multiple widgets. Use a key, type finder, or a more specific finder when needed. - Calling
pumpAndSettlewhile an intentionally endless animation is running.
Exercise
Why does a test usually call await tester.pump() after await tester.tap(...)?
Show answer
The tap can schedule a state change for a later frame. pump processes that frame so the test can inspect the updated UI.