References:
System;
System.Data;
System.Data.DataSetExtensions;
System.Windows.Forms;
System.Xml;
System.Xml.Linq;
UIAutomationClient;
UIAutomationClientsideProviders;
UIAutomationProvider;
UIAutomationProvider;
UIAutomationTypes;
WindowsBase;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace ConsoleApplication2
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust" )]
[ComVisibleAttribute(true )]
class Program
{
#region Variations
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
#endregion
#region SetCursoPos
[DllImport("user32.dll" )]
extern static int SetCursorPos(int incrementX, int incrementY);
#endregion
#region Mouse click event
/// <summary>
/// Mouse click event
/// </summary>
/// <param name="mouseEventFlag">MouseEventFlag </param>
/// <param name="incrementX">X coordinate </param>
/// <param name="incrementY">Y coordinate </param>
/// <param name="data"></param>
/// <param name="extraInfo"></param>
[DllImport("user32.dll" )]
extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo);
#endregion
#region Click AutomationElement
/// <summary>
///
/// </summary>
/// <param name="ae"></param>
public static void LeftClick(AutomationElement ae)
{
System.Windows.Rect rect = ae.Current.BoundingRectangle;
int IncrementX = (int )(rect.Left + rect.Width / 2);
int IncrementY = (int )(rect.Top + rect.Height / 2);
//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);
//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr .Zero);
mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr .Zero);
}
public static void RightClick(AutomationElement Element)
{
System.Windows.Rect rect = Element.Current.BoundingRectangle;
int IncrementX = (int )(rect.Left + rect.Width / 2);
int IncrementY = (int )(rect.Top + rect.Height / 2);
//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);
//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_RIGHTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
mouse_event(MOUSEEVENTF_RIGHTUP, IncrementX, IncrementY, 0, UIntPtr .Zero);
}
#endregion
#region DelaySeconds
/// <summary>
/// Delay Seconds
/// </summary>
/// <param name="num"></param>
private static void DelaySeconds(int num)
{
System.Threading.Thread .Sleep(1000 * num);
}
#endregion
static void Main(string [] args)
{
string computerName = System.Configuration.ConfigurationSettings.AppSettings["ComputerName" ].ToUpper();
computerName = computerName == "" ? System.Net.Dns.GetHostEntry("localhost").HostName.Remove(System.Net.Dns.GetHostEntry("localhost").HostName.IndexOf('.' )).ToUpper() : computerName;
string process1 = System.Configuration.ConfigurationSettings.AppSettings["Process1" ];
for (int i = 1; i < 100; i++)
{
string processi = System.Configuration.ConfigurationSettings.AppSettings["Process" + i.ToString()];
if (processi == null )
{
Console .WriteLine(i);
break ;
}
string[] processiArr = processi.Split('$' );
System.Diagnostics.Process process1 = System.Diagnostics.Process.Start(@"" + processiArr[0]);
process1.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle .Normal;
process1.WaitForInputIdle(1000);
Console.WriteLine(processi);
}
string step1 = System.Configuration.ConfigurationSettings.AppSettings["Step1"];
AutomationElement[] UIElement = new AutomationElement [100];
for (int i = 1; i < 100; i++)
{
string stepi = System.Configuration.ConfigurationSettings.AppSettings["Step" + i.ToString()];
if (stepi == null )
{
Console.WriteLine(i);
break;
}
else
{
stepi = stepi.Replace("%ComputerName%" , computerName);
string[] stepiArr = stepi.Split('$');
if (stepiArr[0].ToUpper() == "Edit".ToUpper())
{
System.Windows.Forms.SendKeys.SendWait(stepiArr[2]);
Console.WriteLine("Edit: " + stepiArr[2]);
}
else if (stepiArr[0].ToUpper() == "Delay".ToUpper())
{
DelaySeconds(Convert .ToInt32(stepiArr[2]));
Console.WriteLine("DelaySeconds: " +stepiArr[2]);
}
else
{
if (stepiArr.Length > 5)
{
UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope.Descendants,
AutomationElement.NameProperty, stepiArr[2],
AutomationElement.AutomationIdProperty, stepiArr[3],
AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4]),
AutomationElement.ClassNameProperty, stepiArr[5]);
}
else
{
UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope .Descendants,
AutomationElement.NameProperty, stepiArr[2],
AutomationElement.AutomationIdProperty, stepiArr[3],
AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4]));
}
if (UIElement == null )
{
Console.WriteLine("UIElement is null" );
}
else
{
Console.WriteLine("UIElement " + stepiArr[2] + " found" );
if (stepiArr[0].ToUpper() == "LeftClick".ToUpper())
{
LeftClick(UIElement[i]);
Console.WriteLine("LeftClick " + stepiArr[2]);
}
else if (stepiArr[0].ToUpper() == "RightClick".ToUpper())
{
RightClick(UIElement[i]);
Console.WriteLine("RightClick " + stepiArr[2]);
}
}
}
}
Console.WriteLine(stepi);
}
Console.Read();
}
public static object StringToControlType(string str)
{
object controlType = null ;
if (str == "Window") controlType = ControlType.Window;
else if (str == "MenuItem") controlType = ControlType.MenuItem;
else if (str == "Document") controlType = ControlType.Document;
else if (str == "TreeItem") controlType = ControlType.TreeItem;
else if (str == "Menu" ) controlType = ControlType.Menu;
else if (str == "MenuItem") controlType = ControlType.MenuItem;
else if (str == "Document") controlType = ControlType.Document;
else if (str == "Document") controlType = ControlType.Document;
else if (str == "Document") controlType = ControlType.Document;
return controlType;
}
public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope treeScope,
AutomationProperty automationProperty1, object value1,
AutomationProperty automationProperty2, object value2,
AutomationProperty automationProperty3, object value3)
{
return automationElement.FindFirst
(
treeScope,
new AndCondition
(
new PropertyCondition(automationProperty1, value1),
new PropertyCondition(automationProperty2, value2),
new PropertyCondition(automationProperty3, value3)
)
);
}
public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope
treeScope,
AutomationProperty automationProperty1, object value1,
AutomationProperty automationProperty2, object value2,
AutomationProperty automationProperty3, object value3,
AutomationProperty automationProperty4, object value4)
{
return automationElement.FindFirst
(
treeScope,
new AndCondition
(
new PropertyCondition (automationProperty1, value1),
new PropertyCondition (automationProperty2, value2),
new PropertyCondition (automationProperty3, value3),
new PropertyCondition (automationProperty4, value4)
)
);
}
}
}
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ComputerName" value=""/>
<add key="Process1" value="C:/aaa.exe"/>
<!--
value=action$StepN$NameProperty$AutomationIdProperty$ControlTypeProperty
value=action$StepN$NameProperty$AutomationIdProperty$ControlTypeProperty$ClassNameProperty
value=Edit$StepN$EditValue
value=Delay$StepN$seconds
1.action:LeftClick;RightClick;Edit;Delay;
StepN:
NameProperty:
AutomationIdProperty:
ControlTypeProperty:
2.EditValue:/when action is Edit;
"%{N}" is ALT+N;
"^s" is Ctrl+S;
"{Enter}" is press Enter key;
"%{F4}" is Alt+F4;
-->
<add key="Step1" value="LeftClick$Step0$aaa Server Manager$$Window"/>
<add key="Step2" value="LeftClick$Step1$Management Server$$TreeItem"/>
<add key="Step3" value="Edit$Step1${RIGHT}" />
<add key="Step4" value="LeftClick$Step1$Action Servers$$TreeItem"/>
<add key="Step5" value="Edit$Step1${RIGHT}"/>
<add key="Step6" value="Delay$Step0$2" />
<add key="Step7" value="RightClick$Step1$%ComputerName%$$TreeItem"/>
<add key="Step8" value="Delay$Step0$2" />
<add key="Step9" value="RightClick$Step0$Menu$$Menu$#32768"/>
<add key="Step10" value="LeftClick$Step9$Uninstall Action Server$Item 44101$MenuItem"/>
</appSettings>
</configuration>