Flutter Widget Test expansionTile not opening on tester.tap()

Viewed 345

I am new to testing widgets in flutter and I have no clue why it doesn't work. My expansionTile:

ExpansionTile(
        title: Padding(
            padding: EdgeInsets.symmetric(vertical: 8),
            child: Table(
              columnWidths: const <int, TableColumnWidth>{
                0: FlexColumnWidth(1),
                1: FlexColumnWidth(2.5),
                2: FlexColumnWidth(1),
              },
              children: [
                TableRow(children: [
                  Container(),
                  Container(TextButton(child: Text('Some title'))),
                  Container(),
                ])
              ],
            )),
        children: [
          Table(
            columnWidths: const <int, TableColumnWidth>{
              0: FlexColumnWidth(1),
              1: FlexColumnWidth(3),
              2: FlexColumnWidth(0.5),
            },
            children: <TableRow>[
              TableRow(children: [
                Container(),
                Container(
                  child: Row(
                    children: [
                    Text('Find me'),
                  ]),
                ),
                Align(),
              ]),
            ],
          ),
        ],
      ),

And this is the test I have now:

testWidgets('Get opties text', (WidgetTester tester) async {
  await _createWidget(tester);
  await tester.pump();

  await tester.ensureVisible(find.byType(ExpansionTile));
  await tester.tap(find.byType(ExpansionTile));
  await tester.pump(Duration(seconds: 15));
  await tester.ensureVisible(find.text('Find me')); //This gives me an error
});

Error: The following StateError was thrown running a test: Bad state: No element

2 Answers

also try this It's work for me

final icon = find.byIcon(Icons.expand_more).first; 
// if you have more then one ExpandablePanel then use .first.
expect(icon, findsOneWidget);
await tester.tap(icon);
Related