.

Quiz: Find the difference between these VBScript codes?

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

Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

26 comments ↓

#1 Tarun Lalwani on 01.28.09 at 00:09

The difference is in the value of True being represented.

'will give 1 while
MsgBox Cint(Browser("Browser").Page("Page").Link("Link").Exist(0))

Will give -1.
Msgbox Cint(True)

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

If Browser("Browser").Page("Page").Link("Link").Exist(0) = "True" Then
   Msgbox "Bingo"
End if
#2 Mayur on 01.28.09 at 06:18

As per my opinion .exist() checks, given link is available or not, if it is available it returns true else it returns false.

#3 reddog87 on 01.28.09 at 08:27

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′.

#4 Santhosh on 01.28.09 at 20:56

Its a Syntax problem, a comma should be placed rite after ReportStatus.

Thanks,Santhosh

#5 srikanth on 01.28.09 at 21:12

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.

#6 RamaSubramanian on 01.28.09 at 21:44

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)

#7 VENKATA REDDY on 01.28.09 at 22:21

“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.

#8 Rekha on 01.28.09 at 22:25

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

#9 Anand on 01.28.09 at 23:08

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.

#10 ramupalanki on 01.29.09 at 00:07

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

#11 Suman Kumar Das on 01.29.09 at 04:53

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

#12 pulekar on 01.29.09 at 07:15

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

#13 Soujanya on 01.29.09 at 07:49

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.

#14 Sheeraz Memon on 01.29.09 at 11:41

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.

#15 RamaSubramanian on 01.29.09 at 18:33

I agree with Rekha her answer seems to be more logical!………..anyone disagrees?

#16 Praabas on 01.30.09 at 02:22

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

#17 RamaSubramanian on 01.30.09 at 17:20

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!

#18 RamaSubramanian on 01.30.09 at 17:25

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

#19 ramu on 02.01.09 at 23:43

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

#20 ramu on 02.01.09 at 23:55

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

#21 ramu on 02.01.09 at 23:57

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

#22 Tarun on 02.02.09 at 08:23

Here is a detailed explanation on the issue

http://knowledgeinbox.com/articles/qtp/object-identification/why-objectexist-true-returns-false-when-object-exists/

#23 kiran on 02.03.09 at 03:47

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

#24 Rick Hall on 02.03.09 at 06:54

> 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.

#25 madhu on 03.02.09 at 21:11

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

#26 Ramakrishna on 12.08.09 at 17:14

Hi,

first one is comparing true=true then only it will return yes

second one if true then it will return yes

Leave a Comment