UFT(QTP)中自動化測試腳本開發時,有時候需要記錄一些信息到文本文件中,以下是一個SUB過程的示例,涉及的技術點:
1. 獲取自動化測試腳本的當前路徑
2. 字符串組織
3. FSO對象
4. 日期、時間格式規範
'============================================================
' 過 程 名 : TestResultLog
' 功能說明 : 將日誌信息輸入到txt日誌文件中
' (日誌路徑爲: "..\..\Test_ResultLog\TestResult_log.txt")
' 輸入參數 : judgment 是否登錄成功(BOOL)
' UserName 登錄的用戶名
'(調用示例: TestResultLog true,"aaaaaa")
' 返回值:無
'===========================================================
Sub TestResultLog(judgment, UserName)
Dim strSuccess,strFail,fso,myfile,strTime
Dim logpath
logpath=environment("TestDir")+"\..\..\Test_ResultLog\TestResult_log.txt"
strTime="[" & date & " " & mid(time,1,5) & "]"
strSuccess=strTime & " 賬戶“" & UserName & " 登錄成功"
strFail=strTime & " 賬戶“" & UserName & " 登錄失敗"
Set fso=CreateObject("Scripting.FileSystemObject")
'判斷文件是否存在,如果存在打開,否則創建
If (fso.FileExists(logpath)) Then
'定義常量intCode,作爲打開文件函數的參數,防止寫入數據是Unicode亂碼
Const intCode=-1
Set myfile=fso.OpenTextFile(logpath,8,true,intCode)
else
Set myfile=fso.CreateTextFile(logpath,2,true)
End If
If judgment Then
myfile.WriteLine strSuccess
else
myfile.WriteLine strFail
End If
myfile.Close
set myfile=nothing
set fso=nothing
End Sub