I had a scenario where pressing a button triggers a loading state which replaces the page, because the button was no longer in the widget tree, the test was getting stuck even though it pressed successfully.
I was able to fix this by duplicating the pressOn action in a new action, and doing the following:
Wrap the final pumpAndSettle with a try catch
Add a one second timeout to the pumpAndSettle
I'm sure this could be cleaner in the actual fix but here is my adjusted code:
@override
Future<bool> execute(Tester tester) async {
final Offset center;
final node = await tester.find(text);
if (node == null) {
return false;
}
center = node.center;
final pointer = _pointerId++;
tester.emitPointerEvent(PointerDownEvent(pointer: pointer, position: center));
await tester.pump(duration: kPressTimeout);
tester.emitPointerEvent(PointerUpEvent(pointer: pointer, position: center));
// Try catch and timeout added here
try {
await tester.pumpAndSettle(timeout: const Duration(milliseconds: 1000));
} catch (e) {}
return true;
}
I had a scenario where pressing a button triggers a loading state which replaces the page, because the button was no longer in the widget tree, the test was getting stuck even though it pressed successfully.
I was able to fix this by duplicating the pressOn action in a new action, and doing the following:
I'm sure this could be cleaner in the actual fix but here is my adjusted code: