1

How can I disable location while single UI test in xamarin forms (I am using Android 8.1 simulator)? In one test case I want cancel location permission, and in other simulate that the GPS can not find user location. I thought about backdors, but I did not found nothing useful or up to date on msdn and stack (some links pasted bellow).

I have tried something like this (and much more similar 'mutations'):

Droid.MainActivity:

[Export("GpsBackdoor")]
    public void GpsBackdoor()
    {
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.PutExtra("enabled", Java.Lang.Boolean.False);
        SendBroadcast(intent);
    }

UITests.Test:

[Test]
    public void SetCurrentUserLocation_WhenGPSisOff_ShouldShowAlert()
    {
        app.WaitForElement(x => x.Text("Nearest stops"));
        app.Invoke("GpsBackdoor");
        app.Tap(x => x.Text("Nearest stops"));

        var result = app.WaitForElement("Could not obtain position");
    }

After running test I am getting message:

System.Exception : Error while performing Invoke("GpsBackdoor", null)
----> System.Net.Http.HttpRequestException : An error occurred while sending 
the request.
----> System.Net.WebException : The underlying connection was closed: The 
connection was closed unexpectedly.
at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args,  
String memberName)
at Xamarin.UITest.Android.AndroidApp.Invoke(String methodName, Object 
argument)
at BusMap.Mobile.UITests.NearestStopsMapPageTests.
SetCurrentUserLocation_WhenGPSisOff_ShouldShowAlert() in 
<source>\NearestStopsMapPageTests.cs:line 40
--HttpRequestException
at Xamarin.UITest.Shared.Http.HttpClient.SendData(String endpoint, String 
method, HttpContent content, ExceptionPolicy exceptionPolicy, Nullable`1 
timeOut)
at Xamarin.UITest.Shared.Http.HttpClient.Post(String endpoint, String 
arguments, ExceptionPolicy exceptionPolicy, Nullable`1 timeOut)
at Xamarin.UITest.Android.AndroidGestures.Invoke(String methodName, Object[] 
arguments)
at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, 
String memberName)
--WebException
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)

I've also tried turn off Wifi or cellular data, which gave me the same error.

I will be grateful for any help.

P.S. Some links which I've found:

Enable/Disable wifi using Xamarin UiTest

How to start GPS ON and OFF programatically in Android

Kliwer
  • 13
  • 3

0 Answers0