Sub topic 3
Finding Widgets with Finders
This chapter assumes basic Dart, Flutter widgets, testWidgets, and WidgetTester. You should also know how to run flutter test.
A finder is an object that describes which widgets to locate in the widget tree. The find object provides common finder methods. A finder does not check whether a widget exists by itself. Pass it to a matcher such as findsOneWidget or findsNothing.
Choosing a finder
Use the most specific finder that expresses the behavior you want to test.
| Finder | Best for |
|---|---|
| `find.text` | Visible text |
| `find.byType` | A widget class |
| `find.byKey` | A stable identifier you assign |
| `find.byIcon` | An icon |
| `find.widgetWithText` | A widget containing specific text |
| `find.descendant` | A widget inside another widget |
find.text searches for Text widgets with matching data. It is useful when the text is part of the user-visible behavior.
find.byType searches by Dart widget type. It can be too broad when several widgets share the same type.
find.byKey searches for a key. Keys make tests less dependent on layout or displayed wording:
find.byKey(const Key('save-button'))Keys should describe the widget's role, not its position, such as first-button.
Combining finders
When one finder matches too many widgets, narrow the search. find.widgetWithText combines a widget type and text. find.descendant restricts a search to a particular part of the tree.
A worked example
This test uses several finder types against one small screen:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class ProductCard extends StatelessWidget {
const ProductCard({super.key});
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: [
const Text('Notebook'),
ElevatedButton(
key: const Key('save-button'),
onPressed: () {},
child: const Text('Save'),
),
],
),
);
}
}
void main() {
testWidgets('locates product-card widgets precisely', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Products')),
body: ProductCard(),
floatingActionButton: FloatingActionButton(
onPressed: null,
child: const Icon(Icons.add),
),
),
),
);
expect(find.text('Notebook'), findsOneWidget);
expect(find.byType(ProductCard), findsOneWidget);
expect(find.byKey(const Key('save-button')), findsOneWidget);
expect(find.byIcon(Icons.add), findsOneWidget);
expect(
find.widgetWithText(ElevatedButton, 'Save'),
findsOneWidget,
);
expect(
find.descendant(
of: find.byType(Card),
matching: find.text('Notebook'),
),
findsOneWidget,
);
});
}findsOneWidget asserts that exactly one widget matches. Other useful matchers include findsNothing, findsNWidgets(2), and findsWidgets, which accepts one or more matches.
Avoiding fragile tests
A test can pass while targeting the wrong widget if a finder is too broad. For example, find.byType(Text) may match a title, label, and error message. Prefer a semantic text finder, a key, or a scoped descendant finder.
Use keys for controls whose visible text may change because of localization. Use text finders when the exact text is itself important to the user experience.
> callout tip > When a test reports multiple matches, inspect the widget tree and make the finder more specific. Do not immediately replace the matcher with findsWidgets; that can hide a real ambiguity.
Exercise
Which finder should you use to locate the text “Settings” only inside an AppBar?
Show answer
Use find.descendant(of: find.byType(AppBar), matching: find.text('Settings')).