首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS测试中以编程方式模拟GPS位置

在iOS测试中以编程方式模拟GPS位置
EN

Stack Overflow用户
提问于 2017-08-25 15:45:00
回答 2查看 3.3K关注 0票数 5

我想在Swift中编写UI测试,在我们的应用程序中制作地图的不同位置的屏幕截图。为了做到这一点,我需要在测试期间模拟假GPS数据。

有一些像这样的解决方案(https://blackpixel.com/writing/2016/05/simulating-locations-with-xcode-revisited.html),它使用GPX文件并在Xcode中使用Debug > Simulate Location模拟位置,但我需要这是完全自动化的。理想的应用应该是类似于安卓系统中的LocationManager

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-28 02:04:36

我在编写UI测试时也遇到过类似的问题,因为模拟器/系留设备不能做你想要的一切。我所做的就是编写模仿所需行为(一些我通常无法控制的行为)的模拟。

用定制的位置管理器替换CLLocationManager将允许您完全控制位置更新,因为您可以通过CLLocationManagerDelegate方法以编程方式发送位置更新:locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

创建一个MyLocationManager类,使其成为CLLocationManager的子类,并让它覆盖您调用的所有方法。不要在被覆盖的方法中调用超级函数,因为CLLocationManager永远不会实际接收方法调用。

代码语言:javascript
复制
class MyLocationManager: CLLocationManager {
  override func requestWhenInUseAuthorization() {
    // Do nothing.
  }

  override func startUpdatingLocation() {
    // Begin location updates. You can use a timer to regularly send the didUpdateLocations method to the delegate, cycling through an array of type CLLocation.
  }

  // Override all other methods used.

}

delegate属性不需要重写(也不能重写),但您可以作为CLLocationManager的子类访问它。

要使用MyLocationManager,你应该传入启动参数,告诉你的应用程序它是否是一个UITest。在测试用例的setUp方法中,插入下面这行代码:

代码语言:javascript
复制
app.launchArguments.append("is_ui_testing")

将CLLocationManager存储为测试时为MyLocationManager的属性。当不测试时,将照常使用CLLocationManager。

代码语言:javascript
复制
static var locationManger: CLLocationManager = ProcessInfo.processInfo.arguments.contains("is_ui_testing") ? MyLocationManager() : CLLocationManager()
票数 6
EN

Stack Overflow用户

发布于 2017-08-25 20:01:44

你不能。CLLocationManager会在委派的帮助下提供你的位置,你可以通过任何方法来设置这个位置。

您可以创建一个CLLocationManager模拟器类,在一段时间内提供一些位置。或者你可以把你的测试和带时间戳的GPX同步。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45876613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档