using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Automation;
using System.Threading;
namespace UIANotepad
{
class NotepadAutomationDemo
{
static AutomationElement notepadWin = null;
static string editPanelID = "15";
static string menuBarID = "MenuBar";
static string fileMenuItemID = "Item 1";
static string fileMenuName = "File";
static string saveMenuItemID = "Item 3";
static string fileNameTextboxID = "1001";
static string saveBtnID = "1";
static string confirmYesbuttonID = "CommandButton_6";
static Dictionary<string, AutomationElement> controls = new Dictionary<string,AutomationElement>();
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
AutomationEventHandler AEHandler = new AutomationEventHandler(OnNotepadOpen);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, AEHandler);
System.Diagnostics.Process.Start("notepad.exe");
Console.ReadLine();
}
private static void OnNotepadOpen(object src, AutomationEventArgs args)
{
AutomationElement notepad;
try
{
notepad = src as AutomationElement;
if ("Untitled - Notepad" == notepad.Current.Name)
{
notepadWin = notepad;
controls.Add("Notepad", notepadWin);
DoSomething();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static AutomationElement FindElementByID(AutomationElement searchBase, TreeScope scope, string ctrlID, ControlType ctrlType)
{
AndCondition conditions = new AndCondition(
new PropertyCondition(AutomationElement.AutomationIdProperty, ctrlID),
new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));
AutomationElement ctrl = null;
int searchCount = 0;
do
{
ctrl = searchBase.FindFirst(scope, conditions);
if(searchCount < 5)
{
searchCount ++;
System.Threading.Thread.Sleep(200);
}
else
{
string exMsg = String.Format("CtrlID:{0}/tCtrlType:{1}", ctrlID, ctrlType.ProgrammaticName);
throw new TimeoutException(exMsg);
}
}while(null == ctrl);
return ctrl;
}
private static AutomationElement FindElementByName(AutomationElement searchBase, TreeScope scope, string ctrlName
, ControlType ctrlType)
{
AndCondition conditions = new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, ctrlName),
new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));
AutomationElement ctrl = null;
int searchCount = 0;
do
{
ctrl = searchBase.FindFirst(scope, conditions);
if (searchCount < 5)
{
searchCount++;
System.Threading.Thread.Sleep(200);
}
else
{
string exMsg = String.Format("CtrlName:{0}/tCtrlType:{1}", ctrlName, ctrlType.ProgrammaticName);
throw new TimeoutException(exMsg);
}
} while (null == ctrl);
return ctrl;
}
private static void DoSomething()
{
//AutomationElement editPanel = SayHello();
//Find edit panel on notepad
AutomationElement editPanel = FindElementByID(controls["Notepad"], TreeScope.Children, editPanelID, ControlType.Document);
if(null != editPanel)
{
controls.Add("Notepad.EditPanel", editPanel);
SendKeysToControl(editPanel, "Hello, world!");
}
SaveToFile("Hello");
}
private static void SaveToFile(string fileName)
{
//Find Menu bar
AutomationElement menuBar = FindElementByID(controls["Notepad"], TreeScope.Children, menuBarID, ControlType.MenuBar);
if (null != menuBar)
{
if (false == controls.ContainsKey("Notepad.MenuBar"))
{
controls.Add("Notepad.MenuBar", menuBar);
}
}
else
{
throw new Exception("Menu bar not found!");
}
//Find file menu & file menu item
AutomationElement fileMenuItem = FindElementByID(controls["Notepad.MenuBar"], TreeScope.Children, fileMenuItemID, ControlType.MenuItem);
if (null != fileMenuItem)
{
if (false == controls.ContainsKey("Notepad.MenuBar.FileMenuItem"))
{
controls.Add("Notepad.MenuBar.FileMenuItem", fileMenuItem);
}
ExpandECPattern(fileMenuItem);
}
else
{
throw new Exception("File menu item not found!");
}
AutomationElement fileMenu = FindElementByName(controls["Notepad.MenuBar.FileMenuItem"], TreeScope.Children, fileMenuName, ControlType.Menu);
if (null != fileMenu)
{
if (false == controls.ContainsKey("Notepad.MenuBar.FileMenuItem.FileMenu"))
{
controls.Add("Notepad.MenuBar.FileMenuItem.FileMenu", fileMenu);
}
}
else
{
throw new Exception("File menu not found!");
}
//Click File->Save menu item
AutomationElement saveMenuItem = FindElementByID(controls["Notepad.MenuBar.FileMenuItem.FileMenu"], TreeScope.Children, saveMenuItemID, ControlType.MenuItem);
if (null != saveMenuItem)
{
if (false == controls.ContainsKey("Notepad.MenuBar.FileMenuItem.FileMenu.SaveMenuItem"))
{
controls.Add("Notepad.FileMenuItem.FileMenu.SaveMenuItem", saveMenuItem);
}
ExecuteInvokePattern(saveMenuItem);
}
else
{
throw new Exception("File->Save menu item not found!");
}
//TypeFileName(fileName);
AutomationElement fileNameTextbox = FindElementByID(controls["Notepad"], TreeScope.Descendants, fileNameTextboxID, ControlType.Edit);
if (null != fileNameTextbox)
{
if (false == controls.ContainsKey("Notepad.*.FileNameTextbox"))
{
controls.Add("Notepad.*.FileNameTextbox", fileNameTextbox);
}
SetValuePattern(fileNameTextbox, fileName);
}
else
{
throw new Exception("FileName Textbox not found!");
}
//ClickSaveButton();
AutomationElement saveButton = FindElementByID(controls["Notepad"], TreeScope.Descendants, saveBtnID, ControlType.Button);
if (null != saveButton)
{
if (false == controls.ContainsKey("Notepad.*.SaveButton"))
{
controls.Add("Notepad.*.SaveButton", saveButton);
}
ExecuteInvokePattern(saveButton);
}
else
{
throw new Exception("Save Button not found!");
}
//ConfirmSave();
AutomationElement confirmYesButton = FindElementByID(controls["Notepad"], TreeScope.Descendants, confirmYesbuttonID, ControlType.Button);
if (null != confirmYesButton)
{
if (false == controls.ContainsKey("Notepad.*.ConfirmYesButton"))
{
controls.Add("Notepad.*.ConfirmYesButton", confirmYesButton);
}
ExecuteInvokePattern(confirmYesButton);
}
else
{
throw new Exception("Save Button not found!");
}
}
static void ExecuteInvokePattern(AutomationElement ctrl)
{
InvokePattern ip = (InvokePattern)ctrl.GetCurrentPattern(InvokePattern.Pattern);
ip.Invoke();
}
static void SetValuePattern(AutomationElement ctrl, string words)
{
ValuePattern vp = (ValuePattern)ctrl.GetCurrentPattern(ValuePattern.Pattern);
vp.SetValue(words);
}
static void ExpandECPattern(AutomationElement ctrl)
{
ExpandCollapsePattern ep = (ExpandCollapsePattern)ctrl.GetCurrentPattern(ExpandCollapsePattern.Pattern);
ep.Expand();
}
static AutomationElement SendKeysToControl(AutomationElement ctrl, string words)
{
ctrl.SetFocus();
System.Windows.Forms.SendKeys.SendWait(words);
return ctrl;
}
}
}