captureWaitForNavigationScreenshot method

Future<bool> captureWaitForNavigationScreenshot({
  1. required TestStepExecutor executor,
  2. required TestStep step,
  3. required int stepIndex,
})

Screenshots for waitForNavigation are captured only after the target remains visible through a paint pass. A tracker event can precede the destination's first rendered frame, so an early snapshot may belong to the previous route. If the target disappears before paint, return false and let the step completion boundary capture the actual rendered screen.

Returns whether a frame was recorded.

Implementation

Future<bool> captureWaitForNavigationScreenshot({
  required TestStepExecutor executor,
  required TestStep step,
  required int stepIndex,
}) async {
  return await RunnerBenchmark.async(
      'screenshot', 'captureWaitForNavigationScreenshot', () async {
    final options = executor.context.config.screenshots;
    if (!options.shouldCaptureStep(step.type)) return false;

    final screen = step.args['screen']?.toString();
    if (screen == null || screen.isEmpty) return false;

    final tracker = ScreenTracker();
    bool isTargetVisible() =>
        tracker.isScreenVisible(screenName: screen) ||
        tracker.isScreenVisible(screenId: screen);

    if (!isTargetVisible()) return false;
    if (treeHasFlutterErrorWidget(executor.tester)) return false;

    try {
      // Tracker events can precede the route's first paint. Give Flutter a
      // frame, then advance once more only when rendering still has scheduled
      // work. A fixed six-frame wait adds up to 300ms for already-painted
      // destinations without improving those captures.
      await executor.tester.pump(const Duration(milliseconds: 50));
      if (isTargetVisible() && areVisibleLottiesReady(executor.tester)) {
        seekVisibleLottiesForScreenshot(executor.tester);
        await executor.tester.pump();
      }
    } catch (_) {
      // Best-effort paint only.
    }

    if (isTargetVisible()) {
      // Still on target after paint; capture the rendered destination.
      await captureAutomaticScreenshotForStepBestEffort(
        executor: executor,
        step: step,
        stepIndex: stepIndex,
        ensureTargetVisible: false,
        waitForLottie: false,
        stabilize: false,
      );
      return executor.context.runtime.screenshotSheetFrames
          .any((frame) => frame.stepIndex == stepIndex);
    }

    // Tracker observed a visit, but no target frame was rendered. Evidence
    // capture falls back to the step completion boundary, which pairs the
    // actual current screen with its Observer snapshot. Give a replacement
    // route one bounded frame to apply state changes queued by the transient
    // route callback; otherwise the completion boundary can freeze the
    // transient widget tree before that queued replacement is built.
    await executor.tester.pump(const Duration(milliseconds: 50));
    return false;
  });
}