Client-Side UI Automation Provider - WinForm Sample

Client-Side UI Automation Provider -  WinForm Sample

2014-09-15

引用程序集

实现提供程序接口

分发客户端提供程序

注册和配置客户端提供程序

WinForm Sample

参考

引用程序集[1]



返回

  • UIAutomationClient.dll
  • UIAutomationProviders.dll
  • UIAutomationTypes.dll

实现提供程序接口[2]



返回

以下示列实现提供程序接口:IRawElementProviderSimple

 1     class WindowProvider : IRawElementProviderSimple
 2     {
 3         IntPtr providerHwnd;
 4         public WindowProvider(IntPtr hwnd)
 5         {
 6             providerHwnd = hwnd;
 7         }
 8         internal static IRawElementProviderSimple Create(
 9             IntPtr hwnd, int idChild, int idObject)
10         {
11             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
12             if (processes.Length == 0)
13                 return null;
14             //Check if the target is specified process
15             //If yes, create and run provider instance
16             if (processes[0].MainWindowHandle != hwnd)
17                 return null;
18             else
19                 return new WindowProvider(hwnd);
20         }
21         ProviderOptions IRawElementProviderSimple.ProviderOptions
22         {
23             get
24             {
25                 //Return ClientSideProvider as the implementation is in client
26                 return ProviderOptions.ClientSideProvider;
27             }
28         }
29         IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
30         {
31             get
32             {
33                 return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
34             }
35         }
36         object IRawElementProviderSimple.GetPropertyValue(int aid)
37         {
38             if (AutomationProperty.LookupById(aid) ==
39                 AutomationElementIdentifiers.NameProperty)
40             {
41                 //Our UIA.Name property implementation
42                 //In production code, usually it uses Win32 or MSAA to get real information
43                 return "UIA Client Implementation";
44             }
45             else
46             {
47                 return null;
48             }
49         }
50         object IRawElementProviderSimple.GetPatternProvider(int iid)
51         {
52             //Return null means it does not support any Pattern Provider
53             return null;
54         }
55     }

分发客户端提供程序[1][2]



返回

UI 自动化应在托管代码程序集中查找客户端提供程序。该程序集中的命名空间应与该程序集同名。例如,称为UIAClientProvider.dll 的程序集应包含UIAClientProvider命名空间。 在该命名空间内创建 UIAutomationClientSideProviders 类。 在静态 ClientSideProviderDescriptionTable 字段的实现中,创建用于描述该提供程序的 ClientSideProviderDescription 结构数组。

ClientSideProviderDescription 结构数组将指定以下属性:

  • 一个回调函数,用于创建提供程序对象。
  • 提供程序支持的控件的类名。
  • 提供程序支持的应用程序的映像名(通常是可执行文件的全名)。
  • 控制如何根据目标应用程序中的窗口类对类名进行匹配的标志。

最后两个参数是可选的。  如果客户端希望对不同的应用程序使用不同的提供程序,则可以指定目标应用程序的映像名。 例如,客户端可以对支持多视图模式的已知应用程序中的 Win32 列表视图控件使用一个提供程序,而对不支持多视图模式的另一个已知应用程序中的类似控件使用另一个提供程序

1     class UIAutomationClientSideProviders
2     {
3         public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
4             { new ClientSideProviderDescription(
5                 WindowProvider.Create,
6                 null) };
7     }

注册和配置客户端提供程序[1][2]



返回

动态链接库 (DLL) 中的客户端提供程序通过调用 RegisterClientSideProviderAssembly 进行加载。  无需进一步的操作,客户端应用程序便可以使用这些提供程序。

在客户端自己的代码中实现的提供程序通过使用 RegisterClientSideProviders 进行注册。  此方法将 ClientSideProviderDescription 结构数组用作参数。

ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);

WinForm Sample[2]



返回

创建Console Application:UIAClientProvider,代码如下:

 1 using System;
 2 using System.Windows.Automation.Provider;
 3 using System.Windows.Automation;
 4
 5 namespace UIAClientProvider
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             ClientProviderSample.ClientProviderTest();
12         }
13     }
14
15     public class ClientProviderSample
16     {
17         public static void ClientProviderTest()
18         {
19             //Calling RegisterClientSideProviders API to register out Client provider
20             //Client Provider’s type information is stored in ClientSideProviderDescriptionTable Array
21             ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);
22             //Obtain main window of test target
23             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
24             if (processes.Length == 0)
25                 return;
26             IntPtr hwnd = processes[0].MainWindowHandle;
27             //Get UIA object of the test target main window
28             AutomationElement elementWindow = AutomationElement.FromHandle(hwnd);
29             //Read UIA.Name property
30             Console.WriteLine(elementWindow.Current.Name);
31             Console.WriteLine();
32             Console.WriteLine("Press any key to exit.");
33             Console.ReadLine();
34         }
35     }
36     class UIAutomationClientSideProviders
37     {
38         public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
39             { new ClientSideProviderDescription(
40                 WindowProvider.Create,
41                 null) };
42     }
43     class WindowProvider : IRawElementProviderSimple
44     {
45         IntPtr providerHwnd;
46         public WindowProvider(IntPtr hwnd)
47         {
48             providerHwnd = hwnd;
49         }
50         internal static IRawElementProviderSimple Create(
51             IntPtr hwnd, int idChild, int idObject)
52         {
53             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
54             if (processes.Length == 0)
55                 return null;
56             //Check if the target is specified process
57             //If yes, create and run provider instance
58             if (processes[0].MainWindowHandle != hwnd)
59                 return null;
60             else
61                 return new WindowProvider(hwnd);
62         }
63         ProviderOptions IRawElementProviderSimple.ProviderOptions
64         {
65             get
66             {
67                 //Return ClientSideProvider as the implementation is in client
68                 return ProviderOptions.ClientSideProvider;
69             }
70         }
71         IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
72         {
73             get
74             {
75                 return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
76             }
77         }
78         object IRawElementProviderSimple.GetPropertyValue(int aid)
79         {
80             if (AutomationProperty.LookupById(aid) ==
81                 AutomationElementIdentifiers.NameProperty)
82             {
83                 //Our UIA.Name property implementation
84                 //In production code, usually it uses Win32 or MSAA to get real information
85                 return "UIA Client Implementation";
86             }
87             else
88             {
89                 return null;
90             }
91         }
92         object IRawElementProviderSimple.GetPatternProvider(int iid)
93         {
94             //Return null means it does not support any Pattern Provider
95             return null;
96         }
97     }
98 }

创建一个简单的WinForm Application: WinFormServer,代码如下:

 1 using System;
 2 using System.Windows.Forms;
 3
 4 namespace WinFormServer
 5 {
 6     public partial class Form1 : Form
 7     {
 8         public Form1()
 9         {
10             InitializeComponent();
11
12             this.Name = "testForm";
13             this.Text = "ClientUIADemo";
14
15         }
16     }
17 }

操作:

打开WinFormServer.exe。

运行UIAClientProvider。

参考

[1] 客户端 UI 自动化提供程序的实现

[2] UI Automation -- Under the Hood

时间: 2024-08-28 13:23:16

Client-Side UI Automation Provider - WinForm Sample的相关文章

Server-Side UI Automation Provider - WinForm Sample

Server-Side UI Automation Provider - WinForm Sample 2014-09-14 引用程序集 提供程序接口 公开服务器端 UI 自动化提供程序 从 UI 自动化提供程序返回属性 从 UI 自动化提供程序中引发事件 在 UI 自动化提供程序中支持控件模式 WinForm Sample 参考 引用程序集[1] 返回 UI 自动化提供程序项目必须引用以下程序集: UIAutomationProviders.dll UIAutomationTypes.dll

Server-Side UI Automation Provider - WPF Sample

Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAutomationProviders.dll UIAutomationTypes.dll WindowsBase.dll 自动化对等类[1] 返回 WPF 控件通过派生自 AutomationPeer 的对等类的树来支持 UI 自动化.  按照约定,对等类的名称须以控件类的名称开头,并以“Automat

MS UI Automation Introduction

MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自动化属性概述 UI 自动化事件概述 示例 使用UISpy工具 UI自动化提供者 常见问题分析解决 控件无法识别 Timing issue 本地化问题 自动化技术和自动化框架 参考 MS UI Automation是什么[1] 返回 UI Automation 就是用另一个程序来控制UI 程序,模拟用

使用UI Automation实现自动化测试--5-7

使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方,而这些不同来自于Winform与WPF的处理机制不同.下面我们通过一个简单的实例来加以说明: 实例描述 我们使用InvokePattern来点击按钮弹出一个对话框,然后点击对话框中的“确定”按钮关闭对话框. 两种方式对比 首先我们使用如下代码来针对Winfom和WPF分别进行测试: 1public

MS UI Automation简介

转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词.新技术出来的所谓的21世纪,它显得已经有些过时了.前些日子,正好一个项目,可以用到它,又重新整理了一下: 什么是MS UI Automation MS UI Automation是MS

UI Automation 简介

转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词.新技术出来的所谓的21世纪,它显得已经有些过时了.前些日子,正好一个项目,可以用到它,又重新整理了一下: 什么是MS UI Automation MS UI Automati

UI Automation

Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active Accessibility.UI Automation在某些方面超过了MSAA,UI自动化提供了Windows Vista中,微软Windows XP的全部功能,和Windows Server 2003. 在UI Automation中,所有的窗体.控件都表现为一个AutomationElement

Visual Studio UI Automation 学习(二)

今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/article/details/78960888 https://blog.csdn.net/qq_37546891/article/details/78959291 重点:UI Automation是.Net Framework框架的4个dll文件: UIAutomationClient UIAutom

iOS instruments之ui automation的简单使用(高手绕道)

最近使用了几次instruments中的automation工具,现记录下automation的简单使用方法,希望对没接触过自动化测试又有需求的人有所帮助.  UI 自动测试是iOS 中重要的附加功能,它由名为"Automation"的新的工具对象支持.Automation工具的脚本是用JavaScript语言编写,主要用于分析应用的性能和用户行为,模仿/击发被请求的事件,利用它可以完成对被测应用的简单的UI测试及相关功能测试. 一. 简单的录制脚本 打开xcode,这里用我为我家亲爱