ui-automationwhite-framework

how to automate xceed updown controls


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?


Solution

  • 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:

    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.

    Test Application Screen

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

    UIA Verifiy Screen


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