扬帆远去醉风云--开启Android之旅
Android, .NET
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 50 文章- 0 评论- 73
使用UI Automation实现自动化测试--4.5 (WindowPattern)
WindowPattern
WindowPattern 控件模式用于支持在传统的 图形用户界面 (GUI) 内提供基于基本窗口的功能的控件。必须实现此控件模式的控件的示例包括顶级应用程序窗口、多文档界面 (MDI) 子窗口、大小可调的拆分窗格控件、模式对话框以及气球状帮助窗口。可以使用WindowPattern来对window进行操作,例如验证window是否激活,是否最大化、最小化、正常模式以及关闭window等。
下面的代码演示了WindowPattern的使用方法:
Code
1
using
System;
2
using
System.Text;
3
using
System.Diagnostics;
4
using
System.Threading;
5
using
System.Windows.Automation;
6
7
namespace
UIATest
8
{
9
class
Program
10
{
11
static
void
Main(
string
[] args)
12
{
13
Process process
=
Process.Start(
@"
F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe
"
);
14
int
processId
=
process.Id;
15
AutomationElement element
=
FindWindowByProcessId(processId);
16
WindowPattern currentPattern
=
GetWindowPattern(element);
17
18
//
Set window visual state to Maximized
19
currentPattern.SetWindowVisualState(WindowVisualState.Maximized);
20
Thread.Sleep(
1000
);
21
22
//
Set window visual state to Normal
23
currentPattern.SetWindowVisualState(WindowVisualState.Normal);
24
Thread.Sleep(
1000
);
25
26
//
Set window visual state to Minimized
27
currentPattern.SetWindowVisualState(WindowVisualState.Minimized);
28
29
//
Close window
30
currentPattern.Close();
31
}
32
33
/**/
///
<summary>
34
///
Get the automation elemention of current form.
35
///
</summary>
36
///
<param name="processId">
Process Id
</param>
37
///
<returns>
Target element
</returns>
38
public
static
AutomationElement FindWindowByProcessId(
int
processId)
39
{
40
AutomationElement targetWindow
=
null
;
41
int
count
=
0
;
42
try
43
{
44
Process p
=
Process.GetProcessById(processId);
45
targetWindow
=
AutomationElement.FromHandle(p.MainWindowHandle);
46
return
targetWindow;
47
}
48
catch
(Exception ex)
49
{
50
count
++
;
51
StringBuilder sb
=
new
StringBuilder();
52
string
message
=
sb.AppendLine(
string
.Format(
"
Target window is not existing.try #{0}
"
, count)).ToString();
53
if
(count
>
5
)
54
{
55
throw
new
InvalidProgramException(message, ex);
56
}
57
else
58
{
59
return
FindWindowByProcessId(processId);
60
}
61
}
62
}
63
64
WindowPattern helper
#region
WindowPattern helper
65
66
/**/
///
<summary>
67
///
Get WindowPattern
68
///
</summary>
69
///
<param name="element">
AutomationElement instance
</param>
70
///
<returns>
WindowPattern instance
</returns>
71
public
static
WindowPattern GetWindowPattern(AutomationElement element)
72
{
73
object
currentPattern;
74
if
(
!
element.TryGetCurrentPattern(WindowPattern.Pattern,
out
currentPattern))
75
{
76
throw
new
Exception(
string
.Format(
"
Element with AutomationId '{0}' and Name '{1}' does not support the WindowPattern.
"
,
77
element.Current.AutomationId, element.Current.Name));
78
}
79
return
currentPattern
as
WindowPattern;
80
}
81
82
#endregion
83
}
84
}
85
本文简单介绍了
WindowPattern
以及
WindowPattern
在测试中的使用方法。
标签:
UI Automation
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted @ 2009-09-16 22:53
开着拖拉机
阅读(1474)
评论(1)
编辑
收藏
发表评论
1735502
回复
引用
查看
#1楼
2009-12-28 12:33
|
mianxiang
Hi Kangyi,
很高兴能看到你的文章,谢谢你的分享:)
我现在碰到了一个问题,不知道如何解决:我用UISpy查看一个窗口时,它显示是支持WindowPattern 和 TransformPattern的,但是当我找到这个窗口的AuotmationElement之后,调用TryGetCurrentPattern去获得WindowPattern和TransformPattern时,总会出现异常。不知道这个是啥原因引起的?有没有什么解决的办法?
谢谢。
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
猎聘网网络招聘新势力
·
京东被指压榨供货商续:苛刻条款或只针对小企业
·
Facebook公布犯罪团伙名单被批妨碍司法调查
·
传谷歌正开发家庭娱乐系统 以自有品牌销售
·
平安信托折戟Facebook 中国富豪梦碎IPO盛宴
»
更多新闻...
最新知识库文章
:
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
·
前端必读:浏览器内部工作原理
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
公告
昵称:
开着拖拉机
园龄:
2年10个月
粉丝:
8
关注:
2
<
2009年9月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
更多链接
最新随笔
1. 个税计算器完美终极版
2. Crazy English 900 Expressions (Android App)
3. 重启职业及生存旅途计划
4. 赌一把
5. 生存之道
6. 生活随想之 积累经验篇
7. partial class在自动化测试中的使用
8. UIA 抛出COMException的解决方案
9. 使用UI Automation实现自动化测试--7.2 (模拟键盘复杂操作在自动化测试中的应用)
10. 使用UI Automation实现自动化测试--7.1 (模拟键盘输入数据在自动化测试中的应用)
我的标签
UI Automation(18)
回文(1)
触发器(1)
trigger(1)
sql server(1)
App Domain(1)
Automation Test(1)
解决方案(1)
职业(1)
技术(1)
随笔分类
(41)
Android series(2)
(rss)
ASP.NET(2)
(rss)
C#(6)
(rss)
Data Structure(2)
(rss)
SQL Server(1)
(rss)
UI Automation(18)
(rss)
Windows phone 7
(rss)
Windows server 2008(5)
(rss)
日积月累(2)
(rss)
生活随想(3)
(rss)
随笔档案
(35)
2011年12月 (1)
2010年10月 (1)
2010年4月 (1)
2010年3月 (2)
2010年1月 (1)
2009年12月 (2)
2009年11月 (2)
2009年10月 (4)
2009年9月 (13)
2009年7月 (2)
2009年6月 (1)
2009年5月 (5)
最新评论
阅读排行榜
评论排行榜
推荐排行榜