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


【環境】

操作系統:Windows7

集成環境:Visual Studio2015

編程語言:C#

目標框架:.net framework4.6

 

1、新建項目

Visual Studio 2015 【文件】->【新建】->【項目】->Visual C#(控制台應用程序)

 

2、添加引用

項目->引用->添加引用-> 打開引用管理器,在程序集搜索UIAutomation

引用 UIAutomationClient、UIAutomationClientsideProviders、UIAutomationProvider、UIAutomationTypes。

 

 

 

備注:這4個dll文件所在路徑:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0

 

3、Inspect.exe和UISpy.exe工具

查找控件屬性

 

 

查看控件模式,打開UI Spy工具,在左邊的樹目錄中右鍵需要查看的控件,點擊“Control Patterns”

 

4、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(); //觸發控件事件
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM