How to run UITests with map view in iOS
Issue #45
Mock a location
You should mock a location to ensure reliable test
Create the gpx file
Go to Xcode -> File -> New -> GPX File
It looks like
1 |
|
The gpx
file is very powerful, as it allows you to specify a route with different movement speed.
Provide one or more waypoints containing a latitude/longitude pair. If you provide one
waypoint, Xcode will simulate that specific location. If you provide multiple waypoints,
Xcode will simulate a route visiting each waypoint.
Optionally provide a time element for each waypoint. Xcode will interpolate movement
at a rate of speed based on the time elapsed between each waypoint. If you do not provide
a time element, then Xcode will use a fixed rate of speed. Waypoints must be sorted by time in ascending order.
Use the gpx file
- Declare the
gpx
file in app target, not UITests target. Go to yourapp scheme -> Run -> Options
- Go to
Simulator -> Debug -> Location -> Custom Location
and select that same location, just to make sure. It does not need to be the same, but I see that withoutCustom Location
, it does not work inUITests
Test that you’re near the initial location
1 | let map = app.maps.element(boundBy: 0) |
The wait
function is from https://github.com/onmyway133/blog/issues/44
Test that you can interact with your custom pin
You need to specify accessibilityIdentifier
, like
1 | class MyPin: MKAnnotationView { |
and then query for that pin. Not that it is not inside map
, it is inside app
1 | let pin = app.otherElements.matching(identifier: "myPin").element(boundBy: 0) |
You should use accessibilityIdentifier
accessibilityIdentifier
is from UIAccessibilityIdentification
protocol. You should not use accessibilityLabel
, see https://github.com/kif-framework/KIF/issues/243
Given that accessibilityLabel is an outwardly-facing string that is actually used by accessibility screen readers (and should be localized to the device user’s language), Apple now provides an alternate property (iOS 5+) that is specifically intended for UI Automation purposes