stepHighlightFinder function
Finder?
stepHighlightFinder({
- required WidgetTester tester,
- required AssertionEngine assertions,
- required TestStep step,
Finder used to draw the report screenshot highlight for step.
Supports YAML id:, structured target: (label/role/text/id), and
text/anyOf and element-value/semantics assertion steps. Returns null when
nothing can be resolved.
Implementation
Finder? stepHighlightFinder({
required WidgetTester tester,
required AssertionEngine assertions,
required TestStep step,
}) {
return RunnerBenchmark.sync('locator', 'stepHighlightFinder', () {
final id = step.args['id']?.toString();
if (id != null && id.isNotEmpty) {
return assertions.finderForId(id);
}
final targetRaw = step.args['target'];
if (targetRaw is Map) {
final locator = ElementLocator.fromJson(
Map<String, dynamic>.from(targetRaw),
);
if (!locator.isEmpty) {
try {
return FlutterTargetResolver(
tester: tester,
assertions: assertions,
registry: ObservationRegistry(),
).resolveFinder(
ElementTarget(locator: locator),
// For actions, highlight the same actionable ancestor that the
// executor taps. Resolving a text locator as a passive text node
// can leave the screenshot without a hit-testable target rect.
requireInteractive: isUserActionStep(step),
allowEmpty: true,
);
} on TestExecutionError {
return null;
}
}
}
final rawText = step.args['text'];
final texts = <String>[
if (rawText is String && rawText.trim().isNotEmpty) rawText,
if (rawText is List)
for (final item in rawText)
if (item != null && item.toString().trim().isNotEmpty)
item.toString(),
if (step.args['anyOf'] is List)
for (final item in step.args['anyOf'] as List)
if (item != null && item.toString().trim().isNotEmpty)
item.toString(),
];
for (final text in texts) {
if (step.type == 'expectTextContains') {
final containing = find.textContaining(text);
if (_hasHighlightRect(assertions, containing)) {
return containing;
}
} else {
final exact = find.text(text);
if (_hasHighlightRect(assertions, exact)) {
return exact;
}
}
}
return null;
});
}