项目搭建(一):windows UIAutomation API 框架

【环境】编程

操做系统:Windows7框架

集成环境:Visual Studio2015编程语言

编程语言:C#ide

目标框架:.net framework4.6工具

 

一、新建项目spa

Visual Studio 2015 【文件】->【新建】->【项目】->Visual C#(控制台应用程序)操作系统

 

二、添加引用.net

项目->引用->添加引用-> 打开引用管理器,在程序集搜索UIAutomationcode

引用 UIAutomationClient、UIAutomationClientsideProviders、UIAutomationProvider、UIAutomationTypes。blog

 

 

 

备注:这4个dll文件所在路径:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0

 

三、Inspect.exe和UISpy.exe工具

查找控件属性

 

 

查看控件模式,打开UI Spy工具,在左边的树目录中右键须要查看的控件,点击“Control Patterns”

 

四、demo脚本

该demo脚本可启动应用程序并进行登陆

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

namespace myProject01
{
    class Program
    {
        static void Main(string[] args)
        {
            Process p = Process.Start(@"D:\xx\xx.exe");//启动应用程序
            Thread.Sleep(10000);
            AutomationElement desktop = AutomationElement.RootElement;//获取RootElement
            //获取当前窗口
            AutomationElement posframe = desktop.FindFirst(TreeScope.Descendants | TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "SystemLogin"));
            //输入用户名控件
            AutomationElement usertext = posframe.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "txt_no"));
            //输入密码控件
            AutomationElement pwdtext = posframe.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "pb_pwd"));
            //登陆控件
            AutomationElement loginBtn = posframe.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "btn_login"));
            //输入用户名
            ValuePattern username = (ValuePattern)usertext.GetCurrentPattern(ValuePattern.Pattern);
            username.SetValue("0114");
            //输入密码
            ValuePattern password = (ValuePattern)pwdtext.GetCurrentPattern(ValuePattern.Pattern);
            password.SetValue("0114");
            //点击登陆
            InvokePattern ivkp = (InvokePattern)loginBtn.GetCurrentPattern(InvokePattern.Pattern);
            ivkp.Invoke(); //触发控件事件
        }
    }
}
相关文章
相关标签/搜索