0

I'm attempting to select a drop down menu, so that I can select a Courier Service using Microsoft UI Automation.

Below is the code in which I'm using

public void SelectCourierService(string courierService)
{
        Console.WriteLine(@"SelectCourierService(" + courierService + @")");

        var expandCollapsePattern = (ExpandCollapsePattern)_courierServiceCombo.GetCurrentPattern(ExpandCollapsePatternIdentifiers.Pattern);
        expandCollapsePattern.Expand();
        expandCollapsePattern.Collapse();

        var listItem = _courierServiceCombo.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, courierService));
        listItem = AutomationElementHelper.GetSubtree(_courierServiceCombo, courierService);

        object selectionItemPattern;
        if (listItem.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectionItemPattern))
        {
            var selectPattern = (SelectionItemPattern)selectionItemPattern;
            selectPattern.Select();
        }

        Thread.Sleep(100);
 }

However when it gets to the following piece of code:-

expandCollapsePattern.Expand();

The UI drop down menu expands down but then collapses back up meaning that I cannot select the items in the drop down.

I was wondering if anyone has had the same problem and what they did to solve this.

Thanks

ChrisMcLellan
  • 613
  • 1
  • 5
  • 13
  • I may missunderstand you, but you got a expandCollapsePattern.Collapse(); after your expandCollapsePattern.Expand(); - is that on purpose?! (if not please remove it and retry :) ) do you mean it collapses before the 1000 ms in your Thread.sleep(); run out? – Haphil Aug 20 '15 at 06:39
  • @Hansa I have removed the expandCollapsePattern.Collapse(); this still does not move. I have created a screencase to show you what is happening:- http://screencast-o-matic.com/watch/cojrlCfBEF – ChrisMcLellan Aug 21 '15 at 12:43

1 Answers1

2

One of my work collegues found an answer to this solution and I have placed it below and this worked for me:-

public static void SelectDropdownItem(AutomationElement dropdownBox, string itemToSelect, bool navigateToParent = true)
{
        var expandCollapsePattern = (ExpandCollapsePattern)dropdownBox.GetCurrentPattern(ExpandCollapsePatternIdentifiers.Pattern);
        expandCollapsePattern.Expand();
        expandCollapsePattern.Collapse();

        var listItem = dropdownBox.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, itemToSelect));

        if (navigateToParent)
        {
            var controlViewWalker = TreeWalker.ControlViewWalker;
            listItem = controlViewWalker.GetParent(listItem);
        }

        object selectionItemPattern;
        if (listItem.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectionItemPattern))
        {
            var selectPattern = (SelectionItemPattern)selectionItemPattern;
            selectPattern.Select();
        }
}
ChrisMcLellan
  • 613
  • 1
  • 5
  • 13