本文主要是介绍QTP测试CodeJock Xtreme Suite控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CodeJock Xtreme Suite是VB编程中经常使用的类库,但是在使用QTP进行测试时往往碰到很多对象识别和控制的问题。
DatePicker
使用QTP录制DatePicker控件得到如下脚本:
'' 录制的脚本
'VbWindow("frmMain").Activate
'' 选择一个日期
'VbWindow("frmMain").ActiveX("Xtreme DatePicker Control").WinObject("XTPDatePicker").Click 361,91
'' 选择日期范围
'VbWindow("frmMain").ActiveX("Xtreme DatePicker Control").WinObject("XTPDatePicker").Drag 526,80
'VbWindow("frmMain").ActiveX("Xtreme DatePicker Control").WinObject("XTPDatePicker").Drop 526,98
对DatePicker封装常用操作函数并用RegisterUserFunc函数注册到ActiveX测试对象中:
' 获取选定的单个日期
Function Xtreme_DataPicker_GetSelectedDate(obj)
Xtreme_DataPicker_GetSelectedDate = obj.Object.Selection.Blocks(0).DateBegin
End Function
RegisterUserFunc "ActiveX","Xtreme_DataPicker_GetSelectedDate","Xtreme_DataPicker_GetSelectedDate"
' 获取开始日期
Function Xtreme_DataPicker_StartDate(obj)
Xtreme_DataPicker_StartDate = obj.Object.Selection.Blocks(0).DateBegin ' 只考虑了一个范围的情况
End Function
RegisterUserFunc "ActiveX","Xtreme_DataPicker_StartDate","Xtreme_DataPicker_StartDate"
' 获取结束日期
Function Xtreme_DataPicker_EndDate(obj)
Xtreme_DataPicker_EndDate = obj.Object.Selection.Blocks(0).DateEnd ' 只考虑了一个范围的情况
End Function
RegisterUserFunc "ActiveX","Xtreme_DataPicker_EndDate","Xtreme_DataPicker_EndDate"
' 选定一个指定的日期
Function Xtreme_DataPicker_SelectDate(obj,sDate)
' 选之前先把已经选择的日期清空
obj.Object.ClearSelection
obj.Object.Select sDate
End Function
RegisterUserFunc "ActiveX","Xtreme_DataPicker_SelectDate","Xtreme_DataPicker_SelectDate"
' 选定一个日期范围
Function Xtreme_DataPicker_SelectDateRange(obj,sStartDate,sEndDate)
obj.Object.SelectRange sStartDate,sEndDate
obj.Object.RedrawControl
End Function
RegisterUserFunc "ActiveX","Xtreme_DataPicker_SelectDateRange","Xtreme_DataPicker_SelectDateRange"
下面是一些使用的例子:
VbWindow("frmMain").Activate
Set oXtremeDataPicker = VbWindow("frmMain").ActiveX("Xtreme DatePicker Control")
Msgbox oXtremeDataPicker.Xtreme_DataPicker_GetSelectedDate
Msgbox oXtremeDataPicker.Xtreme_DataPicker_StartDate
Msgbox oXtremeDataPicker.Xtreme_DataPicker_EndDate
VbWindow("frmMain").Activate
oXtremeDataPicker.Xtreme_DataPicker_SelectDate "2010-01-07"
oXtremeDataPicker.Xtreme_DataPicker_SelectDate "2010-01-08"
VbWindow("frmMain").Activate
oXtremeDataPicker.Xtreme_DataPicker_SelectDateRange "2010-01-10","2010-01-12"
oXtremeDataPicker.Xtreme_DataPicker_SelectDateRange "2010-01-16","2010-01-19"
PropertyGrid
在QTP中录制PropertyGrid控件的操作得到如下脚本:
'' 录制的脚本
'VbWindow("frmMain").Activate
'VbWindow("frmMain").VbTreeView("wndTreeView").Select "Common Properties"
'VbWindow("frmMain").VbTreeView("wndTreeView").Select "Common Properties;General"
'VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").WinList("ListBox").Window("Window").Click 90,23
'VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").WinEdit("Edit").Set "abc"
'VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").WinEdit("Edit").Type micReturn
对PropertyGrid控件的常用操作封装如下所示的函数:
' 设置某项的值
Function Xtreme_PropertyGrid_SetValue( obj, sCaption, sValue )
Set oItem = obj.Object.FindItem(CStr(sCaption)) ' 查找
obj.Object.EditItem oItem,True ' 设置为编辑模式
oItem.Value = sValue ' 赋值
End Function
RegisterUserFunc "ActiveX", "Xtreme_PropertyGrid_SetValue" , "Xtreme_PropertyGrid_SetValue"
' 获取某项的值
Function Xtreme_PropertyGrid_GetValue( obj, sCaption )
Set oItem = obj.Object.FindItem(CStr(sCaption)) ' 查找
Xtreme_PropertyGrid_GetValue =oItem.Value
End Function
RegisterUserFunc "ActiveX", "Xtreme_PropertyGrid_GetValue" , "Xtreme_PropertyGrid_GetValue"
以下是使用例子:
VbWindow("frmMain").Activate
VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").Xtreme_PropertyGrid_SetValue "Type of Output" , "Console Application"
VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").Xtreme_PropertyGrid_SetValue "Startup Object" , "Program"
Msgbox VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").Xtreme_PropertyGrid_GetValue("Type of Output")
Msgbox VbWindow("frmMain").ActiveX("Xtreme PropertyGrid Control").Xtreme_PropertyGrid_GetValue("Project File")
ReportControl
在函数库中添加如下函数,封装ReportControl控件的常用操作:
' 获取指定单元格的文本
Function Xtreme_ReportControl_GetTextByCell( obj, iRow, iColumn )
obj.Object.ExpandAll True
Xtreme_ReportControl_GetTextByCell = obj.Object.Rows.Row(iRow).Record.Item(iColumn).Caption
End Function
RegisterUserFunc "ActiveX","Xtreme_ReportControl_GetTextByCell","Xtreme_ReportControl_GetTextByCell"
'设置指定单元格的文本
Function Xtreme_ReportControl_SetTextByCell( obj, iRow, iColumn , sValue )
obj.Object.ExpandAll True
Set row = obj.Object.Rows.Row(iRow)
Set col = obj.Object.Columns.Column(iColumn)
obj.Object.EditItem row,col
obj.Type sValue
End Function
RegisterUserFunc "ActiveX","Xtreme_ReportControl_SetTextByCell","Xtreme_ReportControl_SetTextByCell"
下面是使用的例子:
VbWindow("frmMain").Activate
Print VbWindow("frmMain").ActiveX("Xtreme Report Control_2").Xtreme_ReportControl_GetTextByCell ( 0, 0 )
VbWindow("frmMain").ActiveX("Xtreme Report Control_2").Xtreme_ReportControl_SetTextByCell 0, 0 ,"abcdefg"
VbWindow("frmMain_2").Activate
Print VbWindow("frmMain_2").ActiveX("Xtreme Report Control").Xtreme_ReportControl_GetTextByCell ( 3, 4 )
ShortcutBar
'' 录制的脚本
'VbWindow("frmMain").Activate
'VbWindow("frmMain").ActiveX("Xtreme ShortcutBar Control").WinObject("XTPShortcutBar").Click 52,420
'VbWindow("frmMain").ActiveX("Xtreme ShortcutBar Control").VbTreeView("treeFavorites").Select "Inbox"
从录制的脚本来看,QTP对ShortcutBar控件的识别和操作依赖坐标,封装一个Xtreme_ShortcutBar_Select函数如下:
Function Xtreme_ShortcutBar_Select( obj, Caption )
For i=0 to obj.Object.ItemCount - 1
If obj.Object.Item(i).Caption = Caption Then
obj.Object.Item(i).Selected = true
Exit For
End If
Next
End Function
RegisterUserFunc "ActiveX","Xtreme_ShortcutBar_Select","Xtreme_ShortcutBar_Select"
使用的例子如下:
VbWindow("frmMain").Activate
VbWindow("frmMain").ActiveX("Xtreme ShortcutBar Control").Xtreme_ShortcutBar_Select "Notes"
VbWindow("frmMain").ActiveX("Xtreme ShortcutBar Control").Xtreme_ShortcutBar_Select "Mail"
TaskPanel
''' 录制的脚本
'VbWindow("frmMain").Activate
'VbWindow("frmMain").ActiveX("Xtreme Task Panel Control").WinObject("XTPTaskPanel").Click 84,306
'VbWindow("frmMain").ActiveX("Xtreme Task Panel Control").WinObject("XTPTaskPanel").Click 71,76
'VbWindow("frmMain").Dialog("ToolBox").WinButton("确定").Click
封装函数如下:
Function Xtreme_TaskPanel_ClickItem(obj, GroupCaption, ItemCaption)
' 先把所有项设置为为选择状态
For a=1 to obj.Object.Groups.Count
For b = 1 to obj.Object.Groups.Item(a).Items.Count
obj.Object.Groups.Item(a).Items.Item(b).Selected = false
obj.Type micUp ' 通过键盘(向上键)移到最顶端
Next
Next
For i=1 to obj.Object.Groups.Count
If obj.Object.Groups.Item(i).Caption = GroupCaption Then ' 查找指定的Group
obj.Object.Groups.Item(i).Expanded = true
For j=1 to obj.Object.Groups.Item(i).Items.Count
If obj.Object.Groups.Item(i).Items.Item(j).Caption = ItemCaption Then ' 查找指定的Item
obj.Object.Groups.Item(i).Items.Item(j).EnsureVisible()
For n=1 to i+j-1
obj.Type micDwn ' 通过键盘(向下键)移到指定的Item
Next
obj.Type micReturn ' 通过键盘(回车键)模拟单击选择Item
Exit For
End If
Next
Exit For
End If
Next
End Function
RegisterUserFunc "ActiveX","Xtreme_TaskPanel_ClickItem","Xtreme_TaskPanel_ClickItem"
使用例子如下:
VbWindow("frmMain").Activate
VbWindow("frmMain").ActiveX("Xtreme Task Panel Control").Xtreme_TaskPanel_ClickItem "Data", "DataSet"
VbWindow("frmMain").Dialog("ToolBox").Activate
VbWindow("frmMain").Dialog("ToolBox").WinButton("确定").Click
VbWindow("frmMain").ActiveX("Xtreme Task Panel Control").Xtreme_TaskPanel_ClickItem "Components", "Timer"
VbWindow("frmMain").Dialog("ToolBox").Activate
VbWindow("frmMain").Dialog("ToolBox").WinButton("确定").Click
注:QtestWare已添加对CodeJock Xtreme Suite VB类库的支持
http://blog.csdn.net/Testing_is_believing/archive/2010/01/03/5125592.aspx
这篇关于QTP测试CodeJock Xtreme Suite控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!