【问题标题】:how to automate xceed updown controls如何自动化 xceed updown 控制
【发布时间】:2017-06-07 00:54:41
【问题描述】:

对于我的 WPF 应用程序,我正在使用 TestStack.White 进行 UI 自动化测试。
我在我的应用程序中使用 xceed wpf 工具包中的 DoubleUpDown 控件。
如何在我的 UI 自动化测试中访问 DoubleUpDown 控件?

【问题讨论】:

    标签: ui-automation white-framework


    【解决方案1】:

    通过使用UIA Verify,您可以看到DoubleUpDown 控件被视为三个没有层次结构信息和以下AutomationIds 的控件:

    • 自动选择文本框
    • Part_IncreaseButton
    • Part_DecreaseButton

    因此您可以将它们作为普通控件自动化,但如果您在同一个窗口中有多个 DoubleUpDown 控件,则会出现问题,因为所有控件都将具有相同的 AutomationId。

    这是一个示例应用程序,其中两个第一个文本框作为 DoubleUpDown 控件,第三个作为为自动化设计的自定义文本框。

    ...
    <Label Content="Label for DoubleUpDown1" Grid.Row="0" Grid.Column="0" 
                        FontSize="15" Background="Aqua"/>
    
    <xctk:DoubleUpDown Name="test1" Grid.Row="0" Grid.Column="1"
                       FormatString="F3" Value="1564.6749586" Increment=".001"  Maximum="200000.599" 
                       AutomationProperties.AutomationId="006" AutomationProperties.Name="NormalDoubleUpDown1" />
    
    <Label Content="Label for DoubleUpDown2" Grid.Row="1" Grid.Column="0" 
                        FontSize="15" Background="Aqua"/>
    
    <xctk:DoubleUpDown Name="test2" Grid.Row="1" Grid.Column="1"
                       FormatString="F3" Value="1564.6749586" Increment=".001"  Maximum="300000.751" 
                       AutomationProperties.AutomationId="007" AutomationProperties.Name="NormalDoubleUpDown2" />
    
    <Label Content="Label for MyDoubleUpDown" Grid.Row="2" Grid.Column="0" FontSize="15" Background="Aqua" />
    
    <local:MyDoubleUpDown x:Name="test3"  Grid.Row="2" Grid.Column="1"                             
                       FormatString="F3" Value="1564.7749586" Increment=".001"  Maximum="200000.599" 
                       AutomationProperties.AutomationId="008" AutomationProperties.Name="My Custom DoubleUpDown" />
    ...
    

    在 UIA 验证中,正常的 DoubleUpDown 控件显示为具有相同的 AutomationId。 自定义的会显示真实的层次结构,并且可以使用在 XAML 中设置的 AutomationId(此处为 008)。


    自定义控件 MyDoubleUpDown,Xceed 的简单子类,但具有自动化对等体。

    public class MyDoubleUpDown : Xceed.Wpf.Toolkit.DoubleUpDown
    {
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new MyDoubleUpDownAutomationPeer(this);
        }
    }
    
    public class MyDoubleUpDownAutomationPeer : FrameworkElementAutomationPeer
    {
        public MyDoubleUpDownAutomationPeer(MyDoubleUpDown owner)
            : base(owner)
        {
        }
    }
    

    这是在窗口中自动执行独特的 DoubleUpDown 控件的默认方式。

    // link to the application and retrieve the main window
    Application application = Application.Attach("WpfTestApplication1");
    var windows = application.GetWindows();
    var window = windows.FirstOrDefault();
    
    // get the child components
    TextBox theEdit = window.Get<TextBox>("AutoSelectTextBox");
    Button increaseButton = window.Get<Button>("PART_IncreaseButton");
    Button decreaseButton = window.Get<Button>("PART_DecreaseButton");
    
    // define the value 
    theEdit.SetValue("600");
    
    // increase and decrease the value
    increaseButton.Click();
    increaseButton.Click();
    increaseButton.Click();
    decreaseButton.Click();
    

    这是基于 Xceed 自动化自定义控件的代码。

    // retrieve the custom control
    IUIItem theCustomControl = window.Get(SearchCriteria.ByAutomationId("008"));
    
    // get the childs items                
    if(theCustomControl is CustomUIItem)
    {
        // retrieve the custom control container
        IUIItemContainer foundCustomControl = (theCustomControl as CustomUIItem).AsContainer();
    
        // get the child components
        TextBox theEdit3 = foundCustomControl.Get<TextBox>("AutoSelectTextBox");
        Button increaseButton3 = foundCustomControl.Get<Button>("PART_IncreaseButton");
        Button decreaseButton3 = foundCustomControl.Get<Button>("PART_DecreaseButton");
    
        // perform actions...
        theEdit3.SetValue("800");
        increaseButton3.Click();
        increaseButton3.Click();
    }
    

    【讨论】:

    • 我收到IUIItemContainer foundCustomControl = (theCustomControl as CustomUIItem).AsContainer(); 行的语法错误,说AsContainer 不可访问。
    • 请务必使用最新版本的 TestStack.White,方法 AsContainer() 在旧版本中是内部的。
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2019-01-08
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2021-12-31
    相关资源
    最近更新 更多