vatbub
vatbub

Reputation: 3118

Xamarin UITest "Unable to set fake location on iOS Simulator"

I am testing a Xamarin Forms app on Android and iOS using Xamarin.UITest. Since the application depends on the GPS location, I need to set a fake location. I do this using

App.Device.SetLocation(latitude, longitude)

...where latitude and longitude are variables of type double which contain the desired coordinates.

This works fine on Android but fails on the iPhone Simulator with the following exception:

SetUp : Xamarin.UITest.XDB.Exceptions.DeviceAgentException : Unable to set location

ExitCode: 4

        -d,--device-id  <device-identifier> iOS Simulator GUID or 40-digit physical device ID
    set-location <latitude,longitude>
Expected lat,lng: Got 48,135831,11,573423

I did some research and found a thread on the Xamarin Forums which says that it might help to round the fake coordinates to only 4 digits after the decimal point but it didn't help. Apart from that, I tried to hardcode the coordinates and to use floats instead of doubles but neither of the attempts worked.

What's going on there and how can it be fixed?

Here's my setup:

The same issue occurred several months ago with the following setup:

At the time, I just left it alone, but now is the time where I need a fix.

Upvotes: 1

Views: 855

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

Expected lat,lng: Got 48,135831,11,573423

It appears to be a bug in that Xamarin is using your local locale to convert those doubles to strings when they send them to simctl (and simctl is expecting decimal point-based strings or quoted decimals and thus it is parsing those are 4 separate numbers).

In your test code before setting the location, change the culture to en-US and set it back later to your actual culture if needed.

Something like this should work:

var currentCulture = CultureInfo.DefaultThreadCurrentCulture;
CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");"

// do some location setting / testing

CultureInfo.DefaultThreadCurrentCulture = currentCulture;

Upvotes: 1

Related Questions