自动化测试:UIA

      在win8/8.1上做Metro app 的自动化测试,其中主要用到UIA相关的技术,下面就作一个简单的记录:

UIA原理图:(摘抄)

   

 

UIA基础:(VB.net版)

1.添加应用:UIAutomationClient.dll ,UIAutomationTypes.dll

 

2.UIA 的一些常用方法: 

1>对于一般的桌面应用程序,我们首先都会获取根元素,然后再通过FindFirst/FindAll 来寻找子元素: 

Dim Desktop As AutomationElement = AutomationElement.RootElement

  但是在Win8中对于Metro UI这个就不好用了,所以我们得用它的另外一个方法:

AutomationElement.FromHandle(HWND).FindFirst(......

 

2>根据条件查找:

单一条件: 

.FindAll(TreeScope.Children, New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem))

多条件:

.FindFirst(TreeScope.Descendants, New AndCondition(New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List),
New PropertyCondition(AutomationElement.AutomationIdProperty, "xx")))

找到所以孩子:

.FindAll(TreeScope.Children, Condition.TrueCondition)

 

 

3>获取属性值:   

.GetCurrentPropertyValue(AutomationElementIdentifiers.NameProperty)

 

4>操作:

点击button:

DirectCast(btn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern).Invoke()

选择:

DirectCast(ele.GetCurrentPattern(SelectionItemPattern.Pattern), SelectionItemPattern).Select()
如果没选中:
If DirectCast(ele.GetCurrentPattern(SelectionItemPattern.Pattern), SelectionItemPattern).Current.IsSelected() = False Then
end if

ToggleSwitchs:如果=off ,则设为ON

If DirectCast(toggleswitch.GetCurrentPattern(TogglePattern.Pattern), TogglePattern).Current.ToggleState.ToString.Equals("Off") Then
DirectCast(toggleswitch.GetCurrentPattern(TogglePattern.Pattern), TogglePattern).Toggle() End If

combbox:

DirectCast(combbox.GetCurrentPattern(ExpandCollapsePattern.Pattern), ExpandCollapsePattern).Expand()

editbox:

设值:
DirectCast
(editbox.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).SetValue("xx")
获取里面的值:
DirectCast(editbox.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).Current.Value

获取第一个子孩子的值:

TreeWalker.ControlViewWalker.GetFirstChild(xxEle)

获取元素的坐标:

GetCurrentPropertyValue(AutomationElement.ClickablePointProperty).ToString()

 

参考:Test win8 app

http://blogs.msdn.com/b/windowsappdev/archive/2012/09/04/automating-the-testing-of-windows-8-apps.aspx

posted on 2014-02-20 13:44  Alvin-x  阅读(3081)  评论(0编辑  收藏  举报

导航