我想在Swift中编写UI测试,在我们的应用程序中制作地图的不同位置的屏幕截图。为了做到这一点,我需要在测试期间模拟假GPS数据。
有一些像这样的解决方案(https://blackpixel.com/writing/2016/05/simulating-locations-with-xcode-revisited.html),它使用GPX文件并在Xcode中使用Debug > Simulate Location模拟位置,但我需要这是完全自动化的。理想的应用应该是类似于安卓系统中的LocationManager。
发布于 2017-08-28 02:04:36
我在编写UI测试时也遇到过类似的问题,因为模拟器/系留设备不能做你想要的一切。我所做的就是编写模仿所需行为(一些我通常无法控制的行为)的模拟。
用定制的位置管理器替换CLLocationManager将允许您完全控制位置更新,因为您可以通过CLLocationManagerDelegate方法以编程方式发送位置更新:locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])。
创建一个MyLocationManager类,使其成为CLLocationManager的子类,并让它覆盖您调用的所有方法。不要在被覆盖的方法中调用超级函数,因为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()发布于 2017-08-25 20:01:44
你不能。CLLocationManager会在委派的帮助下提供你的位置,你可以通过任何方法来设置这个位置。
您可以创建一个CLLocationManager模拟器类,在一段时间内提供一些位置。或者你可以把你的测试和带时间戳的GPX同步。
https://stackoverflow.com/questions/45876613
复制相似问题