• UIAutomation 测试winForm


     static void Main(string[] args)
            {
                Console.WriteLine("
    开始窗口程序自动化测试
    ");
                //启动被测试程序
                string path = @"程序路径";
                Process p = Process.Start(path);
    
                //自动化跟元素
                AutomationElement desktop = AutomationElement.RootElement;
                //查找主窗体方法1
                //Thread.Sleep(2000);
                //AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
                AutomationElement aeForm;
                //方法2
                int numWaits = 0;
                do
                {
                    Console.WriteLine("等待测试窗口……");
    
                    //查找第一个自动化元素
                    aeForm = desktop.FindFirst(TreeScope.Children, new PropertyCondition(
    
                    AutomationElement.NameProperty, "Form1"));
    
                    ++numWaits;
    
                    Thread.Sleep(100);
                } while (null == aeForm && numWaits < 50);
    
                if (aeForm == null)
                    throw new NullReferenceException("找不到测试窗口!");
                else
                    Console.WriteLine("找到测试窗口~");
                //找到第一个Button
                AutomationElement aeButton = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "btnCalc"));
    
    
                
                //找到所有textbox控件
                AutomationElementCollection aeAllTextBoxs=aeForm.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Edit));
    
                AutomationElement aeText3 = aeAllTextBoxs[0];//集合的索引和控件位置相反
                AutomationElement aeText2 = aeAllTextBoxs[1];
                AutomationElement aeText1 = aeAllTextBoxs[2];
    
                ValuePattern vpText1 = (ValuePattern)aeText1.GetCurrentPattern(ValuePattern.Pattern);
                vpText1.SetValue("300");
                ValuePattern vpText2 = (ValuePattern)aeText2.GetCurrentPattern(ValuePattern.Pattern);//把控件转换成Value模式
                vpText2.SetValue("500");
                Thread.Sleep(500);
    
                //通过InvokePattern模拟点击按钮
                InvokePattern ipbtnCalc = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern);//把控件转换成Invoke模式
                ipbtnCalc.Invoke();
                Thread.Sleep(500);
              //  Console.ReadKey();
                
                //验证实际结果和预期结果是否相符
                TextPattern tpText3 = (TextPattern)aeText3.GetCurrentPattern(TextPattern.Pattern);//把控件转换成Text模式
                string actual= tpText3.DocumentRange.GetText(-1);
                // string actual = tpText3.DocumentRange.GetText(2);//getext中的值要>=返回结果的长度,才可以取全值.方便起见-1也可以取全值
                string expect = "800";
                if (actual == expect)
                {
                    Console.WriteLine("【实际值】=" + actual);
                }
                else {
                    Console.WriteLine("【实际值】="+actual+";【期望值】"+expect);
                }
                Thread.Sleep(5000);
                WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern);
                wpCloseForm.Close();
                Console.WriteLine("
    测试结束
    ");
                Console.ReadKey();
            }
    

     MS提供的控件Pattern
     
    DockPattern                              
    ExpandCollapsePattern

    GridPattern                                 
    GridItemPattern

    InvokePattern                             
    MultipleViewPattern

    RangeValuePattern 

    ScrollPattern
    ScrollItemPattern

    SelectionPattern

    SelectionItemPattern                 

    TablePattern
    TableItemPattern                       

    TextPattern
    TogglePattern                           

    TransformPattern
    ValuePattern

    WindowPattern

  • 相关阅读:
    面试题系列---【watch、methods 和 computed 的区别】
    面试题系列---【vue-router是什么?有哪些路由模式?实现原理是什么】
    面试题系列---【vue中watch原理】
    面试题系列--【解决移动端1px边框问题】
    面试题系列---【vue中assets和static目录的区别】
    面试题系列---【vue中router和route区别】
    面试题系列---【mvvm 和 mvc 区别是什么?哪些场景适合?】
    面试题系列---【接口调不通,如何排查问题?】
    面试题系列---【手写一个Promise】
    JavaScript课程——Day11(BOM,宽高位置属性)
  • 原文地址:https://www.cnblogs.com/changshuo/p/3869205.html
Copyright © 2020-2023  润新知