我正在我的 Flutter 应用程序中运行一些小部件测试,并遇到了这个问题。
因此,测试中调用的页面仅由 ListView.builder 生成,该 ListView.builder 构建卡片列表以及其后的“添加更多卡片” float 按钮(该按钮位于 ListView.builder 内部)。
根据Flutter documentation实际上没有什么 secret ,您只需找到它并像其他按钮小部件一样点击它即可。
await tester.tap(find.byType(FloatingActionButton));
但是当测试到达该行时,我收到“错误状态:没有这样的元素”
This is the full widget hierarchy for that page这是测试代码:
testWidgets('Connecting with new machine', (WidgetTester tester) async {
await tester.pumpWidget(makeWigdetTestable(child: MaquinasPage()));
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
expect(find.text('new machine connected'), findsOneWidget);
});
注意:“makeWidgetTestable”小部件仅返回一个 MaterialApp,其“home”属性是参数“MaquinasPage()”。
最佳答案
我遇到过这样的情况,我想从脚手架中找到一个 float 操作按钮
return Scaffold(
appBar: AppBar(title: Text("Title")),
body: Container(*** my code ***),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => { *** my code ***}
),
);
我的测试:
expect(find.widgetWithIcon(FloatingActionButton, Icons.add), findsOneWidget);
关于flutter - 在 Flutter 测试中找到 FloatingActionButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849918/