Sub topic 5
Tapping and Entering Text
Prerequisites
Assume you know basic Dart, async and await, Flutter widgets, and how to run flutter test. You should also know that a widget test uses WidgetTester to build and interact with a widget tree.
A widget test should simulate the actions a user takes. For buttons, use tap. For text fields, use enterText. After an action changes state, call pump so Flutter rebuilds the affected widgets.
Tapping a button
tester.tap sends a tap to the widget matched by a finder. The finder should identify exactly one interactive widget. A key is often the most stable choice when several buttons have similar labels.
Consider this small greeting form:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class GreetingForm extends StatefulWidget {
const GreetingForm({super.key});
@override
State<GreetingForm> createState() => _GreetingFormState();
}
class _GreetingFormState extends State<GreetingForm> {
final _nameController = TextEditingController();
String _greeting = '';
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
key: const Key('name-field'),
controller: _nameController,
),
ElevatedButton(
key: const Key('greet-button'),
onPressed: () {
setState(() {
_greeting = 'Hello, ${_nameController.text}';
});
},
child: const Text('Greet'),
),
Text(_greeting, key: const Key('greeting')),
],
);
}
}
void main() {
testWidgets('greets the entered name', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(body: GreetingForm()),
),
);
await tester.enterText(
find.byKey(const Key('name-field')),
'Ada',
);
await tester.tap(find.byKey(const Key('greet-button')));
await tester.pump();
expect(find.text('Hello, Ada'), findsOneWidget);
});
}enterText replaces the current contents of the target text field. It updates the field's editing value, so the widget can read the new value through its controller or form state.
The button callback calls setState. That schedules a rebuild. pump advances the test environment by a frame and lets the rebuilt tree appear. Without it, an assertion may inspect the old tree.
Choosing a finder
Use the finder that expresses how a user or test should identify the widget:
find.byKeytargets a deliberate, stable test key.find.texttargets visible text.find.byTypetargets a widget class.find.byIcontargets an icon.find.widgetWithTextcombines a widget type and its text.
For example, find.widgetWithText(ElevatedButton, 'Greet') is useful when the button has no key. However, it becomes ambiguous if multiple buttons have the same label.
Interaction flow
For a button that starts disabled, tapping it does not invoke its callback. Test the disabled state separately with a matcher or inspect the button's onPressed property.
Exercise
Why is await tester.pump() needed after tapping the Greet button?
Show answer
The tap invokes setState, which schedules a rebuild. pump advances the test by a frame so the updated greeting is present before the assertion runs.