我一直在尝试针对使用 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/