tryExecute static method

Future<bool> tryExecute(
  1. TestStepExecutor executor,
  2. TestStep step
)

Implementation

static Future<bool> tryExecute(
  TestStepExecutor executor,
  TestStep step,
) async {
  switch (step.type) {
    case 'reloadScreen':
      await _reloadScreen(executor);
      return true;
    case 'restartApp':
      await _restartApp(executor);
      return true;
    case 'resetAppState':
      await _resetAppState(executor);
      return true;
    case 'trigger':
      await _trigger(executor, step);
      return true;
    case 'launchApp':
      await _restartApp(executor);
      return true;
    case 'doubleTap':
      await executor.tapWidget(executor.requireId(step));
      await executor.tapWidget(executor.requireId(step));
      return true;
    case 'longPress':
      await executor.longPressWidget(executor.requireId(step));
      return true;
    case 'focus':
      await executor.focusWidget(executor.requireId(step));
      return true;
    case 'unfocus':
      await executor.unfocus();
      return true;
    case 'selectIndex':
      await _selectIndex(executor, step);
      return true;
    case 'check':
      await executor.tapWidget(executor.requireId(step));
      return true;
    case 'uncheck':
      await _uncheck(executor, step);
      return true;
    case 'setSlider':
      await _setSlider(executor, step);
      return true;
    case 'chooseDate':
    case 'chooseTime':
      await executor.enterTextOn(
        executor.requireId(step),
        step.args['value']?.toString() ?? '',
      );
      return true;
    case 'scroll':
      await _scroll(executor, step);
      return true;
    case 'swipe':
      await _swipe(executor, step);
      return true;
    case 'drag':
      await _drag(executor, step);
      return true;
    case 'pullToRefresh':
      await _pullToRefresh(executor, step);
      return true;
    case 'expectExists':
      executor.assertions.expectExists(executor.requireId(step));
      return true;
    case 'expectNotExists':
      executor.assertions.expectNotExists(executor.requireId(step));
      return true;
    case 'expectTextContains':
      final anyOfRaw = step.args['anyOf'];
      final anyOf = <String>[
        if (anyOfRaw is List)
          for (final item in anyOfRaw)
            if (item != null && item.toString().trim().isNotEmpty)
              item.toString(),
      ];
      final text = step.args['text']?.toString();
      final timeoutMs = step.args['timeoutMs'] as int?;
      if (timeoutMs != null && timeoutMs > 0) {
        await executor.waitForTextContains(
          text: text,
          anyOf: anyOf,
          timeoutMs: timeoutMs,
        );
        return true;
      }
      if (anyOf.isNotEmpty) {
        executor.assertions.expectTextContainsAny(anyOf);
        return true;
      }
      if (text == null) {
        throw EnsembleTestFailure(
          'expectTextContains requires "text" or "anyOf"',
        );
      }
      executor.assertions.expectTextContains(text);
      return true;
    case 'expectChecked':
      executor.assertions.expectChecked(
        executor.requireId(step),
        step.args['equals'] as bool? ?? true,
      );
      return true;
    case 'expectSelected':
      executor.assertions.expectChecked(
        executor.requireId(step),
        step.args['equals'] as bool? ?? true,
      );
      return true;
    case 'expectProperty':
      executor.assertions.expectProperty(
        executor.requireId(step),
        step.args['property']?.toString() ?? 'label',
        step.args['equals'],
      );
      return true;
    case 'expectStyle':
      executor.assertions.expectProperty(
        executor.requireId(step),
        'style',
        step.args['equals'],
      );
      return true;
    case 'expectListCount':
      executor.assertions.expectListCount(
        listId: step.args['id']?.toString() ?? executor.requireId(step),
        expected: step.args['equals'] as int? ??
            (throw EnsembleTestFailure('expectListCount requires "equals"')),
        itemId: step.args['itemId']?.toString(),
      );
      return true;
    case 'expectListContains':
      executor.assertions.expectListContains(
        listId: step.args['id']?.toString() ?? '',
        text: step.args['text']?.toString() ?? '',
      );
      return true;
    case 'expectListItem':
      executor.assertions
          .expectVisible(step.args['itemId']?.toString() ?? '');
      return true;
    case 'expectEmpty':
      executor.assertions.expectListCount(
        listId: executor.requireId(step),
        expected: 0,
      );
      return true;
    case 'expectNotEmpty':
      executor.assertions.expectListCount(
        listId: executor.requireId(step),
        expected: 1,
        atLeast: true,
      );
      return true;
    case 'expectNotVisited':
      final screen = step.args['screen']?.toString();
      if (screen == null) {
        throw EnsembleTestFailure('expectNotVisited requires "screen"');
      }
      executor.assertions.expectNotVisited(screen);
      return true;
    case 'expectBackStack':
      executor.assertions.expectBackStack(
        (step.args['screens'] as List?)?.map((e) => e.toString()).toList() ??
            [],
      );
      return true;
    case 'expectCanGoBack':
      executor.assertions.expectCanGoBack(
        step.args['equals'] as bool? ?? true,
      );
      return true;
    case 'goBack':
      await _goBack(executor);
      return true;
    case 'expectApiCallOrder':
      executor.assertions.expectApiCallOrder(
        (step.args['names'] as List?)?.map((e) => e.toString()).toList() ??
            [],
      );
      return true;
    case 'expectLastApiCall':
      executor.assertions
          .expectLastApiCall(step.args['name']?.toString() ?? '');
      return true;
    case 'removeStorage':
      final key = step.args['key']?.toString();
      if (key == null)
        throw EnsembleTestFailure('removeStorage requires "key"');
      executor.context.removeStorage(key);
      return true;
    case 'clearStorage':
      await executor.context.clearStorage();
      return true;
    case 'setAuth':
      _setAuth(executor, step);
      return true;
    case 'clearAuth':
      _clearAuth(executor);
      return true;
    case 'setPermission':
      _setPermission(executor, step);
      return true;
    case 'setDevice':
      await _setDevice(executor, step);
      return true;
    case 'setLocale':
      await _setLocale(executor, step);
      return true;
    case 'setTheme':
      await _setTheme(executor, step);
      return true;
    case 'runScript':
      _runScript(executor, step, expectResult: false);
      return true;
    case 'expectScript':
    case 'expectScriptResult':
      _runScript(executor, step, expectResult: true);
      return true;
    case 'expectConsoleLog':
      executor.assertions
          .expectConsoleLog(step.args['contains']?.toString() ?? '');
      return true;
    case 'expectAccessible':
      executor.assertions.expectAccessible(executor.requireId(step));
      return true;
    case 'expectSemanticsLabel':
      executor.assertions.expectSemanticsLabel(
        executor.requireId(step),
        step.args['label']?.toString() ?? '',
      );
      return true;
    case 'expectNoOverflow':
      executor.assertions.expectNoOverflow(executor.requireId(step));
      return true;
    case 'expectError':
      executor.assertions.expectErrorRecorded(
        step.args['contains']?.toString(),
      );
      return true;
    case 'expectNoErrors':
      executor.assertions.expectNoRenderErrors();
      return true;
    case 'expectNoConsoleErrors':
      executor.assertions.expectNoConsoleErrors();
      return true;
    case 'expectNoRenderErrors':
      executor.assertions.expectNoRenderErrors();
      return true;
    default:
      return false;
  }
}