向 Flutter Driver 授予 iOS 权限以进行集成测试

问题描述

使用 Flutter integration_test 包时,我遇到了一个由于无法从测试环境访问的 iOS 权限请求而不断失败的测试的问题。

我尝试了 github.com/wix/AppleSimulatorUtils 中的 applesimutils,但是在 Flutter Drive 之前尝试设置权限时,应用程序的包标识符尚未注册

Got error:
An error was encountered processing the command (domain=NSPOSIXErrorDomain,code=2):
The operation Couldn’t be completed. No such file or directory
No such file or directory

也许这可以通过不每次都创建一个新的 Simulator 实例来避免,但是我通过在 Flutter drive ... & sleep 10 命令之前运行 applesimutils --setPermissions 以允许应用程序首先启动来解决这个问题,但这仍然给出我:

══╡ EXCEPTION CAUGHT BY FlutteR TEST FRAMEWORK ╞═════════════════
The following LocationError object was thrown running a test:
  [LocationError code: 0,message: null]

When the exception was thrown,this was the stack:
#2   BackgroundGeolocation.getCurrentPosition.<anonymous closure>
  (package:Flutter_background_geolocation/models/background_geolocation.dart:497:17)

...

解决方法

更熟悉 xcrun simctl 后,我注意到它可以在应用程序启动之前授予权限,只要设备已启动。 applesimutils 不需要!

我的 shell 脚本形式的工作解决方案如下所示:

# Simulator setup
xcrun simctl create iOS14Simulator
xcrun simctl boot iOS14Simulator
xcrun simctl privacy iOS14Simulator grant location-always <YOUR_BUNDLE_ID>

# Launch integration test
flutter drive \
  --driver=test_driver/integration_test_driver.dart \
  --target=integration_test/app_test.dart

# Simulator teardown
xcrun simctl delete iOS14Simulator