Xamarin Forms 上的 UITest 将 StyleId 写为标签而不是 ID

UITest on Xamarin Forms writing StyleId as Label rather than ID

我正在尝试在 Xamarin Forms 项目上设置 UI 测试

我按照指南 here 将 StyleId 添加到我的用户名控件中

<Entry x:Name="username_name" 
        StyleId="username_styleid"
        Text="{Binding Username, Mode=TwoWay}"
        IsEnabled="{Binding IsBusy, Converter={StaticResource ReverseBoolConverter}}"
        Style="{StaticResource TextboxLight}"
        Placeholder="{Binding UsernameLabel}" />

我将代码添加到 Android MainActivity

Forms.ViewInitialized += (sender, e) => {
    if (!string.IsNullOrWhiteSpace(e.View.StyleId))
    {
        e.NativeView.ContentDescription = e.View.StyleId;
    }
};

当我 运行 测试并使用 REPL 时,我可以看到 StyleId 已作为标签 属性 而不是 id 属性

输出

有没有人设法让它工作?

在 2.2.0 中使用 StyleId 很快就会过时。

我还没有亲自测试过,但是

AutomationId Support

Xamarin.Forms now has first class support for setting automation identifiers for usage with Xamarin UITest or other testing frameworks. Simply setting the AutomationID property should allow the automation framework to find and interact with your controls.

不确定 AutomationId 转换成什么,希望是 ID。

这是按设计工作的。在 iOS 上,您会看到该值显示为 id;在 Android 上它将显示为 label

app.Query(c=>c.Marked("username_styleid")) 将跨平台工作。 app.Query("username_styleid") 的较短形式做同样的事情。

Marked 将在 iOS 的 id 字段中查找具有指定值的元素; Android 上的 label 字段和任一字段上的 text。因此,在编写测试时使这一点变得简单和有用的关键是使 StyleID(或 AutomationId)不同于出现在您的应用程序中的文本。