xamarin - 测试记录器和 Xamarin 表单 - Id 不适用于 UITest

标签 xamarin xamarin.forms uitest

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

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

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

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

它应该具备正确地针对 UITest 工作的一切,包括 MainActivity 中的 StyleId 和代码,以将 StyleId 映射到 ContentDescriptions。

我在 REPL 终端 session 中运行 tree 来检查屏幕层次结构,它似乎没有显示应该具有它们的控件的 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 本质上只是一个占位符,用于传输到 native 对应部分。

对于 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);
        }

然后您应该开始看到 Xamarin Forms 元素的 Id。

关于xamarin - 测试记录器和 Xamarin 表单 - Id 不适用于 UITest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34212482/

相关文章:

java - 连接到布局渲染器失败。这可能是Java配置错误导致的

c# - 在 Xamarin Android 和 C#.Net 中使用 AsyncTask 计算多个网站的加载时间

xaml - 找不到关键 TextToBoolConverter 的静态资源

android - Xamarin.Forms (Android) - 签名 APK - 应用程序崩溃版本 5.1 (API 22)

xamarin - 如何在我的 Xamarin Forms 应用程序上本地保存一些用户数据?

c# - Monodroid - 处理重用 ListView 行的 subview 上的事件

ios - Xamarin Monotouch Google Conversion Tracking 以跟踪实际应用下载

android - 点击回收站 View 上的第一个匹配文本 Android espresso 测试

ios - 无法加载 UITests 包,因为它已损坏或缺少必要的资源。尝试重新安装 bundle

iOS 用户界面测试 : How to find UITableViewCell's AccessoryView?