繁体   English   中英

Flutter 测试:在另一个小部件中寻找一个小部件

[英]Flutter Test: Look for a widget inside of another widget

我正在寻找方法来查看我在测试中找到的小部件内部是否有某些文本。 就我而言,我想查找的特定文本不在完整的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);

我认为您正在寻找的是widgetWithText 这将找到一个widgetType小部件,该小部件具有带有给定文本的Text后代。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM