Ranorex UI适配器

使用Ranorex Spy跟踪一个UI元素时,Ranorex会尝试指定该元素的属性和作用。Ranorex可识别超过30种的最常用的UI元素类型,在Ranorex词汇表中,它们被称作”adapters”。对Ranorex Spy的元素树表中每个元素的访问,通过该元素支持的Ranorex UI adapters来实现。如果Ranorex不能给一个UI元素指定具体的adapter类型,就显示为”Unknown”。

Ranorex adapters为创建自动化测试提供了很好的便利性。举个例子:Ranorex Text adapter提供文本框相关属性,如”Text”或者“Selection Text”。相比简单的Text adapter,Ranorex TreeItem adapter提供了tree元素特定的属性和方法,比如展开和折叠。Ranorex Spy在“Overview”选项中显示了所有adapters相关的属性。

 

text-field-11
Ranorex Text adapter的属性。

 

tree-item-29
Ranorex TreeItem adapter的属性。
Ranorex Spy的“General”部分,显示了所有UI元素拥有的常规属性。“Overview”选项中的所有信息则与元素支持的adapter的类型相关。

 

下例说明了如何访问Ranorex Text adapter的属性。

C#

// Create Text adapter from RanoreXPath
Text textBox = “/form[@title=’Calculator’]/text[@controlid=’403′]”;
// Set text value of text box
textBox.TextValue = “12”;

// Check on of the ‘General’ properties
if (textBox.Enabled)
Report.Info(“Text is enabled”);

 

VB.NET

‘ Create Text adapter from RanoreXPath
Dim textBox As Text = “/form[@title=’Calculator’]/text[@controlid=’403′]”
‘ Set text value of text box
textBox.TextValue = “12”

‘ Check on of the ‘General’ properties
If textBox.Enabled Then
Report.Info(“Text is enabled”)
End If

 

 

下例说明了如何访问Ranorex TreeItem adapter的属性。

C#

// Create TreeItem adapter from RanoreXPath
TreeItem treeItem= “/form[@title=’Documents and Settings’]/*/*/*/*
/*/*/treeitem[@text=’Documents and Settings’]”;
// Collapse and expand tree item
if (treeItem.Expanded)
treeItem.Collapse();
else
treeItem.Expand();

 

VB.NET

‘ Create TreeItem adapter from RanoreXPath
Dim treeItem As TreeItem = “/form[@title=’Documents and
Settings’]//treeitem[@text=’Documents and Settings’]”
‘ Collapse and expand tree item
If treeItem.Expanded Then
treeItem.Collapse()
Else
treeItem.Expand()
End If

 

一个GUI元素的多个adapters

除了描述UI元素逻辑视图的Ranorex标准adapters外,Ranorex提供的特定技术adapters,描述了更多的其指向元素相关信息。

比如Ranorex WinForms 插件是能够提供.NET 应用程序中一个简单的button元素的详细信息。这些附加信息由一个附加的叫”Control”的adapter提供。以下截图比较了Ranorex Spy 的 Win32 和 WinForms 下button所显示的信息:

native-button-14

计算器应用的按钮,提供了Ranorex Button和Ranorex NativeWindow适配器

 

win-forms-button-ec

.NET WinForms的按钮,提供了额外的Ranorex Control适配器
Control adapter,可用于.NET WinForms应用程序,允许Ranorex访问更多的待测应用程序的属性,比如显示文字的背景颜色或字体大小。标准button adapter转换为WinForm Control adapter的简单方法如下:
C#

// Create Button adapter with RanoreXPath
Ranorex.Button button= “/form[@controlname=’TestedApp’]
/button[@controlname=’button1′]”;
// Convert Button adapter to Control adapter
Ranorex.Control winFormsButton = new Ranorex.Control(button);
// Call ‘GetPropertyValue’ method provided by the Control adapter
Color color = winFormsButton.GetPropertyValue<Color>(“BackColor”);

 

VB.NET

‘ Create Button adapter with RanoreXPath
Dim button As Ranorex.Button = “/form[@controlname=’TestedApp’]
/button[@controlname=’button1′]”
‘ Convert Button adapter to Control adapter
Dim winFormsButton As New Ranorex.Control(button)
‘ Call ‘GetPropertyValue’ method provided by the Control adapter
Dim color As Color = winFormsButton.GetPropertyValue(Of Color)(“BackColor”)

 

使用Ranreox adapters并非必须, 在编写自动化测试代码时,也可以使用Ranorex元素实例(在Ranorex adapters内部使用)——不过这会更复杂些。

Note: UI objects managed within Ranorex repositories always use Ranorex adapters.
注意:Ranreox 对象库中UI对象的管理总是使用Ranorex adapters。

Comments 1

  1. Reply

    我在声明一个按钮的时候,比如例子中的 Ranorex.Button button= “/form[@controlname=’TestedApp’]/button[@controlname=’button1′]”;
    必须要先找到这个按钮才行?我发现如果按钮处于不可见的状态直接报错,这样是不是不合理啊?比如button.Visible就没意义了啊,因为给button赋值的时候如果不可见直接就弹错退出了,所以button.Visible就肯定是True了啊。感觉很奇怪,是不是我用的方法有问题啊?这样做成变量岂不是很不方便?

灰暗星辰进行回复 取消回复

请输入正确的验证码