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/n/367994.html
微信扫一扫
支付宝扫一扫