FlutterDriver 问题,无法通过 Key 找到 Widget

Han*_*l T 11 integration-testing flutter

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


    });
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

小部件文件(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"))
  ];
Run Code Online (Sandbox Code Playgroud)

第 2 部分:底部导航栏

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

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

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

实际结果 :

抢先看:

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.
Run Code Online (Sandbox Code Playgroud)
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".
Run Code Online (Sandbox Code Playgroud)

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

Til*_*ebe 14

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

Flutter 驱动程序默认是帧同步的,它会一直等到没有待处理的帧,但如果有无限动画,测试将进入超时并失败。

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

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

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

  • 花了8个小时试图解决这个问题。多谢 (6认同)