How to use AutomationProperties.Name?

Lernkurve picture Lernkurve · May 27, 2010 · Viewed 9.4k times · Source

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.

Answer

Martin Suchan picture Martin Suchan · Nov 5, 2012

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