It’s QUIZ time guys. This question comes from one of our fellow bloggers.
What is the difference between the two little snippets of codes given below?
If Browser("Browser").Page("Page").Link("Link").Exist(0)=True Then Reporter.ReportEvent micPass "Link Found", "YES" End If
AND
If Browser("Browser").Page("Page").Link("Link").Exist(0) Then Reporter.ReportEvent micPass "Link Found", "YES" End If
Can you find a problem with the 1st piece of code?
If yes,
- What is the problem?
- What is your solution to it?
Note: Please post your solution in the comments section below. I will publish the comments only after 24 hrs to give everyone a fair chance of attempting this quiz.
Update: Comments section is now open and answers from our blog readers can be seen below. Here is my explanation on differences between boolean, string and numeric comparison. This question was first asked by MaryAnne.
Equals to operator (=) compares string or numeric expressions. What is returned by
Browser("Browser").Page("Page").Link("Link).Exist(0)
is boolean, as can be checked from
msgbox Typename(Browser("Browser").Page("Page").Link("Link).Exist(0))
When a boolean expression (on the LHS) see an equal to operator, it must be implicitly getting changed into string or numeric expression depending on whether string or numeral is present on the other side(RHS).
So in 1st case both of these should work…
String comparison (String – “True” – is present. Note the quotes):
If Browser("Browser").Page("Page").Link("Link").Exist(0) = "True" then Reporter.ReportEvent micPass "Link Found", "YES" End If
Numeric Comparison (Numeral -1- is present):
If Browser("Browser").Page("Page").Link("Link").Exist(0) = 1 then Reporter.ReportEvent micPass "Link Found", "YES" End If
If you want to compare Boolean expressions on both sides use AND
If Browser("Browser").Page("Page").Link("Link").Exist(0) AND True then Reporter.ReportEvent micPass "Link Found", "YES" End If
Hi,
I have got a question! when I observed Watch section. Please look at the corresponding interrelated columns.
Name
—-
1. Browser(“BrowObj”).Page(“PgObj”).Link(“LinkObj”).Exist(0)
2. True
3. CInt(Browser(“Inside The Principal Home”).Page(“Inside The Principal Home”).Link(“Advanced Search”).Exist(0))
4. CInt(True)
Value
—–
1. True
2. True
3. 1
4. -1
Type
—–
1. Boolean
2. Boolean
3. Integer
4. Integer
Question: Why does one which has same value and same Type return different integer values??
Thanks,
Sandeep
Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=True .
In this assignment, we should not use boolean value on the left of the operator (=) with exist because it will return the boolean value after executing the code.
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=”True”, this statement is wrong because Exist(0) is not a method to return value, after all this is a property. so any property doesn’t return any value. But this statement is trying to compare with “True” string with the return value. So it through error.
Where as in the second statement return value not taken. so it doesn’t give any runtime error.
Hi,
first one is comparing true=true then only it will return yes
second one if true then it will return yes
in that case no need to use equqlity operator and boolean values.
why because the Exist function returns boolean value.
Exist(0) : if already object is exists
Exist(5) : tool will wait for 5 sec and then it will return boolean value
> Can you find a problem with the 1st piece of code?
The true “flaw” is the limitation in the (=) operator in VBScript .
(Equals to operator (=) compares string or numeric expressions.)
Now VBScript is forced to do implicit conversions of the boolean factors in the expression.
If boolean factors were compared innately then this conundrum would not exist.
There is no difference in above two statements, when you use a boolean, execution will get faster since its only depend on quick response of the boolean and also there will be no wating
Here is a detailed explanation on the issue
In fact Exist statement will return BOOLEAN VALUE.That means it will return either 1 or 0
I appreciate if you suggest me the right way
–Ramu
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=1 Then
Reporter.ReportEvent micPass “Link Found”, “YES”
End If
just try the above code.It will pass the statement
According to the syntax of the Exist statement, it will return either 1 (True) or 0 (False) . So if you compare with TRUE, it is going in else statement.
–Ramu
I just tried by keeping TRUE in double quotations then it has executed True conditon statement. i thought we need to convert True into string. then i succeeded.
Let me know why we need to keep in it double quotations
–Ramu
Pulekar! your explaination too seems to be okay to me!…..
I would request people who comment here also make an effort to correct each other!…..Pin-point if anyone is CORRECT or INCORRECT!…….
………AND NOT JUST KEEP POSTING YOUR ANSWERS/COMMENTS to the given question!
The idea is to make this forum interesting and interact with each other!….openly !….!
I certainly don’t have any problem if anyone pinpoints that I am wrong!…….except!…..kindly comment where I have gone wrong!
Hope the comment is well taken!
Thanks
Subramanian
Praabas!
I wanted to know why we need to put “True” in quotes…..Do you wish to say that the left hand side of your above expression returns a string ????
…..Well! .I don’t think so!….
If Browser(”Browser”).Page(”Page”).Link(”Link”).Exist
itself should return a true or false since it is a boolean expresion!….
Correct me if I am wrong!
Subramanian
subu_scorpio@yahoo.com
Chennai!
Yes there is a problem with the 1st piece of code.
This comparisation will give a ‘false’ result.
The solution :If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=”True” Then
Reporter.ReportEvent micPass “Link Found”, “YES”
End If
I agree with Rekha her answer seems to be more logical!………..anyone disagrees?
exit is a method used in qtp. in the first piece of code, it is sufficient if exist() alone is written. i mean, no need of passing any value to it. true will be displayed in the result window if the link is present.
thanks
Hi All,
There is no difference between both the codes. If I do not put any euals operator after any conditional statement then in default case condition met if it returns True. Here in first code condition met if it returns True as specified. But in 2nd code nothing is specified, so by default the condition will be met if it returns True.
If “Browser(“Browser”).Page(“Page”).Link(“Link”)” this link exist, then in both the cases “Reporter.ReportEvent micPass “Link Found”, “YES”” statement will be executed.
Thanks,
Suman
Hello,
The problem with the first code is if condition will not pass because
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=True
gives FALSE, if link is present also.
That is why because The True keyword has a value equal to -1, and if the link is exist then it will return “True”, so it means from the if statement
(True=-1)=False
so it will not pass the if condition.
But with the 2nd code it will pass the condition if link is there.
Here, Exist(0) returns “True”, which is a contant and we are comparing this contant with Keryword TRUE which are not equal, so it is not entering into the loop.
solution:
a=Browser(“Browser”).Page(“page”).Exist(0)=TRUE
If a=”True” Then
reporter.ReportEvent micPass, “grid found”, “Yes”
End If
“Exist” Property Returns a Value(Boolean). In the first block of code, Comparing Returned Value(True or Flase) with VB Keywork True will not work. We should use String while comparison(Means we should use “True” instead of True). So first Code will give error. Below is the solution.
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=”True” Then
Reporter.ReportEvent micPass “Link Found”, “YES”
End If
Hope this woks fine. Second Bock doesn’t contain any errors. It shout work fine.
I concur with Tarun. A good way to determine comparison would be to msgbox both sides of the equation. The TRUE keyword, and the string “True” have different values, and this would can be seen by playing with a sample if statement.
The 1st piece of code is wrong
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)=True
= True is wrong
the correction should be
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0)
In first code: if we use “=True” means if that link exists in the page then it assigns as true and then it enters into if statement. in second code: it just checks if that link are exists in the page and then enters into if statement.
With the “True” value in first code we can do further activity.
Its a Syntax problem, a comma should be placed rite after ReportStatus.
Thanks,Santhosh
We don’t need =True in the first code, Exist(0) does the same job with out giving the =True.
Correct me if I am wrong.
Hi All
in both the statements,
Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0) will return — True if the specified link exist.
If you keep the True in double quatations, both the statements wil give you PASS. We need specify True as a String.
Please let me know if you have any further clarifications
Thanks
Ramu
After some experimenting it looks like ‘True’ = -1 and ‘….Exist(0)’ = 1 even though both technically evaluate to ‘True’. So the first bit of code will not evaluate to True even though it looks as though it should.
To fix it just take out the ‘= True’ or set it to ‘= 1’.
As per my opinion .exist() checks, given link is available or not, if it is available it returns true else it returns false.
The difference is in the value of True being represented.
[code]
‘will give 1 while
MsgBox Cint(Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0))
Will give -1.
Msgbox Cint(True)
[/code]
Which does means =True won’t work as QTP True has a value 1.
You need to case it to string to do he comparison
[code]
If Browser(“Browser”).Page(“Page”).Link(“Link”).Exist(0) = “True” Then
Msgbox “Bingo”
End if
[/code]