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

时间:2017-08-25 07:45:00

标签: ios swift gps automated-tests xctest

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

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

2 个答案:

答案 0 :(得分:2)

我在编写UI测试时遇到过类似的问题,因为模拟器/系留设备无法完成您想要的所有操作。我所做的就是编写模仿所需行为的模拟(我通常无法控制的行为)。

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

创建一个类MyLocationManager,使其成为CLLocationManager的子类,并使其覆盖您调用的所有方法。不要在重写的方法中调用super,因为CLLocationManager实际上不应该实际接收方法调用。

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方法中插入以下代码行:

app.launchArguments.append("is_ui_testing")

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

static var locationManger: CLLocationManager = ProcessInfo.processInfo.arguments.contains("is_ui_testing") ? MyLocationManager() : CLLocationManager()

答案 1 :(得分:0)

你不能。 CLLocationManager在代表的帮助下为您提供位置,您可以设置此位置。

您可以创建一个CLLocationManager模拟器类,该类可以随时提供某些位置。 或者,您可以将测试与带时间戳的GPX同步。

相关问题