D
TestingEasy20 XP3 min read

How do you find and interact with widgets in tests?

TL;DR: Use the find object: find.text(), find.byType(), find.byKey(), find.byIcon(), find.ancestor(). Then interact with tester.tap(), tester.enterText(), tester.drag(). Assert with expect(finder, findsOneWidget).

Full Answer

Common Finders

  • ▸find.text('Submit'): by displayed text
  • ▸find.byType(ElevatedButton): by widget type
  • ▸find.byKey(const Key('login-btn')): by Key — most reliable for e2e
  • ▸find.byIcon(Icons.search): by icon data
  • ▸find.descendant(of: find.byType(Card), matching: find.byType(Text)): scoped search
  • ▸find.ancestor(of: find.text('Title'), matching: find.byType(Container))

Matchers

  • ▸findsOneWidget
  • ▸findsNothing
  • ▸findsNWidgets(n)
  • ▸findsWidgets (at least one)

Code Examples

dartCommon find + interact patterns
Output
// Scoped searches prevent false positives from duplicate widgets

Common Mistakes

  • ✗Using find.text() when the widget uses a RichText — use find.textContaining() or find.byWidgetPredicate
  • ✗Not pumping after enterText() — text field hasn't rebuilt yet

Interview Tip

💡

Mention that Keys are the most stable way to locate widgets in large trees — text labels change but keys are intentional. Add Key('submit-btn') for test-critical widgets.

#find#matchers#widget-tester#finders