FlutterDriver问题,无法通过按键找到小部件

时间:2019-06-07 10:26:09

标签: flutter integration-testing

我在SerializableFinder中通过键查找小部件时遇到问题,该怎么解决?

我尝试用常数制作键,并且确保通过提供常数来检查键是否与finder中的键相同。此外,我指的是此链接:Flutter Driver: Test BottomNavigationBarItem

这是代码: 集成测试文件(示例部分,而不是完整的代码):

// todo: bottom navigation pressed
    test('bottom navigation bar test item', () async{

      // todo: intended = pressed favorite dessert, but we want to test
      await flutterDriver.waitFor(bottomNavigationBar);

      // todo: bottom navigation bar item text research
      await flutterDriver.tap(dessert); // intended : tap at bottom navigation view item

      print('This is Dessert section');

      // todo: expect title is keyword

      await flutterDriver.tap(seafood);

      print('This is Seafood section');

      await flutterDriver.tap(favoriteDessert);

      print('This is Favorite Dessert section');

      await flutterDriver.tap(favoriteSeafood);

      print('This is Favorite Seafood section');


    });

Finder文件(用于底部导航栏):

SerializableFinder bottomNavigationBar = find.byValueKey(BOTTOM_NAVIGATION_BAR);

SerializableFinder dessert = find.byValueKey(DESSERT);
SerializableFinder seafood = find.byValueKey(SEAFOOD);
SerializableFinder favoriteDessert = find.byValueKey(FAVORITE_DESSERT);
SerializableFinder favoriteSeafood = find.byValueKey(FAVORITE_SEAFOOD);

小部件文件(两部分): 第1部分:底部导航栏项

List<BottomNavigationBarItem> bottomNavigationBarItems = [
    BottomNavigationBarItem(
        icon: Icon(Icons.cake, key: Key(DESSERT)), title: Text("Dessert")),
    BottomNavigationBarItem(
        icon: Icon(Icons.restaurant, key : Key(SEAFOOD)), title: Text("Seafood")),
    BottomNavigationBarItem(
        icon: Icon(Icons.cake, key: Key(FAVORITE_DESSERT)), title: Text("Favorite Dessert")),
    BottomNavigationBarItem(
        icon: Icon(Icons.restaurant, key: Key(FAVORITE_SEAFOOD)), title: Text("Favorite Seafood"))
  ];

第2部分:底部导航栏

 bottomNavigationBar: BottomNavigationBar(
        key: Key(BOTTOM_NAVIGATION_BAR),
        items: bottomNavigationBarItems,
        currentIndex: currentIndex,
        onTap: (index) {
          changeSelectedBottomNavigationBarItem(index);
        },
        selectedItemColor: appConfig.appColor,
        unselectedItemColor: Colors.grey,
      ),

如果您只想提供完整的代码,我将非常乐意提供它们。

预期结果:进行集成测试时,该应用程序将自动导航所选项目

实际结果:

偷窥:

00:02 +0: Meals Catalogue App bottom navigation bar test item
[warning] FlutterDriver: waitFor message is taking a long time to complete...
00:32 +0 -1: Meals Catalogue App bottom navigation bar test item [E]
  TimeoutException after 0:00:30.000000: Test timed out after 30 seconds.
00:32 +0 -1: Meals Catalogue App (tearDownAll)
00:32 +0 -1: Meals Catalogue App bottom navigation bar test item [E]
  DriverError: Failed to fulfill WaitFor due to remote error
  Original error: Bad state: The client closed with pending request "ext.flutter.driver".

由于链接中的堆栈跟踪太长,我将在此处提供我的粘贴框:https://pastebin.com/p4ktKXLA

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,最终对我有帮助的解决方案来自Pyozer

默认情况下,flutter驱动程序是帧同步的,它将等待直到没有待处理的帧为止,但是如果存在无限动画,则测试将超时并失败。

要解决此问题,您需要使用driver.runUnsynchronized()方法包装测试。 像这样:

test('Test SplashScreen', () async {
  await driver.runUnsynchronized(() async {
    await driver.waitFor(find.text("DESSERT"));
  });
});

(仅当您具有无限动画或要在动画结束之前继续播放时)

答案 1 :(得分:0)

您使用了findByValueKey,但窗口小部件使用了Key而不是ValueKey

之前:

Foo(key: Key(DESSERT))

之后:

Foo(key: ValueKey(DESSERT))