Flutter Test: Look for a widget inside of another widget

Viewed 73

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

1 Answers

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