scrollUntilVisibleFinder method
Implementation
Future<void> scrollUntilVisibleFinder(
Finder finder, {
String? scrollableId,
int? timeoutMs,
}) async {
final effectiveTimeout =
timeoutMs ?? config.defaultWaitTimeout.inMilliseconds;
final scrollable = await scrollableWaiter.wait(
scrollableId: scrollableId,
timeoutMs: effectiveTimeout,
);
// Flutter's scrollUntilVisible always performs up to 50 drags, even when
// the target is absent and the scrollable has already reached its extent.
// Apart from ignoring the caller's timeout, that produces a warning for
// every drag when the viewport is covered or cannot receive a gesture.
// Drive the scrollable only while it is making progress, then report the
// actual unresolved target instead of leaking StateError: No element.
await tester.pump(Duration.zero);
var attempts = 0;
final maxAttempts = (effectiveTimeout ~/ 50).clamp(1, 50);
final scrollableState = tester.state<ScrollableState>(scrollable);
while (attempts < maxAttempts) {
if (assertions.hasVisibleElement(finder)) {
// Settling can complete in-flight app work and rebuild content above
// the target (for example, a landing page replacing loading sections
// with API data). That can move a target which was visible before the
// rebuild back outside the viewport. Only report success after the
// settled tree still has the target in view.
await runtime.settleAfterAction();
if (assertions.hasVisibleElement(finder)) return;
}
final position = scrollableState.position;
if (!position.hasContentDimensions || position.extentAfter <= 0) break;
final previousPixels = position.pixels;
await tester.drag(
scrollable,
const Offset(0, -300),
warnIfMissed: false,
);
await tester.pump(const Duration(milliseconds: 50));
attempts++;
if ((scrollableState.position.pixels - previousPixels).abs() < 0.5) {
break;
}
}
throw EnsembleTestFailure(
'scrollUntilVisible: no visible widget matched $finder after '
'$attempts scroll attempts (scrollable: $scrollable).',
);
}