【发布时间】:2019-05-19 17:30:10
【问题描述】:
我需要测试中的屏幕看起来与物理设备(或模拟器)上的相同。我该怎么做?在我的情况下,设备 ID Iphone SE。
我写了一个将屏幕截图保存到磁盘的测试:
testWidgets('test', (WidgetTester tester) async {
final AutomatedTestWidgetsFlutterBinding binding = tester.binding;
binding.renderView.configuration = TestViewConfiguration(size: Size(640, 1136));
var widget = Scaffold(
appBar: AppBar(title: Text('title'),),
body: Column(children: <Widget>[
RaisedButton(
child: Text('button'),
onPressed: () {},)
],),
);
var key = new GlobalKey();
await tester.pumpWidget(
MaterialApp(home: RepaintBoundary(key: key, child: widget),),
);
await tester.pumpAndSettle();
await tester.runAsync(() async {
RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
var image = await boundary.toImage();
var byteData = await image.toByteData(format: ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
await File('screen.png').writeAsBytes(pngBytes);
});
});
如果使用带有 devicePixelRatio 的 ViewConfiguration 而不是 TestViewConfiguration,则忽略 devicePixelRatio
如果包装 MaterialApp,MediaQuery 也不起作用
应用栏和按钮比模拟器少
【问题讨论】:
-
您可能希望使用
matchesGoldenFile而不是手动进行屏幕截图
标签: flutter flutter-test