resolveFinder method

Finder resolveFinder(
  1. ElementTarget target, {
  2. bool requireInteractive = true,
  3. bool allowEmpty = false,
  4. bool allowOffscreen = false,
})

Returns a Finder for the target. Snapshot targets never fall back to testId.

When requireInteractive is true (default for actions), offstage, non-rendered, and explicitly disabled matches are excluded. Existence checks pass false.

When allowEmpty is true, zero matches return a finder that evaluates empty instead of throwing TestExecutionErrorCode.elementNotFound. Used by negative assertions (expectNotVisible / expectNotExists).

Implementation

Finder resolveFinder(
  ElementTarget target, {
  bool requireInteractive = true,
  bool allowEmpty = false,
  bool allowOffscreen = false,
}) {
  return RunnerBenchmark.sync('locator', 'resolveFinder', () {
    // Snapshot handles must revalidate their observation on every use.
    // Locator-based finders can be reused while the observed tree revision
    // is unchanged.
    if (!target.usesSnapshotElement) {
      final revision = currentRevision();
      if (_cacheRevision != revision) {
        _resolvedFinders.clear();
        _cacheRevision = revision;
      }
      // Finder reuse is a hot path for repeated waits and assertions. The
      // previous key serialized the complete target map on every lookup,
      // even though the target is already a typed value. Keep the same
      // field coverage while avoiding map/list allocation and JSON work.
      final key = _finderCacheKey(
        target,
        requireInteractive: requireInteractive,
        allowEmpty: allowEmpty,
        allowOffscreen: allowOffscreen,
      );
      final cached = _resolvedFinders[key];
      if (cached != null) return cached;

      final finder = _resolveFinderUncached(
        target,
        requireInteractive: requireInteractive,
        allowEmpty: allowEmpty,
        allowOffscreen: allowOffscreen,
      );
      // A zero-match finder is a snapshot of today's tree. Keeping it in the
      // cache prevents a bounded target wait from observing a widget that
      // mounts on a later frame. Cache only live matches; a later lookup can
      // resolve the target again after the tree changes.
      if (finder.evaluate().isEmpty) return finder;
      // Bound retained Elements and Finder closures even on a static tree.
      if (_resolvedFinders.length >= 32) _resolvedFinders.clear();
      _resolvedFinders[key] = finder;
      return finder;
    }
    return _resolveFinderUncached(
      target,
      requireInteractive: requireInteractive,
      allowEmpty: allowEmpty,
      allowOffscreen: allowOffscreen,
    );
  });
}