Nick
Nick

Reputation: 4766

How do you determine (WinForms) control type in UITestControl?

I'm trying to make custom properties for controls available in Coded UI Tests and every example I've found is totally useless.

For instance: http://msdn.microsoft.com/en-us/library/hh552522.aspx

public override int GetControlSupportLevel(UITestControl uiTestControl)
{
    // For MSAA, check the control type
    if (string.Equals(uiTestControl.TechnologyName, "MSAA",
    StringComparison.OrdinalIgnoreCase) &&
    (uiTestControl.ControlType == "Chart"||uiTestControl.ControlType == "Text"))
    {
        return (int)ControlSupport.ControlSpecificSupport;
    }

    // This is not my control, so return NoSupport
    return (int)ControlSupport.NoSupport;
}

// Get the property value by parsing the accessible description
public override object GetPropertyValue(UITestControl uiTestControl, string propertyName)
{
    if (String.Equals(propertyName, "State", StringComparison.OrdinalIgnoreCase))
    {
        object[] native = uiTestControl.NativeElement as object[];
        IAccessible acc = native[0] as IAccessible;

        string[] descriptionTokens = acc.accDescription.Split(new char[] { ';' });
        return descriptionTokens[1];
    }

    // this is not my control
    throw new NotSupportedException();
}

This code is entirely worthless if you have 2 different controls that are "Text" controls - there is no way to determine which type of text control it is. The "ControlType" property is very misleading because it does not return the Type of the control as its name suggests. It's more like a control category. How can you determine what the control actually is?

Upvotes: 4

Views: 1401

Answers (1)

Jai
Jai

Reputation: 63

You can use something like this. Hope this helps.

string controlType = control.GetProperty(XamlControl.PropertyNames.ControlType).ToString();

Upvotes: 1

Related Questions