Skip to content

Instantly share code, notes, and snippets.

@venbrinoDev
Forked from lukepighetti/stupid_simple_testing.dart
Last active November 7, 2023 17:09
Show Gist options
  • Save venbrinoDev/968871142cb4d86659db7953213b0150 to your computer and use it in GitHub Desktop.
Save venbrinoDev/968871142cb4d86659db7953213b0150 to your computer and use it in GitHub Desktop.
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