【问题标题】:Flutter Test: Look for a widget inside of another widgetFlutter 测试:在另一个小部件中寻找一个小部件
【发布时间】:2022-08-03 03:08:45
【问题描述】:

我正在寻找方法来查看我在测试中找到的小部件内部是否有某些文本。就我而言,我想查找的特定文本不在完整的 RadioButtonGroup 内,而是在找到的 ElevatedButton --> firstButton 内。可能吗?

 testWidgets(\'Horizontal Radio Group builds a Row\', (tester) async {
    await tester.pumpWidget(
        Directionality(
            textDirection: TextDirection.ltr,
            child: simpleRadio
        ));

    expect(find.byType(Row), findsOneWidget);
    expect(find.byType(Column), findsNothing);
    var optionButtons = find.byType(ElevatedButton);
    expect(optionButtons, findsNWidgets(2));
    ElevatedButton firstButton = tester.firstWidget(optionButtons);
    
  });

寻找类似的东西:expect(firstButton.findByText(\'bla\'), findsOneWidget);

    标签: flutter dart flutter-test widget-test-flutter


    【解决方案1】:

    我想你要找的是widgetWithText。这将找到一个widgetType 的小部件,它具有带有给定文本的Text 后代。

    因此,对于您的示例,例如:

    expect(find.widgetWithText(ElevatedButton, 'bla'), findsOneWidget);
    

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 2019-01-30
      • 1970-01-01
      • 2012-08-05
      • 2021-12-09
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      相关资源
      最近更新 更多