tap method
Implementation
Future<void> tap(
String id, {
int? timeoutMs,
TestStep? step,
Future<void> Function()? onBeforeTap,
}) async {
return await RunnerBenchmark.async('execution', 'tap', () async {
final effectiveTimeout =
timeoutMs ?? config.defaultWaitTimeout.inMilliseconds;
Finder? tappableFinder;
var attemptedEnsureVisible = false;
final stopwatch = Stopwatch()..start();
while (stopwatch.elapsedMilliseconds < effectiveTimeout) {
RunnerBenchmark.count('pollingAttempts', 1);
tappableFinder = _findTappableFinder(id);
if (tappableFinder != null) break;
// Resolve the already-mounted target before advancing the clock. Most
// actions start from a stable tree, so the old unconditional poll pump
// added waitPollInterval latency to every tap. A frame is still
// delivered when the target is absent, preserving the wait contract
// for targets that are mounted by a later callback.
await runtime.pump(duration: config.waitPollInterval, label: 'tap');
// A target can be mounted below a scroll viewport before it becomes
// hit-testable. Move the primary scroll surface once; repeated
// scrollUntilVisible iterations add seconds on live bindings.
if (!attemptedEnsureVisible) {
final baseFinder = assertions.finderForId(id, skipOffstage: false);
final interactive = _interactiveFinder(baseFinder);
if (interactive.evaluate().isNotEmpty) {
attemptedEnsureVisible = true;
try {
final targetElement = interactive.evaluate().first;
final owner = Scrollable.maybeOf(targetElement);
if (owner != null && owner.position.hasContentDimensions) {
owner.position.jumpTo(owner.position.maxScrollExtent);
}
await Scrollable.ensureVisible(
targetElement,
duration: Duration.zero,
);
final scrollables = find.byType(Scrollable);
if (owner == null && scrollables.evaluate().isNotEmpty) {
for (final element in scrollables.evaluate()) {
final state = tester.state<ScrollableState>(
find.byWidget(element.widget),
);
final position = state.position;
if (position.hasContentDimensions) {
position.jumpTo(position.maxScrollExtent);
}
}
}
await runtime.pump(label: 'tap.scrollIntoView');
} catch (_) {
// Continue polling so disabled/covered targets retain diagnostics.
}
}
}
}
if (tappableFinder == null) {
final baseFinder = assertions.finderForId(id);
if (baseFinder.evaluate().isEmpty) {
if (assertions
.finderForIdIncludingOffstage(id)
.evaluate()
.isNotEmpty) {
throw EnsembleTestFailure(
'Timed out after ${effectiveTimeout}ms waiting for id "$id" to become '
'hit-testable. It may be off-screen, disabled, or covered by another widget. '
'${assertions.widgetIdFailureHint(id)}',
);
}
throw TestExecutionError(
code: TestExecutionErrorCode.elementNotFound,
message: 'Timed out after ${effectiveTimeout}ms waiting for id '
'"$id". ${assertions.widgetIdFailureHint(id)}',
details: {'id': id},
);
}
throw EnsembleTestFailure(
'Timed out after ${effectiveTimeout}ms waiting for id "$id" to become '
'hit-testable. It may be off-screen, disabled, or covered by another widget. '
'${assertions.widgetIdFailureHint(id)}',
);
}
// Already hit-testable finders do not need ensureVisible. Calling it can
// still drive scrollables / rebuilds that race Live-binding navigation and
// trip UnmanagedRestorationScope assertions on some Ensemble screens.
if (tappableFinder.hitTestable().evaluate().length != 1) {
await tester.ensureVisible(tappableFinder);
await runtime.pump(label: 'tap.ensureVisible');
}
tappableFinder = _hitTestableFinderForTap(
assertions.finderForId(id, skipOffstage: false),
id,
);
await onBeforeTap?.call();
await tester.tap(tappableFinder);
await runtime.settleAfterAction();
});
}