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. Ubuntu下配置Docbook环境

    1.准备环境 $sudo apt-get install xsltproc $sudo apt-get install docbook-xsl $sudo apt-get install docboo ...

  2. awk 统计数据在文件中的出现次数

    突然发现awk原来可以统计同一数据在要处理的文件中所出现的次数.原来的时候为了分析数据还自己写程序,哎,无语,当时还以为自己多强,手工分析不过来的东西写程序处理.现在想来实在是年少轻狂.解决问题嘛,不 ...

  3. angularjs取Sevice和directive的引用

    取Sevice和directive的引用 3: Grab any Services We can grab a reference to any service using the injector  ...

  4. HTTP状态码一览表(HTTP Status Code)

    copy from:http://www.189works.com/article-43064-1.html 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 ( ...

  5. 用wget实现cookie欺骗

    用wget实现cookie欺骗 . 分析登录界面的html代码 页面在 http://bbs.linuxeden.com/ <form. id="loginform" met ...

  6. SQL技术内幕-5 比较特殊 insert into 数据的写法

    ---比较特殊,第一次看到这种写法,记录下来 create table Student --学生成绩表 ( id int, --主键 Grade int, --班级 Score int --分数 ) ...

  7. 【转】dip,px,pt,sp 的区别

    dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...

  8. Linux客户/服务器程序设计范式2——并发服务器(进程池)

    引言 让服务器在启动阶段调用fork创建一个子进程池,通过子进程来处理客户端请求.子进程与父进程之间使用socketpair进行通信(为了方便使用sendmsg与recvmsg,如果使用匿名管道,则无 ...

  9. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  10. Java 网络编程(一)

    网络通讯要素 IP地址(InetAddress) 端口号 传输协议 图示: TDP/IP模型 以OSI 7层模型讲解数据的传输 InetAddress   InetAddress类主要表示IP地址. ...