2017-08-25 139 views
2

我想在Swift中編寫UI測試,在我們的應用中製作地圖各個地方的截圖。爲了做到這一點,我需要在測試過程中模擬僞造的GPS數據。以編程方式模擬iOS測試中的GPS位置

有一些像這樣的解決方案(https://blackpixel.com/writing/2016/05/simulating-locations-with-xcode-revisited.html)使用GPX文件並在Xcode中模擬Debug > Simulate Location的位置,但我需要這個完全自動化。理想情況將類似於Android中的LocationManager

回答

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() 
0

你不能。 CLLocationManager在委託人的幫助下給你你的位置,你有任何設置這個位置的方法。

您可以創建一個CLLocationManager模擬器類,它可以提供一些位置的時間。 或者您可以將您的測試與時間戳GPX同步。

+0

這是嚴重不可能的嗎? 所以我最好的選擇是使用時間戳GPX文件並在每次位置更改時都截圖。 – Nbfour

相關問題