Tap() in widget test showing warning in flutter

Viewed 2628
await widgetTester.tap(find.byType(ElevatedButton));

shows warning : Maybe the widget is actually off-screen, or another widget is obscuring it, or the widget cannot receive pointer events.

3 Answers

Try this:

await widgetTester.ensureVisible(find.byType(ElevatedButton));
await widgetTester.pumpAndSettle();
await widgetTester.tap(find.byType(ElevatedButton));

I suggest creating a safeTapBy function logic. This just ensures that the button is visible (keyboard showing, off screen etc). Here is an example using find.byKey:

Future safeTapByKey(WidgetTester tester, String key) async {
  await tester.ensureVisible(find.byKey(Key(key)));
  await tester.pumpAndSettle();
  await tester.tap(find.byKey(Key(key)));
}

I just replace await tester.pump(); by await tester.pumpAndSettle(); before tap

It works for me.

Related