简体   繁体   中英

Printing VBScript variables in QTP UFT

How can I check values of my variables at run time when using QTP UFT? I simply want to create a variable, do logic and fill it with data, set a breakpoint in the line following the variable and then check or output its value somewhere.

I have tried:

print variableName
WScript.Echo variableName

The first produces error: Print function type mismatch
The second produces error: Object required: "WScript"

I'm not sure where the problem lies as I've just started to get into both UFT and VBScript (mostly did C# and javascript and everything is quite different here). Could someone tell me the correct solution and perhaps also explain these errors to me?

If you wanted to see all variables use the debug viewer in QTP.

View -> Debug Viewer

There you can list all the variables you want to watch. You should be able to see them in break points.

Note : Ensure you have Windows script debugger installed to use the debug viewer.

You can also add the variable to watch .. Insert a breakpoint>> start the script then right click on the variable under test and add it to watch(Add to Watch) . After Adding you will see a watch window and the value of the variable will be displayed.

I obsess over my variables... so much so that I sprinkly my code with print statements... Actually, I abstract print into my own UDF (so that I can optionally add logging to a file if needed)...

Here's my function: (it's actually a sub because it doesn't return anything)

Sub say(textToSay)
    If talkative Then 'note that if I set global var "talkative" to false, then the whole log is disabled
            print textToSay
    End If
End Sub

Then, ALL my code typically looks like this...

say "click submit button"
Broswer("WebSite").Page("Search").WebButton("Submit").click

say "check for result"
if not Browser("WebSite").Page("Results").Exist then
  say "results page didn't appear after exist timeout"
  failtest
else
  say "successfully found results page"
end if

Honestly, I'm shocked that your "print variableName" statement gave an error, print is actually available in the VBScript api, so it should have worked.

All of my output goes to the Output pane in UFT. I would give my right-arm to find a way to programmatically clear that output pane between runs, but noone seems to know how to do it.

The benefit of all this logging is that I can watch my code run and see EVERY branch taken by the code, and I can add statements to print my variables.

Here's an example that shows how I would answer your question:

result = browser("WebSite").Page("Results").WebElement("Result").GetROProperty("innertext")

say "result:" & result
if result = "Approved" then
  Reporter.ReportEvent micPass, "Check for approved", "Approved!"
else
  Reporter.ReportEvent micFail, "Check for approved", "NOT Approved!"
End If

Notice the say statement in there - I'll be able to see that immediately during code execution without waiting until the results are shown at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM