1using FlaUI.Core.AutomationElements;
2using FlaUI.Core.Definitions;
3using FlaUI.Core.Identifiers;
5using Gma.System.MouseKeyHook;
6using System.Diagnostics;
13 public static InputSimulator
input;
14 public static Dictionary<string, Recording>
Recordings =
new Dictionary<string, Recording>();
15 public static Dictionary<string, Recording> Properties =
new Dictionary<string, Recording>();
16 public static UIA3Automation
automation =
new UIA3Automation();
20 private KeyEventArgs key;
21 private KeyPressEventArgs keyPress;
22 private string automationID;
24 private string className;
25 private string process;
28 private System.Drawing.Point point;
30 private MouseEventArgs mouse;
31 private MouseButtons mouseButton;
68 return (
Keys)keyPress.KeyChar;
75 get {
return mouseButton; }
76 set { mouseButton = value; }
78 public System.Drawing.Point
Point
81 set { point = value; }
85 get {
return automationID; }
86 set { automationID = value; }
95 get {
return className; }
96 set { className = value; }
100 get {
return process; }
101 set { process = value; }
105 get {
return title; }
106 set { title = value; }
110 get {
return index; }
111 set { index = value; }
115 get {
return autoValue; }
116 set { autoValue = value; }
122 AutomationElement el =
Element;
124 automationID = el.AutomationId;
125 className = el.ClassName;
126 Process p = Process.GetProcessById(proc);
134 AutomationElement el =
Element;
136 automationID = el.AutomationId;
137 className = el.ClassName;
138 Process p = Process.GetProcessById(proc);
146 AutomationElement el =
Element;
150 automationID = el.AutomationId;
156 className = el.Properties.LocalizedControlType.Value.ToString();
157 mouseButton = e.Button;
158 point =
new Point((
int)(e.Location.X - el.BoundingRectangle.X), (
int)(e.Location.Y - el.BoundingRectangle.Y));
159 Process p = Process.GetProcessById(proc);
160 title = p.MainWindowTitle;
164 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, MouseEventArgs mo)
167 this.automationID = id;
168 this.process = process;
170 this.className = classname;
171 this.mouseButton = mo.Button;
178 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyEventArgs ke)
182 this.automationID = id;
184 this.process = process;
185 this.className = classname;
190 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyPressEventArgs ke)
194 this.automationID = id;
195 this.process = process;
197 this.className = classname;
202 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname)
204 this.automationID = id;
206 this.className = classname;
207 this.automationID = id;
208 this.process = process;
210 this.className = classname;
217 Stopwatch stopwatch =
new Stopwatch();
220 if (type ==
Type.mousedown)
222 Cursor.Position =
new Point((
int)el.BoundingRectangle.X +
Point.X, (
int)el.BoundingRectangle.Y +
Point.Y);
223 if (mouse.Button == MouseButtons.Left)
226 if (mouse.Button == MouseButtons.Right)
229 else if (type ==
Type.mouseup)
231 Cursor.Position =
new Point((
int)el.BoundingRectangle.X +
Point.X, (
int)el.BoundingRectangle.Y +
Point.Y);
232 if (mouse.Button == MouseButtons.Left)
234 if (mouse.Button == MouseButtons.Right)
237 else if (type ==
Type.keydown)
240 input.Keyboard.KeyDown((VirtualKeyCode)key.KeyValue);
242 else if (type ==
Type.keyup)
245 input.Keyboard.KeyUp((VirtualKeyCode)key.KeyValue);
247 else if (type ==
Type.keypress)
250 SendKeys.Send(keyPress.KeyChar.ToString());
252 else if (type ==
Type.wheelup)
258 else if (type ==
Type.wheeldown)
265 ticks = stopwatch.ElapsedMilliseconds;
270 if(type ==
Type.keyup || type ==
Type.keydown)
273 if (type ==
Type.keypress)
276 if (type ==
Type.mouseup || type ==
Type.mousedown)
290 set { name = value; }
296 set { file = value; }
302 set { list = value; }
311 Name = System.IO.Path.GetFileNameWithoutExtension(fil);
315 foreach (
Action a
in list)
323 for (
int i = 0; i < list.Count; i++)
326 if (i == list.Count - 1)
355 return ae.BoundingRectangle;
357 public void Set(
string val)
359 for (
int i = 0; i < list.Count; i++)
362 if (i == list.Count - 1)
379 get {
return recording; }
382 private IKeyboardMouseEvents m_GlobalHook;
383 private static bool recording =
false;
384 private static bool move =
false;
389 m_GlobalHook = Hook.GlobalEvents();
390 m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
391 m_GlobalHook.MouseUpExt += GlobalHookMouseUpExt;
392 m_GlobalHook.MouseMoveExt += GlobalHookMouseMoveExt;
393 m_GlobalHook.KeyPress += GlobalHookKeyPress;
394 m_GlobalHook.KeyUp += GlobalHookKeyUp;
395 m_GlobalHook.KeyDown += GlobalHookKeyDown;
396 m_GlobalHook.MouseWheel += GlobalHookMouseWheel;
397 input =
new InputSimulator();
408 return automation.FromPoint(
new Point(Cursor.Position.X, Cursor.Position.Y));
453 return Properties[prop].Get();
463 Recorder.
AddLine(
"Automation.SetProperty(" +
'"' + prop +
'"' +
"," +
'"' + val +
'"' +
");");
471 if (Properties.ContainsKey(
rec.
Name))
483 Recorder.
AddLine(
"Automation.RunRecording(" +
'"' + prop +
'"' +
");");
493 private void GlobalHookMouseDownExt(
object sender, MouseEventExtArgs e)
497 AutomationElement el =
Element;
510 private void GlobalHookMouseUpExt(
object sender, MouseEventExtArgs e)
514 AutomationElement el =
Element;
517 rec.
List.Add(
new Action(Action.Type.mouseup, el.Properties.ProcessId, e));
527 private void GlobalHookMouseWheel(
object sender, MouseEventArgs e)
531 AutomationElement el =
Element;
535 rec.
List.Add(
new Action(Action.Type.wheelup, el.Properties.ProcessId, e));
537 rec.
List.Add(
new Action(Action.Type.wheeldown, el.Properties.ProcessId, e));
547 private void GlobalHookMouseMoveExt(
object sender, MouseEventExtArgs e)
553 AutomationElement el =
Element;
556 rec.
List.Add(
new Action(Action.Type.mousemove, el.Properties.ProcessId, e));
567 private void GlobalHookKeyPress(
object sender, KeyPressEventArgs e)
571 AutomationElement el =
Element;
574 rec.
List.Add(
new Action(Action.Type.keypress, el.Properties.ProcessId, e));
584 private void GlobalHookKeyUp(
object sender, KeyEventArgs e)
588 AutomationElement el =
Element;
591 rec.
List.Add(
new Action(Action.Type.keyup, el.Properties.ProcessId, e));
601 private void GlobalHookKeyDown(
object sender, KeyEventArgs e)
605 AutomationElement el =
Element;
608 rec.
List.Add(
new Action(Action.Type.keydown, el.Properties.ProcessId, e));
614 public static List<AutomationElement>
GetChildren(AutomationElement parent)
619 throw new ArgumentException();
621 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
622 if (collection !=
null)
624 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
638 throw new ArgumentException();
642 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
643 if (collection !=
null)
645 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
659 public static List<AutomationElement>
GetName(AutomationElement parent,
string name)
664 throw new ArgumentException();
666 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
667 if (collection !=
null)
669 List<AutomationElement> result =
new List<AutomationElement>();
670 foreach (AutomationElement item
in collection)
672 if (item.Name == name)
685 public static List<AutomationElement>
GetAutomationID(AutomationElement parent,
string id)
690 throw new ArgumentException();
692 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
693 if (collection !=
null)
695 List<AutomationElement> result =
new List<AutomationElement>();
696 foreach (AutomationElement item
in collection)
698 if (item.AutomationId ==
id)
711 public static List<AutomationElement>
GetClassName(AutomationElement parent,
string name)
716 throw new ArgumentException();
718 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
719 if (collection !=
null)
721 List<AutomationElement> result =
new List<AutomationElement>();
722 foreach (AutomationElement item
in collection)
724 if (item.ClassName == name)
737 public static AutomationElement
GetElementByProcess(
string process,
string title,
string id,
string name,
string classname,
int index)
739 Process[] procs = Process.GetProcessesByName(process);
742 ti = procs[0].MainWindowTitle;
743 AutomationElement root =
automation.GetDesktop();
744 AutomationElement[] prs =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
745 AutomationElement window =
null;
746 List<AutomationElement> ats =
new List<AutomationElement>();
747 foreach (AutomationElement item
in prs)
751 if (item.Name.Contains(ti))
763 throw new ArgumentException(
"Window " + title +
" not found.");
764 AutomationElement[] items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
765 List<AutomationElement> result =
new List<AutomationElement>();
766 foreach (AutomationElement item
in items)
770 if (item.Properties.LocalizedControlType == classname && item.Name == name)
783 return result[index];
785 public static int GetIndex(
string process,
string title,
string id,
string name,
string classname,
Point p)
788 Process[] procs = Process.GetProcessesByName(process);
791 ti = procs[0].MainWindowTitle;
792 AutomationElement[] prs =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
793 AutomationElement window =
null;
794 List<AutomationElement> ats =
new List<AutomationElement>();
795 foreach (AutomationElement item
in prs)
799 if (item.Name.Contains(title))
812 AutomationElement[] items =
null;
815 items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
821 List<AutomationElement> result =
new List<AutomationElement>();
850 int[] r =
new int[3];
851 string[] sts = s.Split(
',');
852 for (
int i = 0; i < 3; i++)
854 r[i] =
int.Parse(sts[i]);
858 public static List<AutomationElement>
GetElement(
string id,
string name)
860 AutomationElement parent =
automation.GetDesktop();
861 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
862 if (collection !=
null)
864 List<AutomationElement> result =
new List<AutomationElement>();
865 foreach (AutomationElement item
in collection)
867 if (item.AutomationId ==
id && item.Name == name)
885 throw new ArgumentException();
887 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
888 if (collection !=
null)
890 List<AutomationElement> result =
new List<AutomationElement>();
891 foreach (AutomationElement item
in collection)
893 if (item.Properties.LocalizedControlType == type)
911 throw new ArgumentException();
913 return parent.FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
920 throw new ArgumentException();
922 List<AutomationElement> ress =
new List<AutomationElement>();
923 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
924 foreach (AutomationElement item
in all)
926 if (item.AutomationId ==
auto)
938 throw new ArgumentException();
940 List<AutomationElement> ress =
new List<AutomationElement>();
941 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
942 foreach (AutomationElement item
in all)
944 if (item.Properties.LocalizedControlType ==
auto)
951 public static AutomationElement[]
GetRaw(AutomationElement parent)
956 throw new ArgumentException();
958 List<AutomationElement> ress =
new List<AutomationElement>();
959 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
961 public static string GetValue(AutomationElement control)
963 if(control.ControlType == ControlType.Edit)
965 FlaUI.Core.AutomationElements.TextBox tb = control.AsTextBox();
968 else if(control.ControlType == ControlType.Text)
970 return control.AsLabel().Text;
976 object patternProvider;
977 FlaUI.Core.AutomationElements.ListBox lb = control.AsListBox();
978 return lb.SelectedItem.Name;
980 public static string GetText(AutomationElement control)
982 return control.AsLabel().Text;
984 public static void SetValue(AutomationElement targetControl,
string value)
988 throw new ArgumentNullException(
989 "String parameter must not be null.");
990 if (targetControl ==
null)
991 throw new ArgumentNullException(
992 "AutomationElement parameter must not be null");
1000 if (!targetControl.IsEnabled)
1002 throw new InvalidOperationException(
1003 "The control is not enabled.\n\n");
1005 targetControl.AsTextBox().Text = value;
1012 throw new ArgumentException();
1015 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default).Length;
1019 if (element ==
null)
1023 ToggleButton tb = element.AsToggleButton();
1024 if (tb.TogglePattern.ToggleState.ValueOrDefault.HasFlag(ToggleState.On))
1031 System.Drawing.Graphics g;
1034 g = System.Drawing.Graphics.FromImage(b);
1035 g.CopyFromScreen(
new Point((
int)r.X,(
int)r.Y),
new Point(0, 0),
new Size(r.Width,r.Height));
AForge.Rectangle Rectangle
System.Windows.Forms.Keys Keys
Action(Type t, string process, string title, int index, string id, string name, string classname, MouseEventArgs mo)
Action(Type t, string process, string title, int index, string id, string name, string classname)
System.Drawing.Point Point
override string ToString()
Action(Type t, int proc, KeyPressEventArgs e)
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyPressEventArgs ke)
Action(Type t, int proc, MouseEventArgs e)
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyEventArgs ke)
Action(Type t, int proc, KeyEventArgs e)
static bool GetToggle(AutomationElement element)
static void SetValue(AutomationElement targetControl, string value)
static List< AutomationElement > GetRawAutomationID(AutomationElement parent, string auto)
static List< AutomationElement > GetAllChildren(AutomationElement parent)
static int GetIndex(string process, string title, string id, string name, string classname, Point p)
static AutomationElement GetElementByProcess(string process, string title, string id, string name, string classname, int index)
static string GetText(AutomationElement control)
static List< AutomationElement > GetName(AutomationElement parent, string name)
static int GetCount(AutomationElement parent)
static List< AutomationElement > GetChildren(AutomationElement parent)
static List< AutomationElement > GetClassName(AutomationElement parent, string name)
static AutomationElement[] GetRaw(AutomationElement parent)
static string GetSelection(AutomationElement control)
static Bitmap GetImage(AutomationElement el)
static List< AutomationElement > GetElement(string id, string name)
static List< AutomationElement > GetAutomationID(AutomationElement parent, string id)
static string GetValue(AutomationElement control)
static List< AutomationElement > GetLocalizedControlType(AutomationElement parent, TreeScope scope, string type)
static AutomationElement[] GetRawChildren(AutomationElement parent)
static int[] StringToRuntimeID(string s)
static List< AutomationElement > GetRawLocalizedControlType(AutomationElement parent, string auto)
override string ToString()
Recording(string fil, List< Action > l)
static UIA3Automation automation
static void SetProperty(string prop, string val)
static void StartPropertyRecording(string name)
static void AddProperty(Recording rec)
static void StartRecording()
It starts recording.
static void RunRecording(string prop)
static void StopRecording()
It stops the recording and adds it to the list of recordings.
static AutomationElement Element
static object GetProperty(string prop)
static InputSimulator input
static void StopPropertyRecording()
Stop recording the property and add it to the list of properties.
static void AddLine(string s)
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.
static string GetActiveWindowTitle()
static void SetFocus(string process)