QTP中对象库的提取

QTP开发中,为了实现自动化程序的框架化,尽可能将测试执行人员和开发人员的角色分离,让测试执行人员只关心外层的对象而忽略内部的处理。往往就需要把QTP录制的对象库分离成单独的脚本,执行人员选择相应的对象即可生成相应的对象脚本。

 

下文说明对对象库的分离处理方法

 

QTP的对象库为tsr文件,是一种2进制文件,不能被QTP直接用脚本获取其中的对象。如图是对象库结构树

QTP的对象管理器提供了把对象库导出为xml格式文件的选项

 

导出为xml格式后,就可以利用QTPXMLUtil对象对xml进行处理,进而获取相应的对象。 下面的函数为把对象从xml格式对象库中提取出来,并写入excel表格

 

Public Function GetResource(Byval XmlFile,byval ResourceFile)

       Dim XmlObj,i,j,curRow

       Dim ObjRoot,Children

       Dim childCnt,Parent

       Dim Layer,CurLayerIndex()

       Dim sourceID,ParentID,sourceClass,sourceName

      

       Dim ExcelApp,Exlsheet,ExcelWorkBook

       Set ExcelApp = CreateObject("Excel.Application")

       ExcelApp.Visible = True

       ExcelApp.DisplayAlerts = False

       Set ExcelWorkBook = ExcelApp.Workbooks.Open(ResourceFile)

       Set Exlsheet = ExcelApp.Sheets("对象库")

       Exlsheet.Activate

      

       Set XmlObj = XMLUtil.CreateXMLFromFile(XmlFile)

      

        '---当前行

       curRow = 1

       '----子对象个数

       childCnt = 1

      

       '---------获取根对象

       Set ObjRoot = XmlObj.GetRootElement().ChildElements().ItemByName("qtpRep:Objects")

       set  Children = ObjRoot.ChildElementsByPath( "./qtpRep:Object" )

      

       For i = 1 to Children.count()

              ' 第一层对象

              Layer =  0

              ReDim Preserve CurLayerIndex(Layer)

              CurLayerIndex(Layer )  = i

              j = 1

              Set Child = Children.item(i)         

              curRow = curRow + 1

              Parent = curRow - 1

              Set Children = Child.ChildElementsByPath( "./qtpRep:ChildObjects/qtpRep:Object" )

             

              sourceID =  curRow - 1

              Call GetAttrib(Child,sourceClass,sourceName)

              ParentID = 0

              Exlsheet.cells(curRow,1) = sourceID

              Exlsheet.cells(curRow,2) = sourceName

              Exlsheet.cells(curRow,3) = ParentID

              Exlsheet.cells(curRow,4) = sourceClass

              Exlsheet.cells(curRow,5) = "(" + chr(34) + sourceName + chr(34) + ")"

      

              If Children.count() >0  Then

                     Layer = Layer + 1

                     ReDim Preserve CurLayerIndex(Layer)

              End If

      

              While Children.count() > 0

                            If  Children.count() >= j  Then

                                   CurLayerIndex(Layer )  = j

                                   Set Child = Children.item(j)

                                  

                                   curRow = curRow + 1

                                   sourceID =  curRow-1

                                   Call GetAttrib(Child,sourceClass,sourceName)

                                   ParentID = Parent

                                   Exlsheet.cells(curRow,1) = sourceID

                                   Exlsheet.cells(curRow,2) = sourceName

                                   Exlsheet.cells(curRow,3) = ParentID

                                   Exlsheet.cells(curRow,4) = sourceClass

                                   Exlsheet.cells(curRow,5) = "(" + chr(34) + sourceName + chr(34) + ")"

                                  

                                   childCnt = Child.ChildElementsByPath( "./qtpRep:ChildObjects/qtpRep:Object" ).count()

             

                                   If  childCnt > 0 Then

                                          Layer = Layer + 1

                                          ReDim Preserve CurLayerIndex(Layer)

                                          Set Children = Child.ChildElementsByPath( "./qtpRep:ChildObjects/qtpRep:Object" )

                                          Parent = curRow - 1

                                          j = 1

                                          CurLayerIndex(Layer )  = j

                                   Else

                                          j = j + 1

                                   End If

                            Else

                                   temprow = CurRow

                                   parent = CInt(Exlsheet.cells(parent + 1,3))

                                  

                                   Layer = Layer - 1

                                   Set Child = Child.Parent.Parent

                                   Set Children = Child.Parent.Parent.ChildElementsByPath( "./qtpRep:ChildObjects/qtpRep:Object" )

                    

                                    j = CurLayerIndex(Layer) + 1

                            End If

              Wend

              Set Children = Child.Parent.ChildElementsByPath( "./qtpRep:Object" )

       Next

       ExcelWorkBook.save

       ExcelApp.Quit

       Set ExcelApp = Nothing

End Function

 

Public Function DataConvent(FileName)

       Dim ExcelApp

       Dim ExcelWorkBook

       Dim ObjectSheet

       Dim DataSheet

       Dim SheetCnt,ObjRowCnt,DataRowCnt

       Dim i,j,n

      

       Set ExcelApp = CreateObject("Excel.Application")

       ExcelApp.Visible = False

      

       Set ExcelWorkBook = ExcelApp.Workbooks.Open(FileName)

       SheetCnt = ExcelWorkBook.sheets.count

 

       Set ObjectSheet = ExcelApp.Sheets("对象库")

 

       ObjRowCnt = ObjectSheet.Range("A1").CurrentRegion.Rows.Count

       For i = 2 to ObjRowCnt

              If Trim(ObjectSheet.cells(i,3))<>"0" Then

                     For j = 2 to ObjRowCnt

                            If Trim(ObjectSheet.cells(j,1))= Trim(ObjectSheet.cells(i,3)) Then

                                   Dim tempvalue

                                   tempvalue = Replace(ObjectSheet.cells(j,5),"(" + Chr(34),"")

                                   tempvalue = Replace(tempvalue,Chr(34)+")","")

                                   ObjectSheet.cells(i,6) = ObjectSheet.cells(j,6) & "." & ObjectSheet.cells(i,4) & ObjectSheet.cells(i,5)

                                   ObjectSheet.cells(i,2) = tempvalue & "-" & ObjectSheet.cells(i,2)

                            End If

                     Next

              Else

                     ObjectSheet.cells(i,6)= ObjectSheet.cells(i,4) & ObjectSheet.cells(i,5)

              End If

       Next      

      

       ExcelWorkBook.save

       ExcelApp.Quit

       Set ExcelApp = Nothing

End Function

 

生成的excel格式如图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值