QTP測試CodeJock Xtreme Suite控制元件

TIB發表於2010-03-13

CodeJock Xtreme SuiteVB程式設計中經常使用的類庫,但是在使用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"

 

 

從錄製的指令碼來看,QTPShortcutBar控制元件的識別和操作依賴座標,封裝一個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 ' 通過鍵盤(Enter鍵)模擬單擊選擇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

相關文章