如何在Xamarin.Forms UI测试中禁用位置

时间:2018-09-08 11:11:27

标签: xamarin xamarin.forms xamarin.android xamarin.uitest

在以xamarin形式进行单个UI测试时如何禁用位置(我正在使用Android 8.1模拟器)?在一个测试用例中,我要取消位置许可,而在另一个测试用例中,我要模拟GPS找不到用户位置。 我想到了背叛者,但在msdn和堆栈(某些链接粘贴在下面)上没有发现有用或最新的内容。

我已经尝试过类似的事情(以及更多类似的“突变”):

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");
    }

运行测试后,我收到消息:

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)

我还尝试关闭Wifi或蜂窝数据,这给了我同样的错误。

我将不胜感激。

P.S。我找到的一些链接:

Enable/Disable wifi using Xamarin UiTest

How to start GPS ON and OFF programatically in Android

0 个答案:

没有答案