Micro Focus QTP (UFT) Forums

Full Version: What is wrong in this code - need help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear All,

I am trying the below piece of code at home. The reporter.report event returns "Fail" for the below code.

Please look in to the code and advise me where it went wrong.

Code:
Set IE1=CreateObject("InternetExplorer.Application")
IE1.visible = True
IE1.navigate "www.google.com"

bpname"google","google" '' Calling the function bpname

If cons1 > 0 Then
    reporter.ReportEvent micPass, "Passed","Links found"
    else
    reporter.ReportEvent micFail, "Failed","Links NOT found"
End If
'Closing the browser'
While Browser("CreationTime:=0").Exist
    Browser("CreationTime:=0").Close
Wend
'Closing the browser ends'
''Start of the function bpname
Function bpname(bname, pname)
   Set obj=description.Create
    obj("html tag").Value ="A"
   set  var = browser("name:=" &bname).page("title:="&pname).ChildObjects(obj)
    cons1=var.count
    msgbox cons1
End Function
''end of the function bpname''

Thanks in advance,
Kishore
Hi Kishore,
There is nothing wrong in the code only thing is u have not declared the variable "cons1"
that is the reason which is not giving the value defined in function to get a value executed within the function u must have to declare that variable so that it would become public variable

Please find the below one
Code:
Dim cons1
Set IE1=CreateObject("InternetExplorer.Application")
IE1.visible = True
IE1.navigate "www.google.com"

bpname"google","google" '' Calling the function bpname

If cons1 > 0 Then
    reporter.ReportEvent micPass, "Passed","Links found"
    else
    reporter.ReportEvent micFail, "Failed","Links NOT found"
End If
'Closing the browser'
While Browser("CreationTime:=1").Exist
    Browser("CreationTime:=1").Close
Wend
'Closing the browser ends'
''Start of the function bpname
Function bpname(bname, pname)
   Set obj=description.Create
    obj("html tag").Value ="A"
   set  var = browser("name:=" &bname).page("title:="&pname).ChildObjects(obj)
    cons1=var.count
    msgbox cons1
End Function
''end of the function bpname''
Thanks Venkat. I will correct this and try again.

Kishore
I have declared the variable cons1 and tried again. Same issue is observed (Test is getting failed). any suggestions please?

Thanx
Kishore

I tried by keeping the below code inside the function and test is passed.
If cons1 > 0 Then
reporter.ReportEvent micPass, "Passed","Links found"
else
reporter.ReportEvent micFail, "Failed","Links NOT found"
End If

Thanks for ur time,
Kishore
Hi Kishore,

Try with the below code.

Fyi,In below code required Browser(Google) will only be closed after reporting "PASS" to Test Results.(Not all active Browsers) Smile

Code:
Dim cons1
Set IE1=CreateObject("InternetExplorer.Application")
IE1.visible = True
IE1.navigate "www.google.com"

bname="Google"
pname="Google"
bpname bname,pname '' Calling the function bpname

If (cons1>0) Then
    reporter.ReportEvent micPass, "Passed","Links found"
else
    reporter.ReportEvent micFail, "Failed","Links NOT found"
End If
'Closing the browser'
   Set br_obj=Description.Create
    br_obj("micclass").Value="Browser"
    br_obj("title").Value="Google"
    
   Set br=Desktop.ChildObjects(br_obj)
      For i=0 to br.count-1
             Act_br_name=br(i).GetRoProperty("title")
      If  Act_br_name=bname Then
                  br(i).close
               End If
     Next
  
''Start of the function bpname
Function bpname(bname, pname)
   Set obj=description.Create
    obj("html tag").Value ="A"
   set  var = browser("name:=" &bname).page("title:="&pname).ChildObjects(obj)
    cons1=var.count
    msgbox cons1
End Function
''end of the function bpname''
Hi Kishore,
Strange thing,
I am not getting any Fail results and am able to see the pass results and which is working as expected.

Please chek it once by keeping Dim cons1
Even in mycase also which got unexpected behavior when it is not assigned like Dim cons1
one it has been declared then am not seeing any problems
Please check it once again....

Regards,
Venkat.Batchu
Hi,

call the function as
Code:
cons1 = bpname ("google","google")

And in the bpname function change to the "cons1=var.count" in to "bpname =var.count" now you try to run.



Code:
Set IE1=CreateObject("InternetExplorer.Application")
IE1.visible = True
IE1.navigate "www.google.com"

cons1 = bpname ("google","google")  ' Calling the function bpname

If cons1 > 0 Then
reporter.ReportEvent micPass, "Passed","Links found"
else
reporter.ReportEvent micFail, "Failed","Links NOT found"
End If
'Closing the browser'
While Browser("CreationTime:=0").Exist
Browser("CreationTime:=0").Close
Wend
'Closing the browser ends'


Code:
''Start of the function bpname
Function bpname(bname, pname)
Set obj=description.Create
obj("html tag").Value ="A"
set var = browser("name:=" &bname).page("title:="&pname).ChildObjects(obj)
bpname =var.count
msgbox cons1
End Function
''end of the function bpname''
Thanks for adding new point Sreekanth!