Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Driver: Test BottomNavigationBarItem

how do I test BottomNavigationBarItems via FlutterDriver?

FlutterDriver allows accessing Widgets via text, byValueKey, byTooltip and byType.

But none of these methods work out for my App because of following reasons:

  • text: The App is localized and I need to test the App in multiple languages.

  • byValueKey: BottomNavigationBarItems do not have key properties.

  • byTooltip: BottomNavigationBarItems do not have toolTip properties.

  • byType: byType only returns the first match of the type and no list (needed because I have multiple tabs).

Thank you very much!

Cheers.

like image 946
basedgod Avatar asked Apr 01 '19 17:04

basedgod


1 Answers

Not sure if you have found answer for this question, but I am going to post a solution here which works for me. Basically, BottomNavigationBar has a key property which you need to use. Once Flutter Driver identifies this key, then you can tell driver to tap on any of its child items ie BottomNavigationBarItem.

My screen has 2 bottomNavigationBarItems as shown below and I defined key for their parent widget ie BottomNavigationBar as:

bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.shifting,
        key: Key('bottom'),
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.ac_unit, color: Colors.green,),
            title: Text('First', style: TextStyle(color: Colors.black),)
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.cast, color: Colors.yellow,),
            title: Text('Second', style: TextStyle(color: Colors.black),)
          )
        ],
      ),

enter image description here

And I wrote a flutter driver test to tap on both items which worked perfectly.

test('bottomnavigationbar test', () async {
      await driver.waitFor(find.byValueKey('bottom'));
      await driver.tap(find.text('First'));
      print('clicked on first');
      await driver.tap(find.text('Second'));
      print('clicked on second too');
    });

Result:

enter image description here

like image 161
Darshan Avatar answered Sep 25 '22 07:09

Darshan