Flutter Driver: Test BottomNavigationBarItem

2019-08-22 00:39发布

问题:

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.

回答1:

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),)
          )
        ],
      ),

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: