captureHostStepReportArtifactsSafely function

Future<bool> captureHostStepReportArtifactsSafely({
  1. required WidgetTester tester,
  2. required EnsembleTestContext context,
  3. required LocalTestExecutionSession session,
  4. required TestStep step,
  5. required int stepIndex,
  6. required List<TestFailureDetails> secondaryFailures,
  7. Future<bool> prepareCapture()?,
  8. bool atCurrentFrame = false,
  9. ObservationDetailLevel observationDetail = ObservationDetailLevel.standard,
})

Implementation

Future<bool> captureHostStepReportArtifactsSafely({
  required WidgetTester tester,
  required EnsembleTestContext context,
  required LocalTestExecutionSession session,
  required TestStep step,
  required int stepIndex,
  required List<TestFailureDetails> secondaryFailures,
  Future<bool> Function()? prepareCapture,
  bool atCurrentFrame = false,
  ObservationDetailLevel observationDetail = ObservationDetailLevel.standard,
}) async {
  // Keep the host path aligned with TestVisualArtifacts: excluded evidence
  // exits before benchmark instrumentation or any capture setup.
  if (!context.config.screenshots.shouldCaptureStep(step.type)) return false;

  // StepEvidenceSubscriber owns de-duplication across pre-action and
  // completed-step boundaries. Keep this function focused on capturing and
  // publishing one host frame; inspecting report state here creates a second,
  // independent capture guard.
  return await RunnerBenchmark.async(
      'workflow', 'captureHostStepReportArtifactsSafely', () async {
    try {
      // Some evidence boundaries (currently navigation matches) need to
      // confirm that their target survives a paint opportunity before this
      // shared capture owner freezes the frame. A false result leaves the
      // step-finished fallback eligible to capture the rendered destination.
      if (prepareCapture != null && !await prepareCapture()) return false;
      if (atCurrentFrame && step.type == 'waitForText') {
        // Match the standalone adapter: freeze the text only after its
        // highlight has reached a visible paint opacity. Text can satisfy the
        // predicate while an AnimatedOpacity transition is still underway.
        await ScreenshotTargetSupport(
          drainFlutterExceptions: _drainHostFlutterExceptions,
        ).waitForHighlightTargetToPaint(session.executor, step);
      }
      var captured = false;
      await captureStepReportArtifacts(
        // Eligibility was checked before benchmark instrumentation above.
        // Repeating the same configuration lookup in the shared report
        // wrapper does not add safety here and runs for every captured step.
        enabled: true,
        captureScreenshot: () async {
          // Capture is awaited inline and appends at most one frame. Comparing
          // list lengths is constant time; filtering all prior frames before
          // and after each screenshot made long suites pay a growing scan cost.
          final before = context.runtime.screenshotSheetFrames.length;
          await captureHostStepScreenshotSafely(
            tester: tester,
            context: context,
            assertions: session.assertions,
            navigation: session.services.navigation,
            step: step,
            stepIndex: stepIndex,
            observationDetail: observationDetail,
            secondaryFailures: secondaryFailures,
            atCurrentFrame: atCurrentFrame,
          );
          final after = context.runtime.screenshotSheetFrames.length;
          captured = after > before;
          return captured;
        },
      );
      return captured;
    } catch (error) {
      // Report capture must never replace the real test failure.
      context.runtime.consoleLogs.add(
        context.runtime.formatConsoleLine(
          '[Screenshot] Step $stepIndex artifact capture failed: $error',
        ),
      );
      return false;
    }
  });
}