【问题标题】:Find a FloatingActionButton in Flutter test在 Flutter 测试中找到一个 FloatingActionButton
【发布时间】:2020-07-06 01:29:50
【问题描述】:

我在我的 Flutter 应用程序中运行了一些小部件测试并遇到了这个问题。

因此,测试中调用的页面仅由构建卡片列表的 ListView.builder 和“添加更多卡片”浮动按钮创建(该按钮位于 ListView.builder 内)。

根据Flutter documentation 所说,这真的没有什么秘密,您只需找到它并像任何其他按钮小部件一样点击它。

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()'。

【问题讨论】:

    标签: flutter flutter-test


    【解决方案1】:

    我遇到过这样的情况,我想从脚手架上找到一个浮动操作按钮

    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);
    

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2020-10-01
      • 2020-12-04
      • 2022-08-03
      • 1970-01-01
      • 2021-02-09
      • 2017-11-23
      • 2018-01-19
      • 2021-02-04
      相关资源
      最近更新 更多