tapFinder method
Taps an already-resolved Finder (exact element identity; no id rematch).
Implementation
Future<void> tapFinder(
Finder finder, {
TestStep? step,
Future<void> Function()? onBeforeTap,
int? timeoutMs,
}) async {
return await RunnerBenchmark.async('execution', 'tapFinder', () async {
if (finder.evaluate().isEmpty) {
throw EnsembleTestFailure(
'tapFinder: target element is not in the tree (detached or never found).',
);
}
// Keep already-visible labels at their current scroll position. Calling
// ensureVisible unconditionally can scroll a nested screen and rebuild a
// TabBar between resolving its label and delivering the tap, so the tap
// lands on a stale/off-target position (seen in live KPN tab flows).
var hitTestable = finder.hitTestable();
Offset? tapPosition;
if (hitTestable.evaluate().isEmpty) {
await tester.ensureVisible(finder);
await runtime.pump(label: 'tapFinder.ensureVisible');
hitTestable = finder.hitTestable();
}
if (hitTestable.evaluate().isEmpty &&
_tapPositionForHitTestableDescendant(finder) == null) {
final timeout = Duration(
milliseconds:
timeoutMs ?? config.defaultWaitTimeout.inMilliseconds,
);
try {
await runtime.waitForCondition(
condition: () =>
finder.hitTestable().evaluate().isNotEmpty ||
_tapPositionForHitTestableDescendant(finder) != null,
timeout: timeout,
);
} on FlutterError catch (error) {
if (!error.toString().contains('timed out')) rethrow;
throw EnsembleTestFailure(
'tapFinder: target did not become hit-testable within '
'${timeout.inMilliseconds}ms. It may be disabled or covered by '
'another widget.',
);
}
hitTestable = finder.hitTestable();
}
// An id can be attached to a logical card whose bounds include empty
// padding. Flutter's hitTestable() checks the center of those bounds,
// which can fail even though a visible child in that same card is
// tappable. Keep the resolved element (and therefore occurrence) fixed,
// and use a hit-testable text/content descendant as the gesture point.
if (hitTestable.evaluate().isEmpty) {
tapPosition = _tapPositionForHitTestableDescendant(finder);
if (tapPosition != null) hitTestable = finder;
}
if (hitTestable.evaluate().isEmpty) {
throw EnsembleTestFailure(
'tapFinder: target element is not hit-testable. '
'It may be off-screen, disabled, or covered by another widget.',
);
}
await onBeforeTap?.call();
// Capturing step evidence can yield to asynchronous work. Revalidate the
// exact resolved finder after capture so a tap cannot pass using a target
// that disappeared or became covered while its proof was being frozen.
if (finder.evaluate().isEmpty) {
throw EnsembleTestFailure(
'tapFinder: target detached while capturing pre-action evidence.',
);
}
if (finder.hitTestable().evaluate().isEmpty) {
tapPosition = _tapPositionForHitTestableDescendant(finder);
if (tapPosition == null) {
throw EnsembleTestFailure(
'tapFinder: target stopped being hit-testable while capturing '
'pre-action evidence.',
);
}
} else {
tapPosition = null;
}
if (tapPosition == null) {
await tester.tap(finder.hitTestable().first);
} else {
await tester.tapAt(tapPosition);
}
await runtime.settleAfterAction();
});
}