Created
April 8, 2023 00:49
-
-
Save lukepighetti/daaa2cd4bd83a4ddd33ba73b1188da03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@KoalityJustin Justin took this concept further, it can be found here: https://gist.github.com/KoalityJustin/910c9a5dd1ddb43c34de9770f4dac11f