简体   繁体   English

使用Xamarin.UITest时如何伪造后台活动

[英]How to fake background activity while using Xamarin.UITest

I'm creating a Xamarin.Forms App and want to use UITest to test the correct behavior of my View. 我正在创建一个Xamarin.Forms应用程序,并希望使用UITest测试我的View的正确行为。 The View does not only react on user input like button click or the like. 视图不仅会对按钮单击之类的用户输入做出反应。 Instead there are background operations which should lead to changes in the View like hiding one element and showing another element at this point. 取而代之的是,有一些后台操作会导致View发生变化,例如此时隐藏一个元素并显示另一个元素。 Another example is filling a ListView with elements which would be produced by a background operation. 另一个示例是用背景操作生成的元素填充ListView。 These changes would be made on the ViewModel whose properties are bound to the View. 这些更改将在其属性绑定到View的ViewModel上进行。 Now I want to simulate the background operation and test that my View behaves correctly. 现在,我想模拟后台操作并测试View的行为是否正确。 But I can not manipulate my ViewModel while in the UITest project because I cannot reference Xamarin.Forms in the Test class. 但是我无法在UITest项目中操作ViewModel,因为无法在Test类中引用Xamarin.Forms。 It seems like it isn't intended to test the application this way. 似乎并不打算以此方式测试应用程序。 The whole app is a black box for the UITest and you can only interact through mouse and keyboard input with it. 整个应用程序是UITest的黑匣子,您只能通过鼠标和键盘输入与之交互。 How can I access internals of my app, the corresponding ViewModel for example. 如何访问应用程序的内部部件,例如相应的ViewModel。 I already searched for this problem but found nothing. 我已经搜索了此问题,但一无所获。 Maybe I'm searching in the wrong direction. 也许我在寻找错误的方向。 Any help would be highly appreciated. 任何帮助将不胜感激。

You can use a back door to access a method in a platform project, and from there you should be able to access Forms code since your app project references the Forms core project. 您可以使用后门访问平台项目中的方法,并且从那里您应该能够访问Forms代码,因为您的应用程序项目引用了Forms核心项目。 See: https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/ 参见: https : //developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/

Create backdoor method in Android project: 在Android项目中创建后门方法:

In MainActivity: 在MainActivity中:

[Export("MyBackdoorMethod")]
// Can optionally take a parameter of string, int, or bool.
// Can optionally return  string or Java.Lang.String. 
public void MyBackdoorMethod() 
{
    // In through the backdoor - do some work.
}

To call the Android backdoor method from a test: 要通过测试调用Android后门方法,请执行以下操作:

app.Invoke("MyBackdoorMethod");

Create backdoor method in iOS project: 在iOS项目中创建后门方法:

In AppDelegate: 在AppDelegate中:

[Export("myBackdoorMethod:")] // notice the colon at the end of the method name
// Must take an NSString and return an NSString.
public NSString MyBackdoorMethod(NSString value)
{
    // In through the backdoor - do some work.
}

To call the iOS backdoor method from a test: 要通过测试调用iOS后门方法:

app.Invoke("myBackdoorMethod:", "the value");

More details at the link, but this should suffice to get one going. 链接中有更多详细信息,但这足以满足要求。

On iOS, you already have a method to achieve it called SendAppToBackground . 在iOS上,您已经有一种名为SendAppToBackground的方法来实现它。 You can also pass the time to be in the background with a TimeSpan object (wiki info) . 您还可以通过TimeSpan对象(Wiki信息)将时间传递到后台。 However, you cannot achieve this on Android. 但是,您无法在Android上实现此目的。

Here is a sample to use it on your UITest project: 这是在UITest项目上使用它的示例:

public void SendAppToBackground(IApp app, TimeSpan timeSpan)
{
    if (OnAndroid)
    {
        return;
    }

    ((iOSApp)app).SendAppToBackground(timeSpan);

    app.Screenshot("Return from background state.");

    return this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM