UIAutomation 全方位深入解析

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
UOQMEUOQME
上一篇 2025-04-02 01:28
下一篇 2025-04-02 01:28

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25

发表回复

登录后才能评论