execute method

  1. @override
Future<void> execute(
  1. {required CancelToken cancelToken,
  2. required TestReport report,
  3. required TestController tester}
)
override

Executes the step. This will first look for the Testable, get the value from the Testable, then compare it against the set value.

Implementation

@override
Future<void> execute({
  required CancelToken cancelToken,
  required TestReport report,
  required TestController tester,
}) async {
  final testableId = tester.resolveVariable(this.testableId);
  final value = tester.resolveVariable(this.value)?.toString();
  assert(testableId?.isNotEmpty == true);

  final name = "$id('$testableId', '$value', '$equals', '$caseSensitive')";
  log(
    name,
    tester: tester,
  );
  final finder = await waitFor(
    testableId,
    cancelToken: cancelToken,
    tester: tester,
    timeout: timeout,
  );

  await sleep(
    tester.delays.postFoundWidget,
    cancelStream: cancelToken.stream,
    tester: tester,
  );

  if (cancelToken.cancelled == true) {
    throw Exception('[CANCELLED]: step was cancelled by the test');
  }
  final widgetFinder = finder.evaluate();
  var match = false;
  dynamic actual;
  if (widgetFinder.isNotEmpty == true) {
    final element = widgetFinder.first as StatefulElement;

    final state = element.state;
    if (state is TestableState) {
      try {
        actual = state.onRequestValue!();
        if (equals ==
            (caseSensitive == true
                ? (actual?.toString() == value)
                : (actual?.toString().toLowerCase() ==
                    value?.toString().toLowerCase()))) {
          match = true;
        }
      } catch (e) {
        throw Exception(
          'testableId: [$testableId] -- could not locate Testable with a functional [onRequestValue] method.',
        );
      }
    }
  }
  if (match != true) {
    throw Exception(
      'testableId: [$testableId] -- actualValue: [$actual] ${equals == true ? '!=' : '=='} [$value] (caseSensitive = [$caseSensitive]).',
    );
  }
}