测试记录器和 Xamarin Forms - Id 不适用于 UITest

Test Recorder and Xamarin Forms - Id not available for UITest

我一直在尝试 运行 针对 Android 和 iOS 使用 Xamarin Forms 创建的应用程序测试记录器。当我单击测试记录器中的控件时,我没有看到生成的 C# 文件中放置的 Id,只有 class 类型。

我决定从等式中删除测试记录器并尝试针对 Xamarin Forms Android 应用进行基本 UI 测试 运行ning。

我从这里下载了一个演示应用程序:

https://developer.xamarin.com/samples/xamarin-forms/UsingUITest/

它应该准备好一切以针对UI测试正确工作,包括 StyleId 和 MainActivity 中的代码以将 StyleId 映射到 ContentDescriptions。

我在 REPL 终端会话中 运行 树来检查屏幕层次结构,它似乎没有显示应该有它们的控件的 ID。

>>> tree
[[object CalabashRootView] > PhoneWindow$DecorView]
  [ActionBarOverlayLayout] id: "decor_content_parent"
    [FrameLayout > ... > Platform_DefaultRenderer] id: "content"
      [ButtonRenderer]
        [Button] label: "MyButton",  text: "Click me"
      [LabelRenderer]
        [FormsTextView] label: "MyLabel",  text: "Hello, Xamarin.Forms!"
  [View] id: "navigationBarBackground"
  [View] id: "statusBarBackground"
>>>

有什么想法吗?

在 Xamarin Forms 中,StyleId 本质上只是一个占位符,用于传输到本机计数器部分。

对于 Android 确保你在 MainActivity.cs

中有这个
Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) => {
                if (!string.IsNullOrWhiteSpace(e.View.StyleId))
                {
                    e.NativeView.ContentDescription = e.View.StyleId;
                }
            };

在 iOS 中执行此操作

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) => {
                if (null != e.View.StyleId)
                {
                    e.NativeView.AccessibilityIdentifier = e.View.StyleId;
                }
            };

            LoadApplication(new App());

#if ENABLE_TEST_CLOUD
// requires Xamarin Test Cloud Agent
Xamarin.Calabash.Start();
#endif

            return base.FinishedLaunching(app, options);
        }

然后您应该开始看到您的 ID 出现在您的 Xamarin Forms 元素中。