Skip to content

Instantly share code, notes, and snippets.

@fiddyschmitt
Created September 30, 2020 05:42
Show Gist options
  • Save fiddyschmitt/47475aa71464a4cc8833dd6a244e4946 to your computer and use it in GitHub Desktop.
Save fiddyschmitt/47475aa71464a4cc8833dd6a244e4946 to your computer and use it in GitHub Desktop.
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
namespace libUIHelper
{
public static class UIHelper
{
public static AutomationElement? FindWindowByTitles(params string[] titles)
{
var allWindows = AutomationElement
.RootElement
.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window))
.OfType<AutomationElement>()
.ToList();
foreach (var title in titles)
{
var window = allWindows.FirstOrDefault(window => window.Current.Name.StartsWith(title));
if (window != null) return window;
}
return null;
}
public static AutomationElement GetElementByClassNN(AutomationElement parent, string classNN)
{
var result = parent
.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ComboBox))
.OfType<AutomationElement>()
.Where((element, index) =>
{
var thisClassNN = $"{element.Current.ClassName}{index + 1}";
var match = thisClassNN.Equals(classNN, StringComparison.CurrentCultureIgnoreCase);
return match;
})
.FirstOrDefault();
return result;
}
public static List<string> GetItemsInComboBox(AutomationElement parent, string classNN)
{
var control = GetElementByClassNN(parent, classNN);
var items = control
.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List))
.FindAll(TreeScope.Children, Condition.TrueCondition)
.OfType<AutomationElement>()
.Select(element => element.Current.Name)
.ToList();
return items;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment