resolveMatches method
List<Element>
resolveMatches(
- ElementTarget target, {
- bool requireInteractive = true,
- bool allowEmpty = false,
- bool allowOffscreen = false,
Candidate elements for target, after semantic/text collapse and
optional interactive filtering. Throws on ambiguity for positive
resolution; returns [] when allowEmpty and nothing matches.
Implementation
List<Element> resolveMatches(
ElementTarget target, {
bool requireInteractive = true,
bool allowEmpty = false,
bool allowOffscreen = false,
}) {
return RunnerBenchmark.sync('locator', 'resolveMatches', () {
if (target.usesSnapshotElement) {
final observationId = target.observationId;
if (observationId == null || observationId.isEmpty) {
throw const TestExecutionError(
code: TestExecutionErrorCode.staleObservation,
message: 'elementId targets require observationId.',
);
}
final handle = registry.revalidate(
observationId: observationId,
elementId: target.elementId!,
liveFingerprint: liveFingerprint,
);
RunnerBenchmark.count('locatorCandidates', 1);
RunnerBenchmark.count('locatorMatches', 1);
return [handle.element];
}
final locator = target.normalizedLocator;
if (locator == null || locator.isEmpty) {
throw const TestExecutionError(
code: TestExecutionErrorCode.elementNotFound,
message:
'ElementTarget requires a locator or elementId+observationId.',
);
}
final finder = _finderForLocator(
locator,
allowEmpty: allowEmpty,
rootLocator: locator,
requireInteractive: requireInteractive,
);
var matches = finder.evaluate().toList();
RunnerBenchmark.count('locatorCandidates', matches.length);
// A `within`-only target describes the logical descendants of a scope,
// not the first implementation widget in its subtree. Filter to
// actionable descendants before collapsing ancestor/descendant pairs;
// otherwise a Padding/Builder wrapper can hide its actual button.
final scopedDescendantsOnly = locator.within != null &&
locator.id == null &&
locator.text == null &&
locator.label == null &&
locator.role == null &&
locator.bounds == null;
if (requireInteractive && scopedDescendantsOnly) {
matches = matches
.where((element) => _isActionable(element, allowOffscreen))
.toList();
}
matches = _collapseOverlappingMatches(matches, locator);
// For ID locators, occurrence identifies the authored widget, not the
// interactive ancestor used to dispatch a tap. Keep the ID matches in
// tree order until occurrence is applied below, then promote only that
// selected widget to its actionable control. Filtering/promoting first
// can reorder or collapse repeated IDs and make waitFor(id, occurrence)
// select a different element from tap(id, occurrence).
final promoteSelectedId = requireInteractive && locator.id != null;
if (requireInteractive && !scopedDescendantsOnly && !promoteSelectedId) {
matches = matches
.where((element) => _isActionable(element, allowOffscreen))
.toList();
}
RunnerBenchmark.count('locatorMatches', matches.length);
if (matches.isEmpty) {
if (allowEmpty) return const [];
throw TestExecutionError(
code: TestExecutionErrorCode.elementNotFound,
message: '${_describeLocator(locator)} not found.',
details: {'locator': locator.toJson()},
);
}
final occurrence = locator.occurrence ?? 0;
if (locator.occurrence == null && matches.length > 1) {
throw TestExecutionError(
code: TestExecutionErrorCode.ambiguousTarget,
message: 'Multiple widgets match ${_describeLocator(locator)} '
'(${matches.length}). '
'Provide occurrence or use a snapshot elementId.',
details: {'locator': locator.toJson(), 'count': matches.length},
);
}
if (occurrence < 0 || occurrence >= matches.length) {
if (allowEmpty) return const [];
throw TestExecutionError(
code: TestExecutionErrorCode.elementNotFound,
message: 'occurrence $occurrence out of range for '
'${_describeLocator(locator)} '
'(${matches.length} matches).',
details: {'locator': locator.toJson(), 'occurrence': occurrence},
);
}
if (promoteSelectedId) {
final selected = _interactiveTargetForId(matches[occurrence]);
if (selected == null || !_isActionable(selected, allowOffscreen)) {
throw TestExecutionError(
code: TestExecutionErrorCode.elementNotFound,
message: '${_describeLocator(locator)} has no interactive target.',
details: {'locator': locator.toJson(), 'occurrence': occurrence},
);
}
return [selected];
}
if (matches.length == 1) return matches;
return [matches[occurrence]];
});
}