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

If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.

Subscribe to get free updates on UFT!

LearnQTP is the most popular site on UFT (formerly QTP).

Enter your first name and email address below to instantly subscribe and download an eBook on Optimizing UFT Scripts. In future, we will make sure you get new tips & tricks on UFT delivered direct to your email box.

Please check your email and confirm free subscription!