在 Flutter 测试中查找 CustomScrollView 内的小部件

Mat*_*teo 7 flutter flutter-test customscrollview widget-test-flutter flutter-integration-test


\n我在尝试测试我的 Flutter 应用程序时遇到错误。我有一个自定义小部件,位于CustomScrollView小部件的底部(第一个视口之外)。在我的测试中,我想验证它是否确实存在。
\n我已经尝试过使用WidgetTester.scrollUntilVisibleas WidgetTester.drag(就像在flutter框架的测试中所做的那样)。此外,我尝试重构我的测试,但这FlutterDriver完全搞乱了其他一切。
\n如何滚动到CustomScrollView测试内部的底部?
\n这个最小的复制显示了包含 的应用程序,该应用CustomScrollView程序具有一个屏幕高度的容器小部件(以便我要查找的小部件不在初始视图中)

\n
void main() {\n  runApp(MyApp());\n}\n\nclass MyApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      home: Scaffold(\n          body: CustomScrollView(\n        slivers: [\n          SliverToBoxAdapter(\n            child: Container(\n              height: MediaQuery.of(context).size.height,\n            ),\n          ),\n          MyWidget(),\n        ],\n      )),\n    );\n  }\n}\n\nclass MyWidget extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return SliverToBoxAdapter(\n      child: Container(\n        height: 100,\n        width: 100,\n      ),\n    );\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我最初编写的用于查找小部件的测试,当然失败了。

\n
void main() {\n  testWidgets('main contains mywidget', (WidgetTester tester) async {\n    // arange\n    await tester.pumpWidget(MyApp());\n    // act\n    final myWidget = find.byType(MyWidget);\n    // assert\n    expect(myWidget, findsOneWidget);\n  });\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在本次迭代中,我使用了WidgetTester.scrollUntilVisible函数,但出现了以下错误。

\n
void main() {\n  testWidgets('main contains mywidget', (WidgetTester tester) async {\n    // arange\n    await tester.pumpWidget(MyApp());\n    // act\n    final myWidget = find.byType(MyWidget);\n    final customScrollView = find.byType(CustomScrollView);\n    await tester.scrollUntilVisible(myWidget, 100,\n        scrollable: customScrollView);\n    await tester.pump();\n    // assert\n    expect(myWidget, findsOneWidget);\n  });\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following _CastError was thrown running a test:\ntype 'CustomScrollView' is not a subtype of type 'Scrollable' in type cast\n\nWhen the exception was thrown, this was the stack:\n#0      WidgetController.widget (package:flutter_test/src/controller.dart:66:44)\n#1      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:995:15)\n#2      WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:993:39)\n#5      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)\n#6      WidgetController.scrollUntilVisible (package:flutter_test/src/controller.dart:993:27)\n#7      main.<anonymous closure> (file:///C:/Users/X/test_example/test/main_test.dart:12:18)\n<asynchronous suspension>\n<asynchronous suspension>\n(elided 3 frames from dart:async and package:stack_trace)\n
Run Code Online (Sandbox Code Playgroud)\n

我感谢任何有关如何解决此问题并成功测试我的帮助或建议CustomScrollView

\n

小智 14

我遇到了同样的问题,最终使用了 DragUntilVisible() 方法而不是scrollUntilVisible()。

向下滚动直到小部件可见的示例:

await tester.dragUntilVisible(myWidget, customScrollView, Offset(0, -500));
Run Code Online (Sandbox Code Playgroud)