how do I select ComboBox\'s SelectedIndex = -1?
I wrote a code to automate testing:
AutomationElement aeBuildMachine = null;
int count =
This is what worked for me.
///
/// Extension method to select item from comboxbox
///
/// Combobox Element
/// Item to select
///
public static bool SelectComboboxItem(this AutomationElement comboBox, string item)
{
if (comboBox == null) return false;
// Get the list box within the combobox
AutomationElement listBox = comboBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List));
if (listBox == null) return false;
// Search for item within the listbox
AutomationElement listItem = listBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, item));
if (listItem == null) return false;
// Check if listbox item has SelectionItemPattern
object objPattern;
if (true == listItem.TryGetCurrentPattern(SelectionItemPatternIdentifiers.Pattern, out objPattern))
{
SelectionItemPattern selectionItemPattern = objPattern as SelectionItemPattern;
selectionItemPattern.Select(); // Invoke Selection
return true;
}
return false;
}
Usage
AutomationElement paymentCombobox = element.FindFirst(
TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "cbPayment")
);
paymentCombobox.SelectComboboxItem("Cash");
resource https://msdn.microsoft.com/en-us/library/ms752305(v=vs.110).aspx