Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save herveGuigoz/e890c8c80e7bc3a71e6c8b06b18fa2d2 to your computer and use it in GitHub Desktop.

Select an option

Save herveGuigoz/e890c8c80e7bc3a71e6c8b06b18fa2d2 to your computer and use it in GitHub Desktop.

Revisions

  1. @lukepighetti lukepighetti created this gist Apr 8, 2023.
    73 changes: 73 additions & 0 deletions stupid_simple_testing.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@

    extension on WidgetTester {
    Future<void> launchApp() async {
    await app.main();
    await pumpAndSettle();
    }

    Future<void> clearState() async {
    await SharedPreferences.getInstance().then((it) => it.clear());
    }

    Future<void> tapFinder(Finder finder,
    {int index = 0, bool scrollTo = false}) async {
    expect(finder, findsWidgets);
    final maxIndex = min(index, finder.evaluate().length - 1);
    final honedFinder = finder.at(maxIndex);
    if (scrollTo) await ensureVisible(honedFinder);
    await tap(honedFinder, warnIfMissed: false);
    await pumpAndSettle();
    }

    Future<void> tapText(String text,
    {int index = 0, bool scrollTo = false}) async {
    final finder = find.text(text);
    await tapFinder(finder, index: index, scrollTo: scrollTo);
    }

    Future<void> tapIcon(IconData icon,
    {int index = 0, bool scrollTo = false}) async {
    final finder = find.byIcon(icon);
    await tapFinder(finder, index: index, scrollTo: scrollTo);
    }

    Future<void> tapBackButton() async {
    final finder = find.byType(BackButton);
    await tapFinder(finder);
    }

    Future<void> wait(int ms) async {
    await Future.delayed(Duration(milliseconds: ms));
    }

    Future<void> waitForText(
    String text, {
    Duration interval = const Duration(milliseconds: 50),
    Duration timeout = const Duration(seconds: 10),
    }) async {
    var stopwatch = Stopwatch()..start();

    while (stopwatch.elapsed < timeout) {
    await Future.delayed(interval);
    await pump();
    final finder = find.text(text);
    final foundSomething = finder.evaluate().isNotEmpty;
    if (foundSomething) {
    expect(finder, findsWidgets);
    stopwatch.stop();
    return;
    }
    }

    final finder = find.text(text);
    expect(finder, findsWidgets);
    }

    Future<void> input(String text, {int index = 0}) async {
    final finder = find.byType(EditableText);
    expect(finder, findsWidgets);
    final maxIndex = max(index, finder.evaluate().length - 1);
    await enterText(finder.at(maxIndex), text);
    await pumpAndSettle();
    }
    }