最近自动化测试工具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