How to use AutomationProperties.Name?
Asked Answered
C

3

13

Question

Can anyone please explain (preferrably with a code example) how the AutomationProperties.Name property is used programmatically and declaratively with XAML?

Explanation

I understand that the Coded UI Builder in Visual Studio 2010, for instance, takes a Window's name as SearchProperty.

Since my Window's name changes, I would like to have a constant SearchProperty that my Coded UI Tests can rely on.

In the code example below, I don't want the window title to be hard-coded as "Properties of Pipe 1" since that changes.

Code example

[GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
public class UIListViewPropertiesTable1 : WpfTable
{
    
    public UIListViewPropertiesTable1(UITestControl searchLimitContainer) : 
            base(searchLimitContainer)
    {
        #region Search Criteria
        this.SearchProperties[WpfTable.PropertyNames.AutomationId] = "listViewProperties";
        this.WindowTitles.Add("Properties of Pipe 1");
        #endregion
    }
    
    #region Properties
    public WpfText NameOfComponent
    {
        get
        {
            if ((this.mNameOfComponent == null))
            {
                this.mNameOfComponent = new WpfText(this);
                #region Search Criteria
                this.mNameOfComponent.SearchProperties[WpfText.PropertyNames.Name] = "Pipe 1";
                this.mNameOfComponent.WindowTitles.Add("Properties of Pipe 1");
                #endregion
            }
            return this.mNameOfComponent;
        }
    }
    #endregion
    
    #region Fields
    private WpfText mNameOfComponent;
    #endregion
}

Links

Here is an example: How To: Get automation working properly on data bound WPF list or combo box. I wasn't able to adapt it for a Window.

Cissiee answered 27/5, 2010 at 9:55 Comment(1)
The deference between AutomationProperties.Name and x:Name is that AutomationProperties.Name can be databinded this is really good when you are using the WVVM pattern an you what to add automation properties to your VM and bind it in the View. Create you own UIMap not using the test builder. This will give you a more controlled map. I hope to create an small sample that demonstrates this and post it on my blog. I'll give a link to it when i'll be done.Tulle
V
13

You can change the attached property AutomationProperties.Name either in XAML using:

AutomationProperties.Name = "new name"

or in code using:

Button.SetValue(AutomationProperties.NameProperty, "new value");
or
AutomationProperties.SetName(Button, "new value");

Vella answered 5/11, 2012 at 12:16 Comment(0)
M
1

You can pass the Window Title as parameter to its parent and set this parameter while initializing.

I do this way and works fine.

Main answered 5/10, 2012 at 7:48 Comment(0)
F
0

There is a way to work around that but its a bit ugly.

We will be using the fact that the the proprty that contains the reference to the window is cached and not looked up every time.

uimap class is a partial class and you can have code in the uimap.cs file that stil counts as a part of the uimap class.

Add a method there that accepts as a parameter the window title, and performs the search, and that puts the found window into the UIListViewPropertiesTable1 property of the generated code.

Furtek answered 28/8, 2012 at 1:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.