execute method
Future<void>
execute({
- required CancelToken cancelToken,
- required TestReport report,
- required TestController tester,
Executes the step. This will first look for the actual image, then attempt to load the image from the golden cache. It will compare the images and return a result.
Implementation
@override
Future<void> execute({
required CancelToken cancelToken,
required TestReport report,
required TestController tester,
}) async {
var enabled = JsonClass.parseBool(
tester.getVariable(kDisableGoldenImageVariable),
) !=
true;
enabled = enabled &&
JsonClass.parseBool(
tester.getVariable(ScreenshotStep.kDisableScreenshotVariable),
) !=
true;
if (enabled == true) {
final allowedDelta = JsonClass.parseDouble(
tester.resolveVariable(this.allowedDelta),
0.01,
);
var imageId = this.imageId;
try {
imageId ??= report.images
.where((image) => image.goldenCompatible == true)
.last
.id;
} catch (e) {
// no-op
}
if (imageId?.isNotEmpty != true) {
throw Exception('$id: No imageId found');
}
final name =
"$id('$imageId', '$failWhenGoldenMissing', '$imageOnFail', '$allowedDelta')";
log(
name,
tester: tester,
);
Uint8List? actual;
try {
actual =
report.images.where((image) => image.id == imageId).first.image;
} catch (e) {
// no-op
}
if (actual == null) {
throw Exception('imageId: [$imageId] -- error loading actual image');
}
final master = await tester.testImageReader(
deviceInfo: report.deviceInfo!,
imageId: imageId!,
suiteName: report.suiteName,
testName: report.name!,
testVersion: report.version,
);
if (master == null) {
final disableFailOnMissing = JsonClass.parseBool(
tester.getVariable(kDisableGoldenImageFailOnMissingVariable),
);
if (failWhenGoldenMissing == true && !disableFailOnMissing) {
throw Exception('imageId: [$imageId] -- unable to load golden');
}
} else {
final comparitor = GoldenImageComparator();
final result =
await comparitor.compareLists(actual, master, allowedDelta);
if (result.passed != true) {
final failImages = [];
switch (imageOnFail) {
case 'both':
failImages.add(result.isolated);
failImages.add(result.masked);
break;
case 'isolated':
failImages.add(result.isolated);
break;
default:
failImages.add(result.masked);
break;
}
for (var failImage in failImages) {
if (failImage != null) {
report.attachScreenshot(
(await failImage.toByteData(format: ImageByteFormat.png))!
.buffer
.asUint8List(),
goldenCompatible: false,
id: 'failed-${imageId}',
);
}
}
throw Exception('${result.error}');
}
}
}
}