Skip to content

Instantly share code, notes, and snippets.

@Roemer
Last active January 15, 2016 08:28
Show Gist options
  • Select an option

  • Save Roemer/fbb720c541d6989a3ce8 to your computer and use it in GitHub Desktop.

Select an option

Save Roemer/fbb720c541d6989a3ce8 to your computer and use it in GitHub Desktop.
Concept of UIAComWrapper control lookup
using System.Drawing;
using System.Windows;
using System.Windows.Automation;
using TestStack.White.UIItems.Finders;
namespace TestStack.White.UIItems.VNext
{
public static class UIItemFluentExtensions
{
public static T DrawHighlight<T>(this T uiItem) where T : IUIItem
{
uiItem.DrawHighlight();
return uiItem;
}
public static T DrawHighlight<T>(this T uiItem, Color color) where T : IUIItem
{
uiItem.DrawHighlight(color);
return uiItem;
}
}
/// <summary>
/// Interface for all UI items
/// </summary>
public interface IUIItem
{
bool Exists { get; }
AutomationElement AutomationElement { get; }
bool Find(AutomationElement root, SearchCriteria crit, TreeScope scope);
IUIItem DrawHighlight();
IUIItem DrawHighlight(Color color);
}
/// <summary>
/// Base class for all UI items
/// </summary>
public abstract class UIItemBase : IUIItem
{
public bool Exists { get; protected set; }
public virtual AutomationElement AutomationElement { get; protected set; }
protected virtual object Property(AutomationProperty automationProperty)
{
return AutomationElement.GetCurrentPropertyValue(automationProperty);
}
public abstract bool Find(AutomationElement root, SearchCriteria crit, TreeScope scope);
public virtual IUIItem DrawHighlight()
{
return DrawHighlight(Color.Red);
}
public virtual IUIItem DrawHighlight(Color color)
{
var rectangle = AutomationElement.Current.BoundingRectangle;
if (rectangle != Rect.Empty)
{
new Drawing.FrameRectangle(color, rectangle).Highlight();
}
return this;
}
}
/// <summary>
/// Generic class for all UI items with the generic fluent methods
/// </summary>
public abstract class UIItemBase<T> : UIItemBase where T : UIItemBase<T>
{
public new virtual T DrawHighlight()
{
return (T)base.DrawHighlight();
}
public new virtual T DrawHighlight(Color color)
{
return (T)base.DrawHighlight(color);
}
}
/// <summary>
/// Example implementation of a label
/// </summary>
public class Label : UIItemBase<Label>
{
public virtual string Text
{
get { return (string)Property(AutomationElement.NameProperty); }
}
public override bool Find(AutomationElement root, SearchCriteria crit, TreeScope scope)
{
crit.AndControlType(ControlType.Text);
AutomationElement = root.FindFirst(scope, crit.AutomationCondition);
Exists = AutomationElement != null;
return Exists;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment