Built-in Flutter Widget Finders
Tags: flutter
Published: 2021-12-07 14:33:13

---
title: Built-in Flutter Widget Finders
published: true
description: Overview of available default options for querying widgets in widget tests
tags: flutter
//cover_image: https://direct_url_to_image.jpg
---

This blog post provides an overview of the options for querying widgets in widget tests which are available by default in the [flutter_test](https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html) library.

--------

## Table of contents

- [Identifier](#identifier)
  * [Find by key](#find-by-key)
- [Text](#text)
  * [Find by text](#find-by-text)
  * [Find by rich text](#find-by-rich-text)
  * [Find by partial text](#find-by-partial-text)
  * [Find by matching text](#find-by-matching-text)
- [Type](#type)
  * [Find by class name](#find-by-class-name)
  * [Find by icon](#find-by-icon)
  * [Find by element type](#find-by-element-type)
- [Hierarchy](#hierarchy)
  * [Find by descendant](#find-by-descendant)
  * [Find by ancestor](#find-by-ancestor)
- [Miscellaneous](#miscellaneous)
  * [Find by semantics label](#find-by-semantics-label)
  * [Find by image](#find-by-image)
  * [Find by tooltip](#find-by-tooltip)
  * [Find by widget predicate](#find-by-widget-predicate)
  * [Find by element predicate](#find-by-element-predicate)
- [References](#references)
- [Credits](#credits)

--------

## Identifier

### Find by key

```dart
find.byKey(const ValueKey('continue'))
```

## Text

### Find by text

```dart
find.text('Back')
```

### Find by rich text

```dart
find.text('Close', findRichText: true)
```

### Find by partial text

```dart
find.textContain('Back')
```

### Find by matching text

```dart
find.textContain(RegExp(r'(\w+)'))
```

## Type

### Find by class name

```dart
find.byType(IconButton)
```

### Find by icon

```dart
find.byIcon(Icons.inbox)
```

### Find by element type

```dart
find.byElementType(SingleChildRenderObjectElement)
```

## Hierarchy

### Find by descendant

```dart
find.descendant(
  of: find.byType(TextField),
  matching: find.text('Key 1'),
)
```

### Find by ancestor

```dart
find.ancestor(
  of: find.text('faded'),
  matching: find.byType('Opacity'),
)
```

## Miscellaneous

### Find by semantics label

```dart
find.bySemanticsLabel('Back')
```

### Find by image

```dart
find.image(FileImage(File(filePath)))
```

### Find by tooltip

```dart
find.byTooltip('Back')
```

### Find by widget predicate

```dart
find.byWidgetPredicate(
 (Widget widget) => widget is Tooltip && widget.message == 'Back',
 description: 'widget with tooltip "Back"',
)
```

### Find by element predicate

```dart
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](https://docs.flutter.dev/cookbook/testing/widget/finders)
- [A Deep Dive Into Widget Testing in Flutter: Part II (Finder and WidgetTester) | Deven Joshi | medium.com](https://medium.com/flutter-community/a-deep-dive-into-widget-testing-in-flutter-part-ii-finder-and-widgettester-f76f98b87a90)
- [CommonFinders | api.flutter.dev](https://api.flutter.dev/flutter/flutter_test/CommonFinders-class.html)
- [Finder | api.flutter.dev](https://api.flutter.dev/flutter/flutter_test/Finder-class.html)

## Credits

The source code snippets are adapted from the examples in the [Flutter](https://github.com/flutter/flutter/) source code which is licensed under the [BSD-3-Clause License](https://github.com/flutter/flutter/blob/master/LICENSE).