最近自动化测试工具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,当然有很大很大差距。当初就是为了找个全局的地方放数据而已。