UIAutomation(用戶界面自動化)是一種可以用來測試和控制應用程序界面的 Microsoft 技術。它允許測試人員模擬用戶交互,比如單擊按鈕和填寫文本字段,並驗證控件的屬性和狀態。UIAutomation 除了可以用於測試之外,還可以用來開發自動化任務,如 UI 自動化流程。在本文中,我們將從多個角度深入剖析 UIAutomation 的應用與技術實現,並提供相應的代碼示例。
一、UIAutomationCore.dll的安裝
UIAutomation 使用 UIAutomationCore.dll 作為核心擴展庫,所以安裝該庫時很重要。安裝 UIAutomationCore.dll 有以下幾種方式:
1、通過「組件服務」進行UIAutomationCore.dll安裝。在「組件服務」中找到「計算機」目錄下的「DCOM 配置」 ,並新建一個對應的應用程序ID,再創建一個對應應用程序ID的新類,並把「天文出版社」一項設置為「庫自身是否可創建實例」。這種方式需要高級用戶級別的權限,並且可以用於本地和遠程計算機。
//示例代碼: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Process process = new Process(); process.StartInfo.FileName = "regsvr32"; process.StartInfo.Arguments = "/u UIAutomationCore.dll"; process.Start(); process.WaitForExit(); } } }
2、將 UIAutomationCore.dll 文件放置在應用程序的工作目錄中。這種方式需要手動複製和配置,需要更多的管理工作,但不需要高級權限。
其他可行的方式還包括:使用 NuGet 程序包管理器打包和分發 UIAutomationCore.dll,使用 Visual Studio 安裝嚮導或 PowerShell 腳本進行自定義部署流程等方式。
二、UIAutomation RPA (機械人流程自動化)
UIAutomation RPA 是一種可以利用 UIAutomation 庫實現的自動化流程,通常用於取代人工任務。這種技術可以將複雜的任務切分為簡單的操作項,並模擬各種人類交互方式,包括鼠標單擊、微調窗口大小和位置、鍵盤輸入等。
下面是一段使用UIAutomation RPA 進行百度精確搜索的示例代碼。該程序將自動打開Chrome瀏覽器並跳轉到百度首頁,之後填寫搜索框並確認搜索,並最後輸出搜索結果頁面的標題。
// 示例代碼: using System; using System.Threading; using System.Windows.Automation; namespace UIAutomation_RPA { class Program { static void Main(string[] args) { AutomationElement browser = OpenBrowser(); AutomationElement searchBox = FindSearchBox(); EnterText(searchBox, "UIAutomation RPA"); Thread.Sleep(1000); Keyboard.Press(System.Windows.Input.Key.Enter); MessageBox.Show("搜索結果為:" + GetPageTitle(browser)); } static AutomationElement OpenBrowser() { Process.Start("chrome.exe", "http://www.baidu.com"); return AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "百度一下,你就知道")); } static AutomationElement FindSearchBox() { AutomationElement searchBoxContainer = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "s_form")); return searchBoxContainer.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "s_ipt")); } static void EnterText(AutomationElement element, string text) { ValuePattern valuePattern = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern); valuePattern.SetValue(text); } static string GetPageTitle(AutomationElement browser) { browser.SetFocus(); return ((ValuePattern)browser.GetCurrentPattern(ValuePattern.Pattern)).Current.Value; } } }
三、UIAutomation Python
Python 是一種常用的腳本語言,並且也可以使用 UIAutomation 庫進行UI自動化的開發。下面是一段使用 Python 實現跨平台桌面自動化(Desktop Automation)功能的代碼。
// 示例代碼: import uiautomation as automation import time #打開記事本程序 notepad = automation.Popen('notepad.exe') #在記事本裏面創建一個新文件 notepad.EditControl().SendKeys('Hello UIAutomation for Python' + automation.SendKeys('{CTRL}{s}')) time.sleep(0.2) #在文件保存對話框中輸入文件名 save = automation.WindowControl(searchDepth=1, ClassName='#32770', RegexName=r'另存為') save['文件名(N):Edit'].SetValue('test.txt') save['保存(S)Button'].Click() time.sleep(0.2) #關閉記事本程序 notepad.Close()
四、C語言UI Automation
UI Automation 支持多種編程語言,包括 C 語言。下面是一段使用C語言實現 UI Automation 的示例代碼,它演示了如何遠程打開筆記本電腦的 RemoteApp 程序。
// 示例代碼: #include "stdafx.h" #include "UIAutomation.h" #include using namespace System; UIAutomation::UIAutomation() { _desktop = AutomationElement->RootElement; } AutomationElement^ UIAutomation::FindRemoteApp() { Condition^ condition = gcnew AndCondition(gcnew PropertyCondition(AutomationElement.ProcessIdProperty, _processId), gcnew PropertyCondition(AutomationElement.ClassNameProperty, "RemoteApplicationModuleClass")); return _desktop->FindFirst(TreeScope::Subtree, condition); } void UIAutomation::OpenRemoteApp(String^ server, String^ userName, String^ password, String^ appName) { Process^ rdcProcess = gcnew Process(); rdcProcess->StartInfo->FileName = "mstsc.exe"; rdcProcess->StartInfo->Arguments = "/v: " + server + " /u: " + userName + " /p: " + password + " /f /remoteapp " + appName; rdcProcess->Start(); _processId = rdcProcess->Id; Console::WriteLine("RemoteApp ProcessID: " + _processId); } void UIAutomation::CloseRemoteApp() { AutomationElement^ remoteApp = FindRemoteApp(); IInvokeProvider^ invokeProvider = (IInvokeProvider^)remoteApp->GetCurrentPattern(InvokePattern::Pattern); invokeProvider->Invoke(); } int main(array^ args) { UIAutomation^ automation = gcnew UIAutomation(); automation->OpenRemoteApp("server", "admin", "password", "notepad"); Console::WriteLine("The RemoteApp program is running."); Console::ReadLine(); automation->CloseRemoteApp(); Console::WriteLine("The RemoteApp program is closed."); Console::ReadLine(); return 0; }
總結
本文詳細介紹了UIAutomation 技術的應用和相關的技術實現。我們討論了UIAutomationCore.dll的安裝方法、UIAutomation RPA 技術、Python 語言和C語言UIAutomation開發實現。希望讀者可以根據本文提供的代碼示例,開發出更加高效、可維護的 UI 自動化和自動化流程。
原創文章,作者:UOQME,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/367994.html