【发布时间】:2019-06-21 21:34:18
【问题描述】:
我正在 Xamarin UI 测试中为基于选项卡的 Xamarin Forms 应用程序编写测试。我想在每个选项卡项上设置自动化 ID,以便我的 UI 测试可以单击特定选项卡,而无需参考已本地化的选项卡的文本标签。
我想您需要使用自定义渲染器并设置 ContentDescription (Android) 和 AccessibilityIdentifier (iOS),我一直在尝试这样做,但结果好坏参半。这样做的正确方法是什么?如果我使用自定义渲染器走在正确的轨道上,我应该在 IOS/Android 中覆盖哪个渲染器方法来实现这一点?
更新:
iOS: 答案由@apineda 提供。在问题下方查看他的解决方案。
Android:似乎需要自定义渲染器。这有点恶心,但它有效。我们必须递归搜索选项卡栏项目的视图层次结构并为每个项目设置“ContentDescription”。由于我们使用的是底部导航栏,因此我们向后搜索以获得更好的性能。对于顶部导航栏,您需要搜索“TabLayout”而不是“BottomNavigationItemView”。
[assembly: ExportRenderer(typeof(MainPage), typeof(CustomTabbedPageRenderer))]
namespace Company.Project.Droid.CustomRenderers
{
public class CustomTabbedPageRenderer : TabbedRenderer
{
private bool tabsSet = false;
public CustomTabbedPageRenderer(Context context)
: base(context)
{
}
protected override void DispatchDraw(Canvas canvas)
{
if (!tabsSet)
{
SetTabsContentDescription(this);
}
base.DispatchDraw(canvas);
}
private void SetTabsContentDescription(Android.Views.ViewGroup viewGroup)
{
if (tabsSet)
{
return;
}
// loop through the view hierarchy backwards. this will work faster since the tab bar
// is at the bottom of the page
for (int i = viewGroup.ChildCount -1; i >= 0; i--)
{
var menuItem = viewGroup.GetChildAt(i) as BottomNavigationItemView;
if (menuItem != null)
{
menuItem.ContentDescription = "TabBarItem" + i.ToString();
// mark the tabs as set, so we don't do this loop again
tabsSet = true;
}
else
{
var viewGroupChild = viewGroup.GetChildAt(i) as Android.Views.ViewGroup;
if (viewGroupChild != null && viewGroupChild.ChildCount > 0)
{
SetTabsContentDescription(viewGroupChild);
}
}
}
}
}
}
【问题讨论】:
标签: xamarin xamarin.forms xamarin.ios xamarin.android xamarin.uitest