如何自动化xceed updown controls

时间:2017-01-22 12:06:18

标签: ui-automation white

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

1 个答案:

答案 0 :(得分:2)

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

  • AutoSelectTextBox
  • Part_IncreaseButton
  • Part_DecreaseButton

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

这是一个示例应用程序,其中第一个Textbox为DoubleUpDown控件,第三个为自定义设计的自定义文件。

Test Application Screen

if (scheduleSettings == 0)
    nav_menu.findItem(R.id.scheduleSettingsSideBar).setVisible(true);

else if (randomDelay == 0)
    nav_menu.findItem(R.id.randomDelaySideBar).setVisible(false);

else if (temperatureSettings == 0)
    nav_menu.findItem(R.id.tempSettingsSideBar).setVisible(false);

else if (humiditySettings == 0)
    nav_menu.findItem(R.id.humiditySettingsSideBar).setVisible(false);

else if (manualControl == 0)
    nav_menu.findItem(R.id.manualSettingsSideBar).setVisible(false);

else if (logging == 0)
    nav_menu.findItem(R.id.loggingSideBar).setVisible(false);

else if (generalSettings == 0)
    nav_menu.findItem(R.id.generalSettingsSideBar).setVisible(false);

在UIA Verify中,正常的DoubleUpDown控件显示具有相同的AutomationIds。 自定义的自定义层次结构与真实层次结构一起出现,可以使用在XAML中设置的AutomationId(此处 008 )。

UIA Verifiy Screen

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

...
<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" />
...

以下是在窗口中自动执行唯一DoubleUpDown控件的默认方法。

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)
    {
    }
}

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

// 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();