how to automate xceed updown controls

此生再无相见时 提交于 2019-12-10 22:53:54

问题


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?


回答1:


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

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.

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

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


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

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

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


来源:https://stackoverflow.com/questions/41790681/how-to-automate-xceed-updown-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!