如何自动执行xceed上下控件 [英] how to automate xceed updown controls

查看:139
本文介绍了如何自动执行xceed上下控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For my WPF application I am working on a UI automated test using TestStack.White.
I am using the DoubleUpDown controls from the xceed wpf toolkit in my app.
How can I get access to the DoubleUpDown control in my UI automated tests?

推荐答案

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

By using the UIA Verify, you can see that the DoubleUpDown control is seen as three controls with no hierarchy information and the following AutomationIds:

  • AutoSelectTextBox
  • Part_IncreaseButton
  • Part_DecreaseButton
  • AutoSelectTextBox
  • Part_IncreaseButton
  • Part_DecreaseButton

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

So you can automate them as normal controls but if you have multiple DoubleUpDown controls in the same window, there is a problem because all the controls will have the same AutomationIds.

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

Here is a sample application with the two first Textbox as DoubleUpDown controls and the third one as a custom one designed for Automation.

...
<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 ).

In UIA Verify, the normal DoubleUpDown controls appears with the same AutomationIds. The custom one appears with the real hierarchy and the AutomationId which was set in the XAML can be used (here 008).

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

The custom control MyDoubleUpDown, simple subclass of the Xceed one but with an automation peer.

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控件的默认方法.

Here is the default way to automate an unique DoubleUpDown control in a window.

// 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一个自动执行自定义控件的代码.

And this is the code to automate the custom control based on the Xceed one.

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

这篇关于如何自动执行xceed上下控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆