Flutter - how to get Text widget on widget test Flutter - how to get Text widget on widget test flutter flutter

Flutter - how to get Text widget on widget test


I got it working. I had to access the widget property of the Element, and then cast it as text:

var text = finder.evaluate().single.widget as Text;print(text.data);


Please check this simple example.

testWidgets('Test name', (WidgetTester tester) async {// findig the widgetvar textFind = find.text("text_of_field");// checking widget present or notexpect(textFind, findsOneWidget);//getting Text objectText text = tester.firstWidget(textFind);// validating properiesexpect(text.style.color, Colors.black);......}


You can use find.text

https://flutter.io/docs/cookbook/testing/widget/finders#1-find-a-text-widget

testWidgets('finds a Text Widget', (WidgetTester tester) async {  // Build an App with a Text Widget that displays the letter 'H'  await tester.pumpWidget(MaterialApp(    home: Scaffold(      body: Text('H'),    ),  ));  // Find a Widget that displays the letter 'H'  expect(find.text('H'), findsOneWidget);});