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/zh-tw/n/367994.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
UOQME的頭像UOQME
上一篇 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

發表回復

登錄後才能評論