• UI Automation


     public Form1()
            {
                InitializeComponent();
                this.textBox1.AccessibilityObject.Name = "t1";
                this.textBox3.AccessibilityObject.Name = "t3";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var t = this.textBox1;
                int i = int.Parse(textBox1.Text);
                int j = int.Parse(textBox2.Text);
    
                textBox3.Text = (i + j).ToString();
            }
    
            private void testBtn_Name_Click(object sender, EventArgs e)
            {
                textBox3.Text = "test";
            }
    

    上面是被自动化的代码。

     1 /// <summary>
     2         /// 应用程序的主入口点。
     3         /// </summary>
     4         [STAThread]
     5         static void Main()
     6         {
     7             //启动被测试的程序
     8             Process p = Process.Start(@"C:vs2015项目AutomationAutomationinDebugAutomation.exe");
     9 
    10             //自动化根元素,可以认为是桌面.
    11             AutomationElement aeDeskTop = AutomationElement.RootElement;
    12 
    13             Thread.Sleep(2000);
    14             AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
    15 
    16             #region 获取 button 并模拟点击事件。
    17 
    18             //找到窗体。
    19             var testForm = aeDeskTop.FindFirst(TreeScope.Children, new PropertyCondition(
    20                     AutomationElement.NameProperty, "Form1"));
    21 
    22             //通过条件找到窗体里面的 btn。这个位置 name 属性对应控件的 Text 属性。
    23             var btnCondition = new AndCondition(
    24                  new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
    25                  new PropertyCondition(AutomationElement.NameProperty, "testBtn"));
    26 
    27             var singalCondition = new PropertyCondition(AutomationElement.NameProperty, "testBtn");
    28 
    29             var testBtn = testForm.FindFirst(TreeScope.Children, btnCondition); // 在桌面的子控件中查找第一个符合条件的窗体。
    30 
    31             //通过InvokePattern模拟点击按钮
    32             InvokePattern ipClickTestBtn = (InvokePattern)testBtn.GetCurrentPattern(InvokePattern.Pattern);
    33             ipClickTestBtn.Invoke();
    34 
    35             #endregion
    36 
    37             #region 获取 textbox  并赋值
    38 
    39             var txtCondition1 = new AndCondition(
    40                 new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
    41                 new PropertyCondition(AutomationElement.NameProperty, "t1"));
    42 
    43             var txtCondition3 = new AndCondition(
    44                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
    45                new PropertyCondition(AutomationElement.NameProperty, "t3"));
    46 
    47             //设置值
    48             var testText1 = testForm.FindFirst(TreeScope.Children, txtCondition1);
    49             ValuePattern t1 = (ValuePattern)testText1.GetCurrentPattern(ValuePattern.Pattern);
    50             t1.SetValue("30");
    51 
    52             //获取值
    53             var testText3 = testForm.FindFirst(TreeScope.Children, txtCondition3);
    54             var t3 = (TextPattern)testText3.GetCurrentPattern(TextPattern.Pattern);
    55             string result2 = t3.DocumentRange.GetText(-1);
    56 
    57             #endregion
    58 
    59             #region 实现关闭被测试程序
    60 
    61             //实现关闭被测试程序
    62             WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern);
    63 
    64             #endregion
    65 }

    上面是控制代码

      

  • 相关阅读:
    关于python3在centOS7下源码安装的配置
    nginx服务器多虚拟主机配置
    完全数
    高精度计算组合数
    算法竞赛入门经典第六章总结
    线段树
    奶牛
    算法竞赛入门经典第五章总结
    优先队列的使用方法
    放魔法石的游戏
  • 原文地址:https://www.cnblogs.com/HKKD/p/7541700.html
Copyright © 2020-2023  润新知