NotepadAutomationDemo的代码V2

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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值