Changing variable’s value in Debug Watch Pane

January 14, 2009 1:43 PM

I have been using QTP from over 4 years now, but only recently i discovered that we can change variables in the Watch Pane of the Debug window. We can also use the Command pane to change the variable value but i found Watch pane to be a more convenient one.

We will see with an example how and when to do this. Consider the code shown below

Visual Basic

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

DimstrText

strText="Tarun Lalwani"

CallValidateLastName(Mid(strText,InStr(strText," ")))

FunctionValidateLastName(LastName)

IfLastName<>"Lalwani"Then

Reporter.ReportEvent micFail,"Last name Validation"," Last Name validation failed"

Else

CallValidLastName()

EndIf

EndFunction

FunctionValidLastName()

Reporter.ReportEvent micPass,"Last name Validation"," Last Name validation Passed"

Msgbox"Passed"

EndFunction

Now when the above code is run, the test result shows a failure. So now it comes to out debugging skills to find the issue. So we will put a breakpoint on the first line of the ValidateLastName function and run the code. When the code breaks on the first Line we need to add a Watch on the LastName variable. This can be done in two ways, one is to type LastName manually in the Watch window and the second way is to select LastName variable in code window and press CTRL + T key to add it to Watch window. The watch window will look like below

Debug Watch Pane - LastName

Now watching the LastName variable value we will observe that it has a extra space in the start ” Lalwani” and the reason for failure of the code. So we need to re look at our Mid statement that we used. Now there are 2 usual actions that one would do next

Stop the script, look at the Mid function call and correct it

Run the script and let it fail

The script i took example for was pretty small script and it does not make a difference even if we stop and re-run the script. But in cases where we have reached to this point after some good amount of execution, we should be looking at saving the time and continuing the script for further testing. What we want in such a situation is to be able to let the code go inside the “If” statement to test the correct code path. This would only happen when the variable LastName has value as “Lalwani” and currently it is ” Lalwani”. So to change the value we double click on the value displayed in the Watch pane and remove the space from the starting. Once change we will notice that now the LastName variable has a value “Lalwani” and if we run the code now it will test the pass condition. Using such techniques during debugging helps avoid time wasted due to re-run of scripts.

Debug Wath Pane - Changing variable value

Have questions related to this article? Want to request a new article? Use our forums to post your questions