You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Patrol测试包做谷歌登录集成测试时遇NativeAutomator未初始化错误

使用Patrol测试无法点击邮箱

我在测试谷歌登录功能时,无法选择指定的邮箱账号。

遇到的错误如下:

_AssertionError ('package:patrol/src/custom_finders/patrol_tester.dart': Failed assertion: line 131 pos 7: 'nativeAutomator != null': NativeAutomator未初始化,请确保已向patrolTest()传入nativeAutomation: true,且未在测试中初始化任何绑定。)

以下是代码片段:

void main() {
  patrolTest(
    'signs up',
    (PatrolTester $) async {
      IntegrationTestWidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      configureDependencies(testing: true);
      // runZoneGuarded(() => runApp(MyApp));
      await $.pumpWidget(const MyApp());

      final getStartedButton = find.byKey(const Key("getStartedButton"));
      await $.tap(getStartedButton);
      await $.pumpAndSettle();
      await $('LOGIN WITH GOOGLE').tap();
      await $.native.tap(Selector(text: 'Neel Kothari'));
      await $.pumpAndSettle();
    },
    
  );
}
解决办法
  • 开启原生自动化支持:在patrolTest的参数里添加nativeAutomation: true,这是调用$.native相关方法的必要前提。
  • 移除手动绑定初始化:删掉IntegrationTestWidgetsFlutterBinding.ensureInitialized();,Patrol会自动处理绑定初始化,手动初始化会导致NativeAutomator无法正常实例化。

修改后的代码:

void main() {
  patrolTest(
    'signs up',
    (PatrolTester $) async {
      await Firebase.initializeApp();
      configureDependencies(testing: true);
      await $.pumpWidget(const MyApp());

      final getStartedButton = find.byKey(const Key("getStartedButton"));
      await $.tap(getStartedButton);
      await $.pumpAndSettle();
      await $('LOGIN WITH GOOGLE').tap();
      await $.native.tap(Selector(text: 'Neel Kothari'));
      await $.pumpAndSettle();
    },
    nativeAutomation: true, // 新增配置
  );
}

内容的提问来源于stack exchange,提问作者neelkothari6191

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:10:03