最近自動化測試工具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