How to debug a script in QTP?

Its easy to debug a code which we have written, but what if the code is developed by someone else !!! Sometimes I feel that its easier for me to write a totally new code rather than spend the time trying to read someone else's code and understand it. However once we join a company and start working on huge amounts of scripts already developed and written, do we have that liberty to tell them that I will write my own code rather than read someone else's code !!!! No way !!! Its always a best practice to document your code very well. But dont ever go and document each and every line..that makes your code look so ugly. We must document the logical separations in the code. But a lot of people dont get into that habit of documentation. This makes the job of someone else reading the code even more difficult. I have frequently been using the below mentioned techniques to debug and it has proved very efficient (in a while I will tell which is the technique I like the most)

1. Giving MsgBox statements
2. Using Reporter.ReportEvent statements
3. Using Print statements
4. Using Run from Step
5. Using Watch
1. Giving MsgBox statements
For small pieces of code, its very useful to use MsgBox statements where you can popup a message box to display the value of the variable you want to print. It also helps to use message boxes to know whether we are actually entering a condition statement like If or While. Its a common thing while debugging to "think" that we entered an If statement or a While statement where in reality we would not have entered.
2. Using Reporter.ReportEvent statements
If our script is very large and we want to verify lots of variables or want to display certain useful information which helps in debugging, we can use the Reporter.ReportEvent statement. For informational purpose we will use Reporter.ReportEvent micDone, "Info","Entered loop". If its just an information, we will use micDone instead of micPass or micFail.
3. Using Print statements
QTP has a utility called Print. If we give Print "Message", a log window will popup during your execution but remember that this log window will not hinder your execution like a MsgBox. So if you give multiple print statements, you can see all the messages that you want to print in the log window without interrupting your execution.
4. Using Run from Step
Step by Step execution is a technique when we want to monitor very closely whats happening after each step. In this case, each step will execute only when we manually execute them. Lets say, we want to execute Line 10 in our script and want to know what happens after the execution of this line but dont want Line 11 to execute unless we instruct it to do so... Bring your cursor to Line 10 and click on it... Go to Debug->Run from step. Now you will see the yellow marker on the left side of your statement and QTP will wait for us to execute that statement. Pressing the F11 key will now execute each statement step by step. This way of debugging is very useful as we know exactly what happens after each statement and the execution speed is completely under our control.
5. Using Watch
Watch is a Debug feature which is very powerful and this is the one I prefer the most. This can effectively be used with the above mentioned step by step execution. Lets say while doing the step by step execution, we want to know the value of a particular variable. We can get this value easily by using Watch. Select the variable and press Ctrl+T or Debug->Add to Watch. Now you will see a Debug window below your script and the variable you wanted to "watch" would be added to the list. The value would be listed alongside as per the variable's current value in the execution. So we can keep "watch"ing different variables as we step.
Debugging a program is a skill. Knowing what to watch and how to debug takes some experience. Using the techniques as mentioned above will easily help to find what went wrong when the test didnt execute as expected.
Hope this added some new information to you.
I dont want to have any sort of copyright or copyleft in this site but if you are reusing this code in any other site, I would appreciate you to provide this page as a link so that others get a more descriptive explanation of this concept. Your suggestion and comments are always welcome. Cheers !!!
As always,
Your friend in need,

George, Reju

Read Users' Comments (4)

4 Response to "How to debug a script in QTP?"

  1. Bibek says:
    September 3, 2009 at 3:01 AM

    Hi George...

    It's great to reach such a wonderful site for learning QTP..i am very greatful to you..Thanx lot..George! i have had a one problem..would you please look after this:)

    How to verify whether the page is loaded properly or not..i have used the following script but it's not showing any result...

    Plz suggest if i have left any steps..
    I have also enclosed the scree shot of the page

    Thank you


    ********************************************


    Browser("micclass:=Browser").Page("micclass:=Page").Sync
    if Browser("micclass:=Browser").Page("micclass:=Page").WaitProperty("status text","done",5000) Then
    chk=Browser("micclass:=Browser").Page("micclass:=Page").getROProperty("status text")
    if chk="done" then
    Reporter.ReportEvent micpass,"Page status:"&chk,"Approved"
    else
    Reporter.ReportEvent micpass,"Page didn't load properly","NotApproved"
    End if
    End If

    ***************

    I have also tried Object.readyState ="complete" but couldn't accomplish..it is not supporting firefox browser

    ..need u r help

  2. REJU GEORGE says:
    September 14, 2009 at 8:45 PM

    Hi Bibek,
    I guess what you should be looking for is the Browser("micclass:=Browser").Object.StatusText property and not the readyState property.

    The second option could be to get the text from the WinStatusBar object

    Browser(("micclass:=Browser").WinStatusBar("miclass:=msctls_statusbar32").GetROProperty("text")

    Let me know if this works for you.

    Thanks,
    George, Reju

  3. REJU GEORGE says:
    September 15, 2009 at 4:48 AM

    Ooops...there is a typo... for the second alternative above it should be
    Browser("micclass:=Browser").WinStatusBar("micclass:=msctls_statusbar32").GetROProperty("text")

    Thanks,
    George, Reju

  4. REJU GEORGE says:
    September 15, 2009 at 9:02 AM

    Hi Bibek,
    I didnt have access to QTP when I first commented...it was just out of memory that I wrote... Here is what will work for you...I have tested it and it works fine.

    Browser("micclass:=Browser").WinStatusBar("nativeclass:=msctls_statusbar32").GetROProperty("text")

    AND

    Browser("micclass:=Browser").Object.StatusText

    Thanks,
    George, Reju

Post a Comment

Visitors

Website Counter