how to return a value from function

1,970 views
Skip to first unread message

vamsi reddy

unread,
Apr 20, 2012, 4:33:18 AM4/20/12
to mercuryqtp
Hi All,
Please clarify the below things - Thanks in Advance
Query 1:
->In the flight application Insert Order, Inserted one record
->I want to the return the value Order_Nu  from the function
->With in the function Order_Nu is displayed
->Outside the function 'msgbox Order_Nu' displaying empty msgbox

Query 2:
How to use function value in QTP Action

-> I put this function in .vbs associated  to action1 in QTP
-> I want to use  'Order_Nu' in Action 1 




InsertOrder(A)
Public function InsertOrder(byval Order_Nu )
Window("Flight Reservation").Activate
Window("Flight Reservation").WinObject("Date of Flight:").Type "111112"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Los Angeles"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "vamsi"
Window("Flight Reservation").WinButton("Insert Order").Click
wait 10
' Fetch the order number
Order_Nu=Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
'To display the order number
msgbox Order_Nu
' Return value 
InsertOrder=Order_Nu
End Function

msgbox Order_Nu

--
Vamsi Reddy

 
 
 

Deepak Kumar Mahapatro

unread,
Apr 20, 2012, 12:05:27 PM4/20/12
to mercu...@googlegroups.com
try this
Dim Ordr_Nu
Public Function InsertOrder()
Window("Flight Reservation").Activate
Window("Flight Reservation").WinObject("Date of Flight:").Type "111112"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Los Angeles"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "vamsi"
Window("Flight Reservation").WinButton("Insert Order").Click
Wait 10
' Fetch the order number
Order_Nu=Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
'To display the order number
msgbox Order_Nu
' Return value 
InsertOrder=Order_Nu
End Function

Ordr_Nu=InsertOrder
msgbox Ordr_Nu


To use Order_Nu in Action, you can use the var Ordr_Nu

--
Vamsi Reddy

 
 
 

--
You received this message because you are subscribed to the Google
"QTP - HP Quick Test Professional - Automated Software Testing"
group.
To post to this group, send email to Mercu...@googlegroups.com
To unsubscribe from this group, send email to
MercuryQTP+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/MercuryQTP?hl=en



--
Regards,
Deepak

Anish Pillai

unread,
Apr 22, 2012, 11:43:49 AM4/22/12
to mercu...@googlegroups.com
Hi Vamsi,

You need to define the variable with global scope so that it becomes available inside and outside the function. This way you would not need to pass the variable inside the function. Typically you can declare the variable at the beginning of the code. Eg

Dim var
var = 10
msgbox var  'this displays 10

Function fnTest()
  var = var + 5
  msgbox var    'this shows 15
End Function

msgbox var   'this shows 15


Now when you pass the value to a function using ByVal a copy of the variable is sent to the function. So whatever change you make to that variable inside the function it will not reflect to the original variable outside the function. So in your code above, use ByRef instead of ByVal. ie use Public function InsertOrder(byRef Order_Nu )

If you want to use any variables across actions, you can define the variable inside your function library so that they are available to all the actions associated with the function library.




--
Regards,
Anish Pillai
My QTP Blog

Reply all
Reply to author
Forward
0 new messages