How to Debug custom functions in QTP

Debugging suggestions include:
1.
Use the suggestions above for debugging a script.
2. Verify the code works when it is not within a function. If the code does not work when it is not in a function in a QuickTest Professional (QTP) script or within a pure VBS script (while QuickTest Professional is not in use), it will not work when it is placed within a function. The following example shows the step by step process of debugging a simple function.

Example:

Function ex_func(obj, txt)
msgbxo obj.Exist
msgboxx txt
End Function

To debug the above function (which has intentional typos), copy the statements into a QuickTest Professional script:

msgbxo obj.Exist
msgboxx txt

If needed, modify any references to a generic object to an actual object:

msgbxo Browser("Browser").Exist
msgboxx txt

Execute the code; debug as needed:

msgbox Browser("Browser").Exist ' corrected typo: msgbxo -> msgbox
msgbox txt ' corrected typo: msgboxx –> msgbox

Copy the corrected code into the function. Verify it works as expected:

Function ex_func(obj, txt)
msgbox Browser("Browser").Exist
msgbox txt
End Function

If needed, modify references to specific objects back to general references:

Function ex_func(obj, txt)
msgbox obj.Exist
msgbox txt
End Function

Verify the function works as expected.
(Optional) Move the function into a function library.

3. If your function is returning a value, the value should be assigned to the function name. This is a VBScript requirement.

Example:
Function examplefunc
examplefunc = "returned value"
End Function
msgbox examplefunc

4. If your function is saved in an external function library, make sure you associate the function library with the test script.

by - Abhishek Nema - http://goo.gl/HUSSo

How to debug QuickTest Professional (QTP) Automation scripts

Debugging scripts :
Debugging suggestions include:
1. Use the Watch Expressions Tab in the Debug Viewer pane to display the values contained in variables
2. Step through the script one line at a time
3. Use the msgbox function to pause the test and display values contained in variables or retrieved from the application.

Example:
txt = Window("Notepad").WinEditor("Edit").GetROProperty("text")
msgbox txt
4. If the replay error is related to not being able to identify an object, determine which object is causing the problem (the error can be from a parent object). You can use the .Exist method with the msgbox function to do this.

Example:
msgbox Browser("Browser").Exist
msgbox Browser("Browser").Page("Page").Exist
msgbox Browser("Browser").Page("Page").WebCheckBox("WebCheckBox").Exist
Once you know which object in the hierarchy is causing the problem (the object that returns "false"), you can work to resolve the issue.
5. If an object class method (one of QTP’s built in methods) is failing, you can use the GetLastError and DescribeResult methods to get additional information on the error.

Note:

By default, QTP is set to pause the script during replay and pop up a message box with the error. This setting can be modified on the Run tab of the Test Settings dialog (Test -> Settings).

GetLastError
The GetLastError statement returns the error code of the most recent error. You can use this statement together with the DescribeResult Statement

DescribeResult(Error)
Error    An integer value representing an error   
Example:
on error resume next
' Make sure the Notepad window is not visible during replay (minimized)
Window("Notepad").WinEditor("Edit").Type "test"
x = GetLastError
msgbox( DescribeResult(x) )

Note:
If QTP is not set to pop up a message box on an error, the first line in the example is not needed. If an error message box appears, click <Skip> to continue with the next line in the script. The message box will appear with the results of the GetLastError statement.
6. Define specific sections of the script to run. Use breakpoints and the Run from Step replay option (Test -> Run from Step) to run your script in these sections. For additional information, refer to the following articles:
7. Check the syntax of the statements in your script. Make sure the VBScript code (in the Expert View) matches the expected format. Refer to the QuickTest Object Model Reference for the syntax of QTP methods and statements. For syntax information on VBScript functions, refer to the online VBScript Reference (both documents are accessed by selecting Help –> QTP Help).
8. If your script contains calls to external reusable actions, make sure the calls were inserted correctly.

Note:

If a replay error occurs in a reusable action, you will need to open the script containing the original action in order to debug it.
The above are just a few options that you can use to debug a test. The best process to follow for debugging is to isolate the problem and work on resolving it. Test your script in portions to narrow down the area that is causing the problem. Then use the above suggestions try to determine why the problem is occurring.

by - Abhishek Nema - http://goo.gl/HUSSo