我叫陈永达,欢迎来到我的51testing空间! 才开始使用,想把关于软件测试的一些资料和心得保存于此。 欢迎大家多来参观,多提宝贵意见!

【QTP】【头脑风暴】如何在脚本运行时,Log中能记录当前进入的函数

上一篇 / 下一篇  2013-01-25 17:02:00 / 个人分类:QTP

在坛子里发了个帖子:
【头脑风暴】如何在脚本运行时,Log中能记录当前进入的函数

好吧,那我来抛砖引玉一下~比如:我在外部vbs文件中写入一段脚本,是个log工具的class:

Set Lg   = new LogTools      '实例化 LogTools 

Class LogTools
    Dim LogFileName        'log文件名
    Dim LogFilePath           'log文件路径
    Dim isPtLog                 '是否打印日志
    Dim NowFunctionDir    '当前Function的路径
    
    Sub Class_Initialize
        Call NewLogFolder 
        Call NewLogFile
        Depth = 0
    End Sub
 
    Sub Class_Terminate
        
    End Sub
    
    '------------------------------------------------------------------------------
    '返值:log文件名
    Property Get getLogFileName()  
        '处理 LogFileName
        If IsEmpty(LogFileName) Then
            LogFileName = "log" & getUniqueNumber & ".txt"
        End If
        
         getLogFileName = LogFileName
    End Property
    
    '------------------------------------------------------------------------------
    '返值:log文件路径
    Property Get getLogFilePath() 
        '处理 LogFilePath
        If IsEmpty(LogFilePath) Then LogFilePath = Environment("TestDir") & "\LOG\" & getLogFileName
        
        getLogFilePath = LogFilePath
    End Property
    
    '------------------------------------------------------------------------------
    '返值:当前Function的路径
    Property Get getNowFunctionDir() 
        getNowFunctionDir = NowFunctionDir
    End Property
    
    '------------------------------------------------------------------------------
    '属性:是否打印 log
    Property Let isPrintLog(ByVal oValue)
         isPtLog = oValue
         isPrintLog = isPtLog
    End Property 
    Property Get isPrintLog()
        If IsEmpty(isPtLog) Then
            '给默认值
            isPtLog = True
        End If
        isPrintLog = isPtLog
    End Property
    
    '------------------------------------------------------------------------------
    '作用:新建Log文件夹
    Sub NewLogFolder()
        Dim FSO , FolderDir
        Set FSO = CreateObject("Scripting.FileSystemObject")
        FolderDir = Environment("TestDir") & "\LOG"
        If Not(FSO.FolderExists(FolderDir)) Then
            FSO.CreateFolder(FolderDir)
        End If
        Set FSO = Nothing
    End Sub
    
    '------------------------------------------------------------------------------
    '作用:新建Log文件
    Sub NewLogFile()
        '新建文件
        Dim FSO
        Const ForReading=1,ForWriting=2,ForAppending=8      '参数赋值(1:只读,2:只写,3:追加)
        Set FSO = CreateObject("Scripting.FileSystemObject")    '创建一个文本对象
        Set LOGFILE = FSO.OpenTextFile(getLogFilePath,8,true)
        wait 3
        Set LOGFILE = Nothing
        Set FSO = Nothing
    End Sub

   '------------------------------------------------------------------------------
    '作用:重写 Log
    '参数:oWords:需要打印的内容
    Sub Log(oWords)
        '是否打印的判断
        If isPrintLog = False Then Exit Sub
        
        '处理打印内容
        oWords = "[" & now & "] [" & NowFunctionDir & "] " & oWords
        
        'Log 写入QTP
        print oWords
        
        'LOG 写入外部文件
        Dim FSO
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set ologFile = FSO.OpenTextFile(getLogFilePath, 8true)
        ologFile.WriteLine (CStr(oWords))
        ologFile.Close
        Set ologFile = Nothing
        Set FSO = Nothing
    End Sub


   '------------------------------------------------------------------------------
    '作用:进入 Function 时的标记
    '参数:oFunctionName:Function名
    Sub GoIn(oFunctionName)
        NowFunctionDir = NowFunctionDir & ">>" & oFunctionName
        Log "-------------------进入<" & oFunctionName & ">-------------------"
    End Sub
    
    
    '------------------------------------------------------------------------------
    '作用:出 Function 时的标记
    Sub GoOut()
        Dim arrFunDir
        arrFunDir = split(NowFunctionDir , ">>" , -1 , 1)
        Log "-------------------退出<" & arrFunDir(ubound(arrFunDir)) & ">-------------------"
        
        NowFunctionDir = ""
        Dim i
        For i = 1 To ubound(arrFunDir) - 1
            NowFunctionDir = NowFunctionDir & ">>" & arrFunDir(i)
        Next
    End Sub
    
    
    '------------------------------------------------------------------------------
    '作用:得到一个唯一的值
    Function getUniqueNumber()  
        Dim yyyy,mm,dd,ss
        Dim nowtime
        nowDate = date
        nowTimer = cstr(int(timer))
        yyyy = CStr(Year(nowDate))
        mm = CStr(String(2 - Len(Month(nowDate)) , "0") & Month(nowDate))
        dd = CStr(String(2 - Len(Day(nowDate)) , "0") & Day(nowDate))
        ss = CStr(String(5 - Len(nowTimer) , "0") & nowTimer)
        GetUniqueNumber = yyyy & mm & dd & ss
    End Function
    
End Class

然后,在Action中随便写一点调用函数的测试脚本:

 lg.Log "开始示范"
Call Main


Public Sub Main()
    lg.GoIn "Main"
    '----------------------------------------------------------
    lg.Log "我在Main函数里111"
    Call test1()
    lg.Log "我在Main函数里222"
    lg.isPrintLog = False     '关闭LOG打印
    lg.Log "这个不会出现的"
    '----------------------------------------------------------
    lg.GoOut
End Sub

Function test1()
    lg.GoIn "test1"
    '----------------------------------------------------------
    lg.Log "我在test1函数里111"
    Call test2()
    lg.Log "我在test1函数里222"
    '----------------------------------------------------------
    lg.GoOut
End Function

Function test2()
    lg.GoIn "test2"
    '----------------------------------------------------------
    lg.Log "我在test2函数里"
    '----------------------------------------------------------
    lg.GoOut
End Function

运行后,打印的结果是:
[2013/1/25 16:52:55] [] 开始示范
[2013/1/25 16:52:55] [>>Main] -------------------进入<Main>-------------------
[2013/1/25 16:52:55] [>>Main] 我在Main函数里111
[2013/1/25 16:52:55] [>>Main>>test1] -------------------进入<test1>-------------------
[2013/1/25 16:52:55] [>>Main>>test1] 我在test1函数里111
[2013/1/25 16:52:55] [>>Main>>test1>>test2] -------------------进入<test2>-------------------
[2013/1/25 16:52:55] [>>Main>>test1>>test2] 我在test2函数里
[2013/1/25 16:52:55] [>>Main>>test1>>test2] -------------------退出<test2>-------------------
[2013/1/25 16:52:56] [>>Main>>test1] 我在test1函数里222
[2013/1/25 16:52:56] [>>Main>>test1] -------------------退出<test1>-------------------
[2013/1/25 16:52:56] [>>Main] 我在Main函数里222

当然在这个脚本的目录下的LOG文件夹内,也会有个这样内容的文本文件。
但这段脚本有个小毛病,如果我用Exit Function写在中间,就必须在Exit Function前写上一句lg.Log,不然[]中的记录会错位。
请各位如果有什么想说的经过留言或到上面的地址里发帖。
畅所欲言~






TAG: 软件测试 Log log LOG QTP 陈永达

 

评分:0

我来说两句

Open Toolbar