Xamarin.UITest:如何检索列表中的所有元素 [英] Xamarin.UITest: How to Retrieve All Elements in List

查看:114
本文介绍了Xamarin.UITest:如何检索列表中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有500个元素的列表,当我在页面上使用app.Query时,Xamarin.UITest仅给我6个元素,因为UI中只有6个元素可见.

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.

如何从UITest内部的列表中检索所有500个元素?

How can I retrieve all 500 elements from the list inside of my UITest?

推荐答案

如上所述,app.Query的预期行为将仅返回页面上所有可见控件的结果.因此,如果控件不可见,则app.Query不会返回它.

As described above, the expected behavior of app.Query will only return the results of all visible controls on the page. Thus, if a control is not visible, app.Query will not return it.

检索列表中所有数据的方法是使用后门方法.

The way to retrieve all of the data in a list is to use a Backdoor Method.

Xamarin具有

Xamarin has additional documentation on how to use backdoors in UITest.

此示例应用程序实现了教程中的片段: https://github.com/brminnick/UITestSampleApp

This sample app implements the snippets from the tutorial: https://github.com/brminnick/UITestSampleApp

由于后门方法仅限于返回字符串,因此我们需要能够序列化对象.

Because Backdoor Methods are limited to return a string, we will need to be able to serialize our object.

您需要将 Newtonsoft.Json NuGet程序包添加到每个程序包中您的项目;也就是说,将Newtonsoft.Json NuGet添加到.NET Standard项目,iOS项目,Android项目和UITest项目中.

You will need to add the Newtonsoft.Json NuGet package to each of your projects; i.e. add the Newtonsoft.Json NuGet to the .NET Standard project, the iOS Project, the Android Project and the UITest Project.

这些方法将用于序列化和反序列化对象.

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.将后门方法添加到AppDelegate

AppDelegate中的此方法将从iOS应用暴露UITest可以利用的后门.

3. Add Backdoor Method to AppDelegate

This method in the AppDelegate will expose a backdoor from your iOS app that the UITest can utilize.

如果您没有iOS应用,请跳过此步骤.

If you do not have an iOS app, skip this step.

[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.将Backdoor方法添加到MainActivity或Application类中

MainActivity(或Application类,如果有的话)中的此方法将公开UITest可以利用的Android应用程序的后门.

4. Add Backdoor Method to MainActivity or Application class

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.

如果您没有Android应用,请跳过此步骤.

If you do not have an Android app, skip this step.

[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.创建静态方法以从UITest调用后门

在UITest项目中创建一个静态方法,以从UITest调用后门方法.

5. Create Static Method to Invoke Backdoors from UITest

Create a static method in the UITest project to invoke backdoor methods from 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.从UITest调用后门

在UITest测试方法中,实现静态方法以检索数据.

6. Invoke the Backdoor From The UITest

In the UITest test method, implement the static method to retrieve the data.

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

这篇关于Xamarin.UITest:如何检索列表中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆