【问题标题】:FlutterDriver problem, not able to find Widget by KeyFlutterDriver 问题,无法通过 Key 找到 Widget
【发布时间】:2019-10-22 20:34:29
【问题描述】:

我在 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);

小部件文件(2 部分): 第 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".

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

【问题讨论】:

    标签: flutter integration-testing


    【解决方案1】:

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

    flutter 驱动默认是帧同步的,它会一直等到没有挂起的帧,但如果有无限动画,测试会超时失败。

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

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

    (仅当您有无限动画或者您想在动画结束之前继续)

    【讨论】:

    • 花了 8 个小时试图解决这个问题。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 2021-01-29
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多