Flutter Driver: Test BottomNavigationBarItem

那年仲夏 提交于 2019-12-11 17:45:53

问题


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:



来源:https://stackoverflow.com/questions/55460993/flutter-driver-test-bottomnavigationbaritem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!