티스토리 뷰

Code/C#

[C#] Windows UI Automation Sample

Hide Code 2010. 4. 13. 22:01
add UIAutomationClient.dll and UIAutomationTypes.dll libraries to project references

using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;

namespace AutoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isRun = false;
            Process[] myProcesses = Process.GetProcesses();
            foreach (Process item in myProcesses)
            {
                if (item.ProcessName == "calc")
                {
                    isRun = true;
                    break;
                }
            }
            if (!isRun) Process.Start("calc.exe");

            AutomationElement aeDesktop = AutomationElement.RootElement;
            AutomationElement aeForm = null;

            int cnt = 0;
            do
            {
                aeForm = aeDesktop.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.NameProperty, "Calculator"));
                Thread.Sleep(100);
                if (cnt++ > 50) return;
            } while (aeForm == null);
            if (aeForm == null) return;

            AutomationElement aeButton1 = aeForm.FindFirst(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.NameProperty, "1"));
            AutomationElement aeButton2 = aeForm.FindFirst(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.NameProperty, "2"));
            AutomationElement aeButtonP = aeForm.FindFirst(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.NameProperty, "+"));
            AutomationElement aeButtonE = aeForm.FindFirst(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.NameProperty, "="));

            InvokePattern ipClickButton1 = (InvokePattern)aeButton1.GetCurrentPattern(InvokePattern.Pattern);
            InvokePattern ipClickButton2 = (InvokePattern)aeButton2.GetCurrentPattern(InvokePattern.Pattern);
            InvokePattern ipClickButtonP = (InvokePattern)aeButtonP.GetCurrentPattern(InvokePattern.Pattern);
            InvokePattern ipClickButtonE = (InvokePattern)aeButtonE.GetCurrentPattern(InvokePattern.Pattern);

            ipClickButton1.Invoke();
            Thread.Sleep(1000);
            ipClickButtonP.Invoke();
            Thread.Sleep(1000);
            ipClickButton2.Invoke();
            Thread.Sleep(1000);
            ipClickButtonE.Invoke();
        }
    }
}


'Code > C#' 카테고리의 다른 글

[C#] Exception Tracing Sample Code  (0) 2010.04.16
[C#] indexer sample  (0) 2010.02.23
[C#] Shift Operator  (0) 2010.02.17
[C#] Register/Unregister ActiveX Control  (0) 2009.03.15
[C#] Detect UserControl's Closing  (0) 2009.01.07
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함