简体   繁体   中英

Flutter Test: Look for a widget inside of another widget

I am looking for the way to see that the widget I have found in the test, has certain text inside. In my case I want to look for the specific text not inside the complete RadioButtonGroup but inside of the found ElevatedButton --> firstButton . Is it possible?

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

Looking for something like: expect(firstButton.findByText('bla'), findsOneWidget);

I think what you're looking for is widgetWithText . This'll find a widget of widgetType that has a Text descendant with the given text.

So for your example, something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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