简体   繁体   English

Xamarin.UITest:如何检索列表中的所有元素

[英]Xamarin.UITest: How to Retrieve All Elements in List

I have a list of 500 elements and when I use app.Query on the page, Xamarin.UITest gives me only 6 elements as only 6 elements are visible in UI. 我有一个500个元素的列表,当我在页面上使用app.Query时,Xamarin.UITest只给了我6个元素,因为在UI中只能看到6个元素。

How can I retrieve all 500 elements from the list inside of my UITest? 如何从UITest内的列表中检索所有500个元素?

As described above, the expected behavior of app.Query will only return the results of all visible controls on the page. 如上所述, app.Query的预期行为将仅返回页面上所有可见控件的结果。 Thus, if a control is not visible, app.Query will not return it. 因此,如果控件不可见, app.Query将不会返回它。

The way to retrieve all of the data in a list is to use a Backdoor Method. 检索列表中所有数据的方法是使用后门方法。

Xamarin has additional documentation on how to use backdoors in UITest. Xamarin有关于如何在UITest中使用后门的附加文档

Sample App 示例应用程序

This sample app implements the snippets from the tutorial: https://github.com/brminnick/UITestSampleApp 此示例应用程序实现了教程中的代码段: https//github.com/brminnick/UITestSampleApp

Tutorial 教程

1. Create a Serializable Object 1.创建可序列化对象

Because Backdoor Methods are limited to return a string, we will need to be able to serialize our object. 因为Backdoor方法仅限于返回一个字符串,所以我们需要能够序列化我们的对象。

You will need to add the Newtonsoft.Json NuGet package to each of your projects; 您需要将Newtonsoft.Json NuGet包添加到每个项目中; ie add the Newtonsoft.Json NuGet to the .NET Standard project, the iOS Project, the Android Project and the UITest Project. 即将Newtonsoft.Json NuGet添加到.NET Standard项目,iOS项目,Android项目和UITest项目中。

2. Create Static Methods to Serialize the Object 2.创建静态方法以序列化对象

These methods will be used to serialize and deserialize the object. 这些方法将用于序列化和反序列化对象。

using Newtonsoft.Json;

public static class ConverterHelpers
{
    public static string SerializeObject(object value)
    {
        return JsonConvert.SerializeObject(value);
    }

    public static T DeserializeObject<T>(string value)
    {
        return JsonConvert.DeserializeObject<T>(value);
    }
}

3. Add Backdoor Method to AppDelegate 3.将后门方法添加到AppDelegate

This method in the AppDelegate will expose a backdoor from your iOS app that the UITest can utilize. AppDelegate中的此方法将从您的iOS应用程序中公开UITest可以使用的后门。

If you do not have an iOS app, skip this step. 如果您没有iOS应用程序,请跳过此步骤。

[Export("getDataAsString:")]
public NSString GetDataAsString(NSString noValue)
{
    var data = [Add code here to retrieve the data from your app]

    var dataAsString = ConverterHelpers.SerializeObject(data);

    return new NSString(dataAsString);
}

4. Add Backdoor Method to MainActivity or Application class 4.将后门方法添加到MainActivity或Application类

This method in the MainActivity (or the Application class, if you have one) will expose a backdoor from your Android app that the UITest can utilize. MainActivity中的此方法(或Application类,如果有的话)将从您的Android应用程序中公开UITest可以使用的后门。

If you do not have an Android app, skip this step. 如果您没有Android应用,请跳过此步骤。

[Export("GetDataAsString")]
public string GetDataAsString()
{
    var data = [Add code here to retrieve the data from your app]

    var dataAsBase64String = ConverterHelpers.SerializeObject(data);

    return dataAsBase64String;
}

5. Create Static Method to Invoke Backdoors from UITest 5.创建静态方法以从UITest调用后门

Create a static method in the UITest project to invoke backdoor methods from UITest. 在UITest项目中创建一个静态方法,以从UITest调用后门方法。

internal static List<DataModel> GetListData(IApp app)
{
    string dataAsString;

    if (app is iOSApp)
        dataAsString = app.Invoke("getDataAsString:", "").ToString();
    else
        dataAsString = app.Invoke("GetDataAsString").ToString();

    return ConverterHelpers.DeserializeObject<List<DataModel>>(dataAsString);
}

6. Invoke the Backdoor From The UITest 6.从UITest调用后门

In the UITest test method, implement the static method to retrieve the data. 在UITest测试方法中,实现静态方法来检索数据。

[Test]
public void VerifyData()
{
    Assert.IsTrue(GetListData(app).Count == 500);
}

对于仍在问这个问题的人,现在有一个AppQuery.All会更改查询以返回所有元素而不仅仅是可见元素。

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

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