views:

168

answers:

2

Hi,

I am trying to test a WPF application using the UI-Automation framework that MSFT provides. There were a few powershell scripts written that invoked the cmdlets created to manipulate the visual controls of the application.

There is a DropDown within my application that has an entry 'DropDownEntry'. In my cmdlet, I am trying to do something as follows:

 AutomationElement getItem = DropDown.FindFirst(TreeScope.Descendants,
 new AndCondition(
 new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ListItem),
 new PropertyCondition(AutomationElement.NameProperty, "DropDownEntry",PropertyConditionFlags.IgnoreCase)));

The above given snippet returns 'null' upon execution which essentially means that the above given logic was unable to find my dropdown entry.

Can somebody tell me why this might be happening? I checked the name of my control and the values. Everything seems to be in order. I am not sure why this would be happening. Any help would be much appreciated.

Thanks

A: 

If you're not already, you may want to inspect your application with UISpy to verify the properties.

jaws
Thanks jws. I was wondering what sort of properties should I be on the lookout for? Should I be raising flags if the control I am trying to access using UI-Automation does not show up within the Visual tree? Isn't anything that's visible within the application accessible by UI-Automation in theory?
sc_ray
It would depend on the application, but if its built with standard windows controls, then everything should be accessible with UIAutomation. Firefox, for example uses XUL and Gecko, and isn't UIAutomation accessible. If you're not seeing the drop-down control in the visual tree, then its not a UIAutomationElement. If you're not seeing the dropdownentry's, then see @Samuel Jack's answer.
jaws
+1  A: 

Since it is a DropDown control you are automating, it may be that the child items are not available through UIAutomation until the DropDown is dropped down.

You need to get hold of the ExpandCollapse pattern from the DropDown element, then call its Expand method.

I created some extension methods to help with getting hold of patterns. Here's one example

public static class PatternExtensions
{
    public static ExpandCollapsePattern GetExpandCollapsePattern(this AutomationElement element)
    {
        return element.GetPattern<ExpandCollapsePattern>(ExpandCollapsePattern.Pattern);    
    }

    public static T GetPattern<T>(this AutomationElement element, AutomationPattern pattern) where T : class
    {
        object patternObject = null;
        element.TryGetCurrentPattern(pattern, out patternObject);

        return patternObject as T;
    }
}

Use it like this:

DropDown.GetExpandCollapsePattern().Expand()

Then you can execute your original code to find the child element.

Samuel Jack
@Samuel Jack - Thanks. This helps
sc_ray