代码之家  ›  专栏  ›  技术社区  ›  iupchris10

如何在用于UI测试的Xamarin窗体应用程序的选项卡页栏项上设置automationid

  •  1
  • iupchris10  · 技术社区  · 6 年前

    我正在为基于选项卡的Xamarin表单应用程序编写Xamarin UI测试。我想在每个选项卡项上设置自动化ID,以便我的UI测试可以单击特定的选项卡,而不参考选项卡的文本标签(本地化)。

    我想您需要使用一个定制的渲染器并设置ContentDescription(Android)和AccessibilityIdentifier(iOS),我一直在尝试这样做,结果是混合的。正确的方法是什么?如果我使用自定义渲染器的方法是正确的,我应该在iOS/android中重写哪个渲染器方法来实现这一点?

    更新:

    网间网操作系统: 答案由@apineda提供。在问题下面看他的解决方案。

    安卓: 似乎需要自定义渲染器。有点不舒服,但很管用。我们必须递归地搜索视图层次结构中的选项卡栏项,并为每个项设置“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);
                        }
                    }
                }
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   pinedax    6 年前

    你不需要 CustomRenderer 为此。你只需要设置 AutomationId 给孩子们 Pages 选项卡页的,并将其分配给条形图项。

    假设你有这个 TabPage 如下

    <?xml version="1.0" encoding="UTF-8"?>
    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:local="clr-namespace:MyGreatNamespace"
                 x:Class="MyGreatNamespace.MyTabPage">
        <TabbedPage.Children>
             <local:MainPage AutomationId="MainTab" Title="Main Page" />
             <local:PageOne AutomationId="TabOne" Title="Page One" />
             <local:PageTwo AutomationId="TabTwo" Title="Page Two" />
        </TabbedPage.Children>
    </TabbedPage>
    

    使用此配置,您可以执行以下操作:

    app.Tap("TabTwo");
    

    你不需要使用 Text 财产。

    希望这有帮助。-

    更新:

    刚刚确认以上不适用于Android(注意你最初的问题是针对Android),但只适用于iOS。出于某种原因,这种行为是不同的。

    您仍然可以使用文本的本地化版本“点击它”,如下所述。

    处理本地化文本时可以使用的一个技巧是设置正确的区域性,然后将XAML中的相同资源集用作测试的一部分。

    app.Tap(AppResources.MyMainTabText)

    推荐文章