ensureHighlightTargetVisible method

Future<void> ensureHighlightTargetVisible(
  1. TestStepExecutor executor,
  2. TestStep step
)

Implementation

Future<void> ensureHighlightTargetVisible(
  TestStepExecutor executor,
  TestStep step,
) async {
  return await RunnerBenchmark.async(
      'screenshot', 'ensureHighlightTargetVisible', () async {
    if (!shouldHighlightStep(step)) return;
    final finder = _highlightFinder(executor, step);
    if (finder == null) return;

    final requireHitTestable = isUserActionStep(step);
    final visibleElement = executor.assertions.firstVisuallyActionableElement(
      finder,
      requireHitTestable: requireHitTestable,
    );
    if (visibleElement != null) {
      // The matched state is already on screen. Calling ensureVisible here
      // can advance a live frame before the evidence is frozen, replacing a
      // transient match with whatever arrives next. Off-screen targets use
      // the recovery path below.
      return;
    }

    Element? currentRouteMatch;
    for (final candidate in finder.evaluate()) {
      if (!isUnderCurrentModalRoute(candidate)) continue;
      currentRouteMatch = candidate;
      break;
    }
    if (currentRouteMatch == null) return;

    var visibilityTarget = find.byWidget(currentRouteMatch.widget);
    if (step.type == 'toggle' ||
        step.type == 'check' ||
        step.type == 'uncheck') {
      final control = find.descendant(
        of: visibilityTarget,
        matching: find.byWidgetPredicate(
          (widget) =>
              widget is Switch ||
              widget is CupertinoSwitch ||
              widget is Checkbox,
        ),
      );
      if (control.evaluate().isNotEmpty) visibilityTarget = control.first;
    }
    await executor.tester.ensureVisible(visibilityTarget);
    await executor.tester.pump();
  });
}