Printing VBScript variables in QTP UFT - vbscript

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.

Related

How to display debug info or console.log equivalent in Lua

I am creating many games using Lua and LOVE2D, but whenever I implement a new function and want to test it out, or simply want to know a value of a variable in Lua, I either display it on the game screen or just hope that it works.
Now my question is...
IS THERE A WAY TO DISPLAY SOME INFO, such as A VARIABLE VALUE or something else into the terminal or somewhere else? Just like console.log in javascript which displays some content in the javascript console in the browser. So, is there a way to do this is Lua?? using LOVE2D?
I am using a Mac, so I have a terminal and not a command prompt. Is there a way to display some content there? Anywhere else would also be fine, I just need to see if those values are as expected or not.
Use a conf.lua file to enable the console, then you should be able to use a standard print(). You can read the wiki entry here.
Note: You have to run Lua and Love2D via the terminal for this to work. Running Lua and Love2D like this is required for the print statements to show:
/Applications/love.app/Contents/MacOS/love "/Users/myuser/Desktop/love2d-test-proj"
You just need to add a conf.lua file to the same location where your main.lua. Your file may be as simple as this:
function love.conf(t)
t.console = true
end
But feel free to copy the whole configuration file from the above link and edit what you need.
I can't be completely sure about this, because I have no access to Mac, but the console is disabled by default and even on Windows, no prints are shown until you turn it on.
Alternatively You can also display debug info in the game itself like some games do.
What I like to do is add something like debugVariable = {} for logging events that happen in each loop and debugPermanent = {} for events that happen rarely. Possibly add convenience functions for writing to the variables:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
Now a function to draw our debug info:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
And we just call this draw function at the end of our love.draw() and the texts should appear.
Obviously, this method can be refined further and further almost infinitely, displaying specific variables, and adding graphs for some other variables to clarify the information you want to show, but that's kind of outside of the scope of the question.
Lastly Feel free to check here for debug libraries submitted by users.

HP UFT (Unified Functional Testing) : Results View : Is there a way to capture the row, line number of the script where it failed?

I usually report the events when it passes or fails like:
Reporter.ReportEvent micPass," Title of the passed Event", " Description of Passed Event"
And
Reporter.ReportEvent micFail," Title of the failed Event", " Description of Failed Event"
Is it a good idea to hard code the line number in the title or description of the event like ?
Reporter.ReportEvent micFail," Test Failed at Line 432 ", " Please check line 432 "
To answer the 2nd part of your question, hard-coding will not be a good idea. If you hard-code the line number and in the future, a few lines get added to the code above that line number, the whole point hard-coding goes in vain as the report will now contain the incorrect-line number(which was hard-coded).
And for the first part, I can understand why you want the line-number to be reported. But I don't think there is any "predefined" way of doing it.
But, you can make your own function to get the line number. Here's an idea that you can implement:
Go to the location where your QTP test is stored. Get the full path of the file script.mts(you can find it inside the Action1 Folder). Also, this path can be constructed dynamically if you have good framework and proper folder structure.
After getting the path, you can open that file(script.mts) as a text stream(using the filesystemobject) in Read only mode.
Now you can traverse through the file line-by-line searching for the text "Reporter.ReportEvent micPass," Title of the passed Event", " Description of Passed Event"". After you have found it, you can easily get the line number and return that from your function to store it in some variable which you can use while reporting.
I have not tried this method myself but would have surely given it a try, If I really needed it.
Hardcoding is not a good idea from an end user perspective. In case it's a regression suite it could be used by a tester who doesn't know how to debug the code or analyze. To get the root cause of the failure through reporting (micPass/micFail) you can further refine your scripts

QTP - Object doesn't support this property or method error

I am not sure what's wrong with the below code.
Set obj=description.Create()
obj("micClass").Value="Link"
obj("name").Value="Advertising Programs"
Set totalnobuttons=Browser("title:=.*").Page("title:=.*").ChildObjects(obj)
totalnobuttons.highlight
print totalnobuttons.count
For i=0 to totalnobuttons.count-1
print totalnobuttons(i).GetRoProperty("name")
Next
This gives an error "Object doesn't support this property or method error" during execution. I need to highlight the "Advertising Programs" program link using the above code.
Your line:
totalnobuttons.highlight
is a culprit. You are trying to highlight the whole collection of Link objects. You cannot do that. Instead, remove that line and put that in your For...Loop like this:
For i = 0 to totalnobuttons.count-1
totalnobuttons(i).HighLight
print totalnobuttons(i).GetRoProperty("name")
Next
You are trying to find collection object. Return type should be always array of objects.
Try to use advanced for loop to proceed. Find the code below:
For each button in totalnobuttons
button.HighLight
print button.GetRoProperty("name")
Next

Do loop won't exit after conditions are met?

I've been trying to make a loop that repeats itself until an does not element exists on the page. However, when the condition is met, the loop does not exit. Any suggestions?
Dim userExt
userExt = 1
Do until Not Browser("Some_browser").Page("Some_page").WebElement("This username is already").exist
Browser("Some_browser").Page("Some_page").WebEdit("WebEdit").Set "registertester" & userExt+1
userExt = userExt+1
Browser("Some_browser").Page("Some_page").WebElement("Save & Continue").Click
Loop
I also tried changing the first line to
Do until Browser("Some_browser").Page("Some_page").WebElement("This username is already").exist=False
I tried adding an if exist then exit loop, which didn't work. It just kept looping. I know for sure the element does not exist on the page.
The element did not exist in the source code but for some reason UFT found it. I found that out by going on the page and click the "Highlight in Application" button in UFT. Although nothing was highlighted, UFT did not generate any error. I went on to check the properties of the element and found a property called visible.
I changed the Do Until statement to:
Do until Browser("Some_browser").Page("Some_page").WebElement("This username is already").GetROProperty("visible")="False"

Cannot convert Group Description to String Variable

I have the following code:
Set objHoldGroup = GetObject("LDAP://" & objGroup)
strGroupDesc = (objHoldGroup.Description)
WScript.Echo(strGroupDesc)
The variable strGroupDesc returns nothing when echoed. I can output the description directly but I need it for further processing. Thoughts?
Explanation: Apparently your script sets Option Explicit (good), which you didn't tell us about (bad). This option makes defining variables before you can use them mandatory (good). Normally that would raise an "undefined variable" error, though. Since that doesn't seem to happen with your code, you seem to also have an On Error Resume Next somewhere in your code (very bad), which, again, you chose to keep quiet about (bad).
Next time please don't omit parts of your code that are vital for troubleshooting the problem. And don't use On Error Resume Next.
Issue found: I forgot to Dim strGroupDesc.

Resources