QTP第三方偵錯程式PowerDebug試用手記

TIB發表於2010-04-19

今天試用了一下PowerDebug的Beta版本,下面介紹一下PowerDebug的主要功能。

 

1、寫Log功能

PowerDebug的輸出視窗寫Log

例如:

        'Clear the error

        PowerDebug.ClearLog()

        'Don't report Time to the output window

        PowerDebug.ReportTimeInLog = False

        PowerDebug.Log("This is a text Log without Time")

 

        'Report Time to the output window

        PowerDebug.ReportTimeInLog = True

        PowerDebug.Log("This is a text Log with Time")

 

        'Save the log to a file

        PowerDebug.SaveLog("C:/Log.txt")

 

        Dim varLog

        'Returns the current text from the Log

        varLog = PowerDebug.GetLog()

 

2、使用GoTo語句

PowerDebug allow you to use Goto Statements. The Goto method takes input the tag name. The tag need to be present with the Prefix and the Postfix. Consider the below code

(需要用字首和字尾指定GoTo跳轉到的標籤位置)

例如:

PowerDebug.GotoPrefix = "':"

PowerDebug.GotoPostfix = ":"

 

PowerDebug.Goto("JumpLocation")

MsgBox("This code should not be executed")

':JumpLocation:

MsgBox("Here after a jump")

 

'Goto statements can also be used to create a continue statement in a loop

For i = 0 To 10

    If i > 5 And i < 8 Then

        PowerDebug.Goto("ForContinue")

    End If

    PowerDebug.Log("Printing Loop counter - " & i)

    ':ForContinue:

Next

 

 

3、檢視當前程式碼執行環境

PowerDebug allows you to access current scope information which can be very useful for debugging information. The code sample below shows different information that can be accessed

 

例如:

Function CallMe()

    'Load the current Scope information

    PowerDebug.LoadInformation()

 

    PowerDebug.Log("Currently inside the function - " & PowerDebug.FunctionName)

    PowerDebug.Log("The function was called by - " & PowerDebug.Caller)

    PowerDebug.Log("The current code is: " & vbCrLf & vbCrLf & PowerDebug.CurrentCode)

    PowerDebug.Log("The current stack trace is: " & vbCrLf & vbCrLf & PowerDebug.StackTrace)

End Function

 

Function IamCallingCaller()

    Call CallMe()

End Function

 

Call IamCallingCaller

 

上面的指令碼將輸出以下資訊:

[19-ËÄÔÂ-10|09:09:11] Currently inside the function - CallMe

[19-ËÄÔÂ-10|09:09:11] The function was called by - IamCallingCaller

[19-ËÄÔÂ-10|09:09:11] The current code is:

 

Function CallMe()

    'Load the current Scope information

    PowerDebug.LoadInformation()

 

 

    PowerDebug.Log("Currently inside the function - " & PowerDebug.FunctionName)

    PowerDebug.Log("The function was called by - " & PowerDebug.Caller)

    PowerDebug.Log("The current code is: " & vbCrLf & vbCrLf & PowerDebug.CurrentCode)

    PowerDebug.Log("The current stack trace is: " & vbCrLf & vbCrLf & PowerDebug.StackTrace)

End Function

 

 

Function IamCallingCaller()

    Call CallMe()

End Function

 

 

Call IamCallingCaller

 

[19-ËÄÔÂ-10|09:09:11] The current stack trace is:

 

CallMe!Line (6):PowerDebug.Log("Currently inside the function - " & PowerDebug.FunctionName)

IamCallingCaller!Line (14):Call CallMe()

VBScript global code!Line (18):Call IamCallingCaller

VBScript global code!Line (1):RunAction "Action1", oneIteration

 

 

4、用Assert語句實現斷點

PowerDebug doesn't support QTP's breakpoint. So to pause execution or simulate a breakpoint one needs to use Assert method. Assert method when passed a False value pauses the execution

 

例如:

'Break execution on next statement

PowerDebug.Assert False

 

Print "The execution should be paused here"

 

 

 

5PowerDebugCommand視窗、Watch視窗、Variable視窗、Code視窗、Output視窗、CallStack視窗大大增強了QTP的除錯能力和易用性。

 

 

6WaitForAllObjectExist方法

PowerDebug提供的WaitForAllObjectExist方法可以用於判斷多個物件是否存在,而僅僅用一個語句:

bool WaitForAllObjectExist(int timeoutInSeconds, object obj1, [object obj2]....)

 

Returns true if all the passed objects exist within specified time, else returns false

 

例如:

Set obj1 = Window("regexpwndtitle:=File1.*")

Set obj2 = Window("regexpwndtitle:=File2.*")

 

'Wait for 10 seconds max for all objects to exist

Msgbox PowerDebug.WaitForAllObjectExist(10, obj1, obj2)

 

還有一個類似的方法是WaitForAnyObjectExist,用於判斷指定的若干個物件中是否有任意一個是存在的。關於該方法的使用可以參考PowerDebug的幫助文件,也可以參考作者主頁上的文章:

http://knowledgeinbox.com/products/powerdebug/enhancing-scripts-perfomance-using-waitforanyobjectexist/

 

 

 

 

 

 

試用PowerDebugBeta版本發現還不太穩定,有時候會停止響應。

 

 

相關文章