executeStep method

Future<bool> executeStep({
  1. required CancelToken cancelToken,
  2. required TestReport report,
  3. required TestStep step,
  4. bool subStep = true,
})

Executes a single step and attaches the execution information to the testReport. The cancelToken allows the step to be cancelled. Whenever a step has a loop or a long running task, it should listen to the stream from the token or read the flag from the token.

Implementation

Future<bool> executeStep({
  required CancelToken cancelToken,
  required TestReport report,
  required TestStep step,
  bool subStep = true,
}) async {
  var passed = true;
  final runnerStep = _registry.getRunnerStep(
    id: step.id,
    values: step.values,
  )!;

  if (cancelToken.cancelled == true) {
    throw Exception('[CANCELLED]: step was canceled by the test');
  }

  if (delays.preStep.inMilliseconds > 0) {
    await runnerStep.preStepSleep(delays.preStep);
  }

  report.startStep(
    step,
    subStep: subStep,
  );
  String? error;
  try {
    if (cancelToken.cancelled == true) {
      throw Exception('[CANCELLED]: step was cancelled by the test');
    }
    _testControllerState.currentStep = step.id;
    await runnerStep.execute(
      cancelToken: cancelToken,
      report: report,
      tester: this,
    );
  } catch (e, stack) {
    if (cancelToken.cancelled == true) {
      rethrow;
    }
    if (screenshotOnFail == true) {
      try {
        final imageNum = report.images.length + 1;
        await ScreenshotStep(
          goldenCompatible: false,
          imageId: 'failure_${step.id}_${imageNum}',
        ).execute(
          cancelToken: cancelToken,
          report: report,
          tester: this,
        );
      } catch (e2, stack2) {
        _logger.severe(
          'Error taking failure screenshot: ${step.id}',
          e2,
          stack2,
        );
      }
    }

    _logger.severe('Error running test step: ${step.id}', e, stack);
    error = '$e';
    passed = false;
  } finally {
    _testControllerState.currentStep = null;
    report.endStep(step, error);
  }

  if (cancelToken.cancelled == true) {
    throw Exception('[CANCELLED]: step was cancelled by the test');
  }
  if (delays.postStep.inMilliseconds > 0) {
    await runnerStep.postStepSleep(delays.preStep);
  }

  return passed;
}