基于ui automation的计算器测试例子

来源:互联网 发布:vb文本框内容不可编辑 编辑:程序博客网 时间:2024/06/05 16:00

基于ui automation的计算器测试程序

using System;

using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows;
using System.Windows.Automation;
using System.Diagnostics;

namespace TestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            //Process p = Process.Start(@"E:\Project\WinFormTest\WinFormTest\bin\Debug\WinFormTest.exe");
            
            var desktop = AutomationElement.RootElement; // 先找到根元素,可以认为是桌面
            var condition = new PropertyCondition(AutomationElement.NameProperty, "计算器"); // 定义我们的查找条件,名字是test
            var window = desktop.FindFirst(TreeScope.Children, condition); // 在桌面的子控件中查找第一个符合条件的窗体。

          //  AutomationElement btnpane = window.FindFirst(TreeScope.Children,
           //         new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame"));

            AutomationElement btn9 = window.FindFirst(TreeScope.Children,
                   // new PropertyCondition(AutomationElement.NameProperty, "9"));
                    new PropertyCondition(AutomationElement.AutomationIdProperty, "133"));
            AutomationElement btnPlus = window.FindFirst(TreeScope.Children,
                   new PropertyCondition(AutomationElement.NameProperty, "+"));
            AutomationElement btn3 = window.FindFirst(TreeScope.Children,
                   new PropertyCondition(AutomationElement.NameProperty, "3"));
            AutomationElement btnEq = window.FindFirst(TreeScope.Children,
                   new PropertyCondition(AutomationElement.NameProperty, "="));
            

            List<AutomationElement> ele_list = new List<AutomationElement>();
            ele_list.Add(btn9);
            ele_list.Add(btnPlus);
            ele_list.Add(btn3);
            ele_list.Add(btnEq);
            InvokePattern invokeptn = null;
            foreach (AutomationElement elem in ele_list)
            {
                invokeptn = (InvokePattern)elem.GetCurrentPattern(InvokePattern.Pattern);
                invokeptn.Invoke();
            }

            AutomationElement result = window.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.AutomationIdProperty, "403"));
            if (result.Equals(12))
            {
                int t1 = 9;

            }
            else {

                int t2 = 7;
            }



            //通过ValuePattern设置或者获得TextBox1的值
            //ValuePattern vpTextBox1 = (ValuePattern)result.GetCurrentPattern(ValuePattern.Pattern);
            //vpTextBox1.SetValue("50");


            TextPattern tpTextBox3 = (TextPattern)result.GetCurrentPattern(TextPattern.Pattern);
            string results = tpTextBox3.DocumentRange.GetText(-1);  //获取textbox3中的值


           //AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.onWindowOpenOrClose);
           //Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);
           
            /*
            Condition conditions = new AndCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, resultTextAutoId),
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
            AutomationElement btn = window.FindAll(TreeScope.Descendants, conditions)[0];
            InvokePattern invokeptn = (InvokePattern)btn.GetCurrentPattern(InvokePattern.Pattern);
            invokeptn.Invoke();


            
            var btnCondition = new AndCondition(
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
                new PropertyCondition(AutomationElement.NameProperty, "ok"));
            var button = window.FindFirst(TreeScope.Children, btnCondition);
            var clickPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
            clickPattern.Invoke();
            */


        }
    }

}




最后添加引用UIAutomationClientsideProviders.dll,UIAutomationTypes.dll,UIAutomationClient.dll三个dll的引用即可


0 0
原创粉丝点击