This blog post provides an overview of the options for querying widgets in widget tests which are available by default in the flutter_test library.
Table of contents
Identifier
Find by key
find.byKey(const ValueKey('continue'))
Text
Find by text
find.text('Back')
Find by rich text
find.text('Close', findRichText: true)
Find by partial text
find.textContain('Back')
Find by matching text
find.textContain(RegExp(r'(\w+)'))
Type
Find by class name
find.byType(IconButton)
Find by icon
find.byIcon(Icons.inbox)
Find by element type
find.byElementType(SingleChildRenderObjectElement)
Hierarchy
Find by descendant
find.descendant(
of: find.byType(TextField),
matching: find.text('Key 1'),
)
Find by ancestor
find.ancestor(
of: find.text('faded'),
matching: find.byType('Opacity'),
)
Miscellaneous
Find by semantics label
find.bySemanticsLabel('Back')
Find by image
find.image(FileImage(File(filePath)))
Find by tooltip
find.byTooltip('Back')
Find by widget predicate
find.byWidgetPredicate(
(Widget widget) => widget is Tooltip && widget.message == 'Back',
description: 'widget with tooltip "Back"',
)
Find by element predicate
find.byElementPredicate(
// finds elements of type SingleChildRenderObjectElement, including
// those that are actually subclasses of that type.
// (contrast with byElementType, which only returns exact matches)
(Element element) => element is SingleChildRenderObjectElement,
description: '$SingleChildRenderObjectElement element',
)
References
- Find widgets | docs.flutter.dev
- A Deep Dive Into Widget Testing in Flutter: Part II (Finder and WidgetTester) | Deven Joshi | medium.com
- CommonFinders | api.flutter.dev
- Finder | api.flutter.dev
Credits
The source code snippets are adapted from the examples in the Flutter source code which is licensed under the BSD-3-Clause License.
Top comments (0)