Sunday, September 23, 2018

Get current GPS position in iOS UITest

Leave a Comment

Is it possible to get the current GPS position of the phone/simulator in a UITest test-case?

One possible workaround would be to run a test app in the background and communicate with that app in the test-case but I want to avoid that if possible.

2 Answers

Answers 1

TL;DR:

Example project: https://github.com/JakubMazur/SO52339694


The principle of testing is that you control environment, so for testing you should be using one of predefined location or put one of your's location into .gpx file:

example:

<?xml version="1.0"?> <gpx version="1.1"> <wpt lat="50.12108905423" lon="39.9260224404235"> </wpt> </gpx> 

If you said like in comment a location in a given moment inside UITesting, that's not gonna be possible in real time, but you can combine couple of things to get location from time couple of seconds/minutes back.

Here what I would do:

  1. There is a tool called corelocationcli available through homebrew. If you install it this will give you current coordinates.

  2. Then I will create file linked in my xcode project in tests let's call it genarated.gpx and filled it with any data on this point.

  3. Then I will wrote a script like this:

code

#!/bin/bash  latitude="$(CoreLocationCLI -format \"%latitude\")" longitude="$(CoreLocationCLI -format \"%longitude\")"  echo "<?xml version=\"1.0\"?> <gpx version=\"1.1\"> <wpt lat=${longitude} lon=${latitude}> </wpt> </gpx>" > generated.gpx 
  1. And apply this as a build phase:

enter image description here

That way getting location get a couple of seconds before starting an UITests, but if you can live with this couple of seconds delay this solution should work for you.

Example project you can find on github

Answers 2

Assuming you have control over the app code, and not just the test cases code :

  • Implement location tracking in the app itself
  • Expose the current / last known location with timestamp via the tracking class' public (static?) method
  • Access it in your test case addition to the functionality being tested

This makes your test cases less robust but should get you the desired behavior

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment