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。

参考

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

[2] UI Automation -- Under the Hood

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

  1. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  2. Server-Side UI Automation Provider - WPF Sample

    Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...

  3. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  4. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

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

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  6. UI Automation 简介

    转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...

  7. MS UI Automation简介

    转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface A ...

  8. 使用UI Automation实现自动化测试--1-4

    Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...

  9. Visual Studio UI Automation 学习(二)

    今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/art ...

随机推荐

  1. [原]TCP/UDP使用细节备忘

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. delete错误

    今天找了半天delete错误,后来才知道是MTd和MDd模式的问题,MTd的内存申请和释放必须在同一个模块里面,接口上面不能使用stl等,MDd可以使用.改成MDd就可以了

  3. 引擎设计跟踪(九.8) Gizmo helper实现与多国语言

    最近把gizmo helper的绘制做好了. 1.为了复用代码,写了utility来创建sphere, cube, cylinder, plane, ring(line), circle(solid) ...

  4. 停止使用循环 教你用underscore优雅的写代码

    你一天(一周)内写了多少个循环了? var i; for(i = 0; i < someArray.length; i++) {   var someThing = someArray[i]; ...

  5. 白话CSS3的新特性

    声明:这篇文章不是手册,所以不会说的很详细,只是告诉初学者CSS3显著的改进有啥,高手老手绕行. 一.在边框上的改进 1.可以给方框加圆角了,值越大越圆,解决了过去大方框的不美观 2.可以给控件加阴影 ...

  6. make_pair() (STL)

    转载来的 Pairs C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和multimap就是使用pairs来管理其 ...

  7. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  8. JAVA Map集合类简介

    了解最常用的集合类型之一 Map 的基础知识以及如何针对您应用程序特有的数据优化 Map. 本文相关下载: · Jack 的 HashMap 测试· Oracle JDeveloper 10g jav ...

  9. Sold out

    When will the writer see the play? 'The play may begin at any moment,'I said. 'It may have begun alr ...

  10. lintcode:将二叉查找树转换成双链表

    题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...