Flutter - How test drawer in widgets test?

1,165

Use a finder by type, that should give your an array of all the items of an specific type, then just access the items by index like this. find.byType(ListTile).at(index);

Share:
1,165
FetFrumos
Author by

FetFrumos

Updated on December 12, 2022

Comments

  • FetFrumos
    FetFrumos over 1 year

    I'm testing my Flutter app. I have a widget drawer. I am doing a test for this widget. I need to open the box and click on the items. I use this code:

    final  drawer = find.byTooltip('Open navigation menu');
    expect(drawer, findsWidgets);
    tester.tap(drawer) ;
    

    It is work. But I don't know how finds items in my drawer anb tap to them. This is code for drawer:

        drawer: Drawer(
        key: Key("Drawer"),
        child: ListView(
          children: <Widget>[
             drawerItem(context, 'Login', Icons.account_circle, 'login'),
             drawerItem(context, 'Users', Icons.person, 'users'),
             drawerItem(context, 'Log Out', Icons.exit_to_app, 'logout')
          ],
        ),
      )
    
      .....
    
      ListTile drawerItem(BuildContext context, String title, IconData icon,
      [String route]) {
      return new ListTile(
      key: Key(title),
      title: Text(title),
      leading: Icon(icon)
         );
    

    How do I click on these items?

  • Sergio Bernal
    Sergio Bernal almost 5 years
    did you try this? tester.tap(find.byType(ListTile).first);