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
class WindowProvider : IRawElementProviderSimple
{
IntPtr providerHwnd;
public WindowProvider(IntPtr hwnd)
{
providerHwnd = hwnd;
}
internal static IRawElementProviderSimple Create(
IntPtr hwnd, int idChild, int idObject)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return null;
//Check if the target is specified process
//If yes, create and run provider instance
if (processes[].MainWindowHandle != hwnd)
return null;
else
return new WindowProvider(hwnd);
}
ProviderOptions IRawElementProviderSimple.ProviderOptions
{
get
{
//Return ClientSideProvider as the implementation is in client
return ProviderOptions.ClientSideProvider;
}
}
IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
{
get
{
return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
}
}
object IRawElementProviderSimple.GetPropertyValue(int aid)
{
if (AutomationProperty.LookupById(aid) ==
AutomationElementIdentifiers.NameProperty)
{
//Our UIA.Name property implementation
//In production code, usually it uses Win32 or MSAA to get real information
return "UIA Client Implementation";
}
else
{
return null;
}
}
object IRawElementProviderSimple.GetPatternProvider(int iid)
{
//Return null means it does not support any Pattern Provider
return null;
}
}
分发客户端提供程序[1][2]
UI 自动化应在托管代码程序集中查找客户端提供程序。该程序集中的命名空间应与该程序集同名。例如,称为UIAClientProvider.dll 的程序集应包含UIAClientProvider命名空间。 在该命名空间内创建 UIAutomationClientSideProviders 类。 在静态 ClientSideProviderDescriptionTable 字段的实现中,创建用于描述该提供程序的 ClientSideProviderDescription 结构数组。
ClientSideProviderDescription 结构数组将指定以下属性:
- 一个回调函数,用于创建提供程序对象。
- 提供程序支持的控件的类名。
- 提供程序支持的应用程序的映像名(通常是可执行文件的全名)。
- 控制如何根据目标应用程序中的窗口类对类名进行匹配的标志。
最后两个参数是可选的。 如果客户端希望对不同的应用程序使用不同的提供程序,则可以指定目标应用程序的映像名。 例如,客户端可以对支持多视图模式的已知应用程序中的 Win32 列表视图控件使用一个提供程序,而对不支持多视图模式的另一个已知应用程序中的类似控件使用另一个提供程序
class UIAutomationClientSideProviders
{
public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
{ new ClientSideProviderDescription(
WindowProvider.Create,
null) };
}
注册和配置客户端提供程序[1][2]
动态链接库 (DLL) 中的客户端提供程序通过调用 RegisterClientSideProviderAssembly 进行加载。 无需进一步的操作,客户端应用程序便可以使用这些提供程序。
在客户端自己的代码中实现的提供程序通过使用 RegisterClientSideProviders 进行注册。 此方法将 ClientSideProviderDescription 结构数组用作参数。
ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);
WinForm Sample[2]
创建Console Application:UIAClientProvider,代码如下:
using System;
using System.Windows.Automation.Provider;
using System.Windows.Automation; namespace UIAClientProvider
{
class Program
{
static void Main(string[] args)
{
ClientProviderSample.ClientProviderTest();
}
} public class ClientProviderSample
{
public static void ClientProviderTest()
{
//Calling RegisterClientSideProviders API to register out Client provider
//Client Provider’s type information is stored in ClientSideProviderDescriptionTable Array
ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);
//Obtain main window of test target
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return;
IntPtr hwnd = processes[].MainWindowHandle;
//Get UIA object of the test target main window
AutomationElement elementWindow = AutomationElement.FromHandle(hwnd);
//Read UIA.Name property
Console.WriteLine(elementWindow.Current.Name);
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
class UIAutomationClientSideProviders
{
public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
{ new ClientSideProviderDescription(
WindowProvider.Create,
null) };
}
class WindowProvider : IRawElementProviderSimple
{
IntPtr providerHwnd;
public WindowProvider(IntPtr hwnd)
{
providerHwnd = hwnd;
}
internal static IRawElementProviderSimple Create(
IntPtr hwnd, int idChild, int idObject)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return null;
//Check if the target is specified process
//If yes, create and run provider instance
if (processes[].MainWindowHandle != hwnd)
return null;
else
return new WindowProvider(hwnd);
}
ProviderOptions IRawElementProviderSimple.ProviderOptions
{
get
{
//Return ClientSideProvider as the implementation is in client
return ProviderOptions.ClientSideProvider;
}
}
IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
{
get
{
return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
}
}
object IRawElementProviderSimple.GetPropertyValue(int aid)
{
if (AutomationProperty.LookupById(aid) ==
AutomationElementIdentifiers.NameProperty)
{
//Our UIA.Name property implementation
//In production code, usually it uses Win32 or MSAA to get real information
return "UIA Client Implementation";
}
else
{
return null;
}
}
object IRawElementProviderSimple.GetPatternProvider(int iid)
{
//Return null means it does not support any Pattern Provider
return null;
}
}
}
创建一个简单的WinForm Application: WinFormServer,代码如下:
using System;
using System.Windows.Forms; namespace WinFormServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); this.Name = "testForm";
this.Text = "ClientUIADemo"; }
}
}
操作:
打开WinFormServer.exe。

运行UIAClientProvider。

参考
[2] UI Automation -- Under the Hood
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 自动化提供程序从 ...
- Server-Side UI Automation Provider - WPF Sample
Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...
- MS UI Automation Introduction
MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...
- 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...
- 使用UI Automation实现自动化测试--5-7
使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...
- UI Automation 简介
转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...
- MS UI Automation简介
转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface A ...
- 使用UI Automation实现自动化测试--1-4
Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...
- Visual Studio UI Automation 学习(二)
今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/art ...
随机推荐
- c++ 接口继承和实现继承
所谓接口继承,就是派生类只继承函数的接口,也就是声明:而实现继承,就是派生类同时继承函数的接口和实现. 我们都很清楚C++中有几个基本的概念,虚函数.纯虚函数.非虚函数. 虚函数: 虚函数是指一个类中 ...
- [工作积累] NDK通过Java获取package name 和version
////////////////////////////////////////////////////////////////////////// //Java code snippet //get ...
- windows phone MVVM开发心得第一天
之前刚刚学了asp.net网站的三层架构,为其中的优点着迷,可惜寒假本来决定学下MVC的计划泡汤了,刚开学,学了下windows phone 的MVVM模式的开发,在此留下点心得和脚印,第一天只是学了 ...
- DevExpress12.2.4 GridControl相关技巧
1.DevExpress12.2.4中,设置GridControl的GridView为可编辑方法如下: gvMainControl.OptionsBehavior.Editable = true; 2 ...
- redis 数据库维护之 key 大小获取
获得 redis key 大小 redis 用过一段时间后,发现一个KEY每天需更新值,但总是更新不全,故此为了定位问题,整理此脚本,辅助监控一下 写了两个脚本 注意:需要提前从 https://gi ...
- Sql注入一种dump所有数据的方法
Select exp(~(select*from(select(concat(@:=0,(select count(*)from`information_schema`.columns where t ...
- jQuery学习记录1
jquery 和 js css里面都是坑呀 this.style.backgroundColor 和 css {background:#8df;} 是冲突的,用了前者,再$(this).addClas ...
- 历代诗词咏宁夏注释1----常星景:< 六盘>
六盘 常星景 关中形势甲天下,四岳分峙西太华.[1] 中有汭泾经纬之,六盘嵚崎历历落.[2] □□□□其流亚,终年峰头雪不消. 弟畜太白兒美高,眼底培缕纷纷何足数,呼吸想通天尺五.[3] 西北堆镇一切 ...
- ios下划线变量:为什么变量前要加下划线才有用?
先看一段代码. 复制代码 appdelegate.h @property (weak) IBOutlet NSMatrix *StockType; @property (weak) IBOutle ...
- 机器学习之神经网络模型-下(Neural Networks: Representation)
3. Model Representation I 1 神经网络是在模仿大脑中的神经元或者神经网络时发明的.因此,要解释如何表示模型假设,我们不妨先来看单个神经元在大脑中是什么样的. 我们的大脑中充满 ...