使用 flutter_driver 查找 flutter sandwich 菜单按钮

Find flutter sandwich menu button using flutter_driver

使用 flutter_driver

查找 flutter sandwich 菜单按钮

    Scaffold(
        key: widget.scaffoldKey,
        drawer: Observer(builder: (BuildContext context) {
          return DrawerMenu(
            user: controller.appStore.user,
            onTapLogout: controller.loginController.logout,
          );
        }),
        appBar: widget.appBar,
        body: widget.body);

button to be identified


 test('test by inserting valid username and login', () async {
      await driver.tap(buttonFinder);

      expect(await driver.getText(loginName), "LoginName");
    });

您所指的三明治菜单名为 Drawer,您可以使用 toolTip 属性 点击它。 如果你长按抽屉菜单,它会显示一个工具提示,如下:

然后,在您的 flutter 驱动程序测试中,您可以通过使用 byToolTip 方法声明一个 finder 来识别此小部件,如下所示:

final drawerFinder = find.byTooltip('Open navigation menu');

然后将测试写成:

test('tap on drawer menu', () async {
        await driver.waitFor(drawerFinder);
        await driver.tap(drawerFinder);
        print('clicked on drawer');
      });

不是 100% 安全但允许您不使用工具提示的解决方案是:

test('OpenDrawer', () async {
      await driver.tap(find.descendant(
        of: find.byType('AppBar'),
        matching: find.byType('IconButton'),
        firstMatchOnly: true,
      ));
    });