I find the idea to use goldens for creating screenshots quite intriguing. It is so much faster, more convenient and easier to work with than the screenshots package. Also by mocking the providers the screenshots have exactly the data I want.
The challenge are all the network images used across the app. Even when allowing http requests the following code will fail:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class SimpleImageWidget extends StatelessWidget {
final String imageUrl;
const SimpleImageWidget({super.key, required this.imageUrl});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Imagework(imageUrl),
),
);
}
}
void main() {
setUpAll(() async {
HttpOverrides.global = null;
});
testWidgets('Simple Image Widget Test', (WidgetTester tester) async {
// Use a reliable image URL for testing
const imageUrl = '/200/300';
await tester.pumpWidget(const SimpleImageWidget(imageUrl: imageUrl));
// Wait for the image to load
await tester.pumpAndSettle(const Duration(seconds: 10), EnginePhase.sendSemanticsUpdate, const Duration(seconds: 20));
// Capture the golden
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/simple_image_widget.png'),
);
});
}
The golden is pure white and there is an error
A Timer is still pending even after the widget tree was disposed.
Just to be clear: I know that there are several packages that mock the Image and deliver a transparent image. While this clearly works it is - for the sake of screenshots - not helpful.
I also tried CachedNetworkImage without success, was pumping the Widgets several times and mocking all images individually seems quite cumbersome.
I was wondering if someone has experience with this topic or a smart idea how to work around it.
Please share your opinion, Thanks a lot in advance!