True / False as a number
Pass is 0
Fail is a non zero number such as -13412
What are true and false equivalent to as numbers?
How can I prove this to myself?
Play around with this Kevin:
If 1 Then
If Not 0 Then
If you really want some frustration, take a look at this:
You might think the result would be "It exists" when you're on the google home page... but you'd be wrong. VBscript treats any number other than 0 as true in a boolean comparison. However, if you convert True to an integer, it's -1. The Exist function returns 1 if the object you're looking for actually exists. So if you do the comparison above, it'll always fail because the function returns 1 and vbscript converts the True to -1.
' Browser("Google").Page("Google").WebEdit("q") is the google home page search box... add it to your repository
If Browser("Google").Page("Google").WebEdit("q").Exist(1) = True Then
msgbox "It exists"
msgbox "It does not exist."
1 <> -1
Which is why you really shouldn't think of true/false as a number, but if anything a range of numbers.
Think of Pass/Fail as a process level item. Any process, like a .bat file or a .exe will return an error code when it exits to the system. In this case, Pass is 0, meaning error code 0, as no errors. Otherwise it's any number, meaning an error code was returned.
For true/false in programming, you're looking at bits. 0 in this case is False, meaning 0 bits are turned on. Other wise any number is true, meaning there is a True bit somewhere in the bit string. To conceptualize the one above in this model, think of pass being 0, as being a "False" for "Was there an error?"
Mark, Dennis, NoUse4aName,David,
0 is false.
-1 or some other number (other than 0) is true.
May I understand it as this for QTP UFT VBscript?
A simple VBScript test to prove that 0 is equivalent to a False, and any other number is equivalent to True:
For i = -5 to 5 Step 1
msgbox i & " is " & cbool(i), vbOKOnly, "True/False Test"
You don't need QTP or UFT to run this since it's just VBScript. You can use wscript or in windows to run it from a .vbs file.
... just another Tester ...
I tried the code and understand it.
I will think of it as 0 is there are no light bulbs turned on. Anyother number sets a switch to turn a bulb on someplace.
Also cbool is something I have not needed in the past.
I learned GW BASIC 30 years back. It still trips me up at times.