在xaml子元素中添加AutomationId

时间:2019-07-09 10:56:03

标签: c# xaml inheritance automation ui-automation

我目前正在从事自动化UI测试支持,不幸的是,这是第一次。我在无法添加AutomationId以使其出现在生成的xml文件的所需元素中的问题上感到困惑。

问题是该元素是另一个元素的子元素,当我添加AutomationId时(无论是在C#代码中还是在XAML中),该AutomationId仅出现在最顶部的xml元素中,该元素是一些矩形面板(按钮),但是在此按钮中还有一些其他元素如图片,按钮标签,带有计数器的标签。也许将来测试人员也需要访问这些子元素,但现在我只能通过ID找到最上方的矩形区域。

这里是元素SomeEntryView及其父元素View的代码结构,该代码结构在创建布局并将元素添加到此布局时用于代码中。

public partial class SomeEntryView : MainMenuButton
{ ...

public partial class MainMenuButton : ContentView, IDisposable
...

public class ContentView : TemplatedView
...

public class TemplatedView : Layout
...

public abstract class Layout : View

xaml中的元素是通过这种方式定义的

<c:MainMenuButton
x:Class="...Views.SomeEntryView"
AutomationId="automationIdTest"
... />

结果xml文件如下所示

<Custom AcceleratorKey="" AccessKey="" AutomationId="automationIdTest" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.13" x="27" y="151" width="1868" height="148">
    <Custom AcceleratorKey="" AccessKey="" AutomationId="" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.17" x="27" y="151" width="1868" height="148">
            <Custom AcceleratorKey="" AccessKey="" AutomationId="" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.18" x="51" y="165" width="1830" height="110">
                <Custom AcceleratorKey="" AccessKey="" AutomationId="" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.19" x="51" y="175" width="286" height="100">
                    <Image AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Image" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="image" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.21" x="51" y="175" width="106" height="100" />
                        <Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="This is label for entry view" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.22" x="174" y="211" width="163" height="28" />
                </Custom>
                <Custom AcceleratorKey="" AccessKey="" AutomationId="" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.20" x="1829" y="165" width="52" height="52">
                    <Custom AcceleratorKey="" AccessKey="" AutomationId="" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="custom" Name="" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.23" x="1829" y="165" width="52" height="52">
                        <Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="109" Orientation="None" ProcessId="8804" RuntimeId="42.267070.4.24" x="1834" y="177" width="41" height="28" />
                </Custom>
            </Custom>
        </Custom>
    </Custom>
</Custom>

如您所见,最顶层的元素上有AutomationId="automationIdTest",但是是否有办法以某种方式将AutomationId添加到其子元素中?在将此MainMenuButton作为View添加到布局中时,我还尝试通过C#代码添加AutomationId。

0 个答案:

没有答案