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

Grant iOS permissions to Flutter Driver for integration test

使用 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

也许可以通过不每次都创建一个新的模拟器实例来避免这种情况,但是我在 applesimutils --setPermissions 命令之前通过 运行 flutter drive ... & sleep 10 克服了这个问题以允许应用程序启动首先,但这仍然给了我:

══╡ 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