如何让‘navigation.push()’适用于Flutter中的集成测试?

问题描述 投票:0回答:1

这是我第一次在 Flutter 中使用集成测试。

我写了下面的测试代码:

testWidgets('Navigate to password page', (tester) async {

      // ! Launch the app.
      app.main();
      await tester.pumpAndSettle();
      await Future.delayed(const Duration(seconds: 2));

      // ! Tap on the enter user login page
      await tester.tap(find.byKey(const Key('enter_login_page_btn')));
      await tester.pumpAndSettle();

      // Check if the new page is displayed correctly
      expect(find.byKey(const Key('page_user_login')), findsWidgets);

      // Enter text into the login/email input field.
      await tester.enterText(
          find.byKey(const Key('user_input_login')), "[email protected]");
      await tester.testTextInput.receiveAction(TextInputAction.done);
      await tester.pumpAndSettle();
      await Future.delayed(const Duration(seconds: 2));

      // ! Tap on the validate user login button.
      await tester.tap(find.byKey(const Key('validate_login_btn')));
      await tester.pumpAndSettle();
      await Future.delayed(const Duration(seconds: 2));
}

这是按下“登录”按钮时的代码:

Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => PasswordPage(
                  email ?? '',
                  key: const Key('page_user_password'), 
                ),
                maintainState: true,
              ),
            );

在正常的手动测试中,导航工作正常,但是当我尝试使用集成测试对其进行测试时,没有任何反应,它不会导航到下一页,即密码页面,并且测试结束。

有人知道为什么导航在集成测试中不起作用吗?

android ios flutter dart cross-platform
1个回答
0
投票
  1. 检查小部件树:确保小部件存在并可使用 find.byType 或 find.byKey 进行访问。
  2. 确保正确的小部件键:确认键是唯一的且分配正确。 等待导航:使用 tester.pumpAndSettle() 等待导航完成。
  3. 检查导航器堆栈:验证导航堆栈是否已正确更新。 处理异步操作:使用await正确处理异步任务。 调试:使用打印语句或断点来调试导航。 模拟依赖项:考虑模拟外部依赖项以获得一致的行为。
© www.soinside.com 2019 - 2024. All rights reserved.