多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

QTP中几个交换值的方法-自动化测试

更新时间:2022-05-20 09:24:29 作者:多测师 浏览:115

  最近自动化测试工具QTP做挺多,有些用到的方法拿出来一点点共享,也算自己沉淀一下。

QTP中几个交换值的方法-自动化测试

  首先一个,测试中可能需要将某些数据存放到全局空间中,这个全局空间我也说不好,就是能让多个Action都取到的地方吧。我目前了解到的:

  1)可以存在Action内定义的变量中,作为参数传给下一个调用的Action。

  比如:Action1定义两个参数(右击Action,选择Action Properties,在Parameters选项卡中设置。)

  调用时在RunAction最后部分把要传的变量写进去就行。

  在Action中用Parameter(“参数名”)就可以取出来了。

  2)存储在自定义对象或变量中,由QTP的Environment进行引用。

  这里找个我以前参考AdvanceQtp中文档自己写的类及实现。

  Class OurExcel

  Private bAleadyInit

  '********************sub Class_Initialize begin**********************

  Private Sub Class_Initialize

  'check whether has a environment variable named Reference_counter.

  'this variable is for storing the number of current reference to "One" object.

  On Error Resume Next

  bAlreadyInit = IsObject(Environment("Reference_counter"))

  If Err.Number <> 0 Then Environment("Reference_counter")=0

  On Error Goto 0

  'When "new" operation happen, add the counter.

  Environment("Reference_counter") = Environment("Reference_counter") + 1

  'Using the feature of QTP, storing the "One" object into environment...

  'The environment name is "Excel_Object".

  'a. check whether the variable exist.

  On Error Resume Next

  bAlreadyInit = IsObject(Environment("Excel_Object"))

  If Err.Number <> 0 Then bAlreadyInit = False 'Environment isn’t even initialized

  On Error Goto 0

  'b. check whether has it's contents.

  If bAlreadyInit = True Then

  If Environment("Excel_Object") is Nothing Then bAlreadyInit = False

  End If

  'c. If no object found, create.

  If bAlreadyInit = False Then

  'Msgbox "Constrction object."

  Environment("Excel_Object") = CreateObject("Excel.Application")

  End If

  End Sub

  '********************sub Class_Initialize End**********************

  '*******************sub Class_Terminate Begin********

  Private Sub Class_Terminate

  'Msgbox "enter terminate"

  Environment("Reference_counter") = Environment("Reference_counter") - 1

  'If no more reference exist, close the excel and terminate the environment variable.

  If Environment("Reference_counter") = 0 Then

  msgbox "Closing excel process........."

  Environment("Excel_Object").Quit

  Environment("Excel_Object") = Nothing

  Environment("Reference_counter") = Nothing

  End If

  End Sub

  '*******************sub Class_Terminate End***********

  'the function only for providing a demo....

  Public Function GetSheetData(sFileName, SheetNumber, strRangeBegin, strRangeEnd)

  With Environment("Excel_Object")

  .Visible = False

  Set bjWorkbook = .Workbooks.Open(sFileName) '"C:/temp/Branches.xls"

  Set bjWorkSheet = objWorkbook.WorkSheets(SheetNumber)

  Reporter.ReportEvent micDone, "Current range:"&strCurrentQueryRangeBegin, _

  objWorkSheet.Range(strRangeBegin&":"&strRangeEnd).value

  Set bjWorkbook = Nothing

  Set bjWorkSheet = Nothing

  End with

  End Function

  End Class

  Set oExcelInstace1 = New OurExcel

  oExcelInstace1.GetSheetData "C:/temp/Branches.xls", 1, "A200", "A200"

  oExcelInstace1.GetSheetData "C:/temp/Branches.xls", 1, "A201", "A201"

  Set oExcelInstace2 = New OurExcel

  oExcelInstace2.GetSheetData "C:/temp/Branches.xls", 1, "A202", "A202"

  oExcelInstace2.GetSheetData "C:/temp/Branches.xls", 1, "A203", "A203"

  'with class's Class_Terminate method, you can call it using the following style....

  'Notice:This behave will call destructor of your class.............

  'If has reference exist, then you will find one excel process in your machine.

  Set oExcelInstace1 = Nothing

  Set oExcelInstace2 = Nothing

  里面有两个Environment变量,一个Excel_Object,一个Reference_counter。我想尽力把这东西做的象COM,当然有很大很大差距。当初就是为了找个全局的地方放数据而已。

  3)存储在QTP的GlobalTable中。

  涉及2个函数。一个是写入到GlobalTalbe,另一个是从其读入。

  Function setValue(strColName, strColValue)

  On Error Resume Next

  DataTable.Value(strColName, dtGlobalSheet) = strColValue

  If Err.Number <> 0 Then DataTable.GlobalSheet.AddParameter strColName, strColValue

  setValue = strColValue

  End Function

  Function getValue(strColName)

  Dim outputTemp

  On Error Resume Next

  outputTemp = DataTable.Value(strColName, dtGlobalSheet)

  If Err.Number <> 0 Then

  getValueFromGS = ""

  Exit Function

  Else

  getValue = outputTemp

  End If

  End Function

  一般这个方法的实现,可能还要遍历列名来判断是否存在,这样如果变量很多肯定效率低。QTP对于DataTable取数的实现肯定也进行了遍历或Find这样的方法,所以任务尽量让QTP来解决比较好,比如随机数干脆就直接用RandomNumber对象而取代 vbscript的Randomize加Rnd。

  以上内容为大家介绍了自动化测试中的QTP中几个交换值的方法,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部