Sunday, January 9, 2011

Return an Object to Function (Using Descriptive Programming)

' Descriptive programming to use the calling function as an object, called function contain the descriptive programming to identify the object
' Invoke Browser and navigate to google
systemutil.Run "iexplore",""

' Declaring Variables
Dim Obj_Class, Obj_Name, Par_Obj, inp_Val, Obj_Type
' Initialize the variables with desired values.
Obj_Class = "WebEdit"
Obj_Name ="q"
inp_Val = "Baba QTP"
Obj_Type = "WebEdit"
' Set the Parent Object
Set Par_Obj = Browser("Google").Page("Google")
' Call the Function as an object
Describe (Obj_Class, Obj_Name, Par_Obj).set inp_Val     ' Calling function as an object and operating action with available methods.
' Developed Function contains the code to do action
Function Describe (Obj_Class, Obj_Name, Par_Obj)
    Dim des
    Set des = description.Create()
    des("micclass").value = Obj_Class
    des("name").value = Obj_Name
    ' Here finding an object and performing an action.
    Set Chld_Objts = Par_Obj.ChildObjects(des)
    For i=0 to Chld_Objts.count
        act_val = Chld_Objts(i).getroproperty("name")
        If (act_val = Obj_Name) Then
            ' Returning an Object to the calling function.
            Set Describe = Chld_Objts(i)
            Exit Function ' Exiting function once we found an Object
        End If
'   If you wanna work on few objects... They can be keep in Select Case...
'Select Case Obj_Class
'    case WebEdit:
    '    Set Describe = Par_Obj.WebEdit(des)
'    Case WebElement
    '    Set Describe = Par_Obj.WebElement(des)
'    Case else
'        Set Describe = Par_Obj
'End Select
End Function

or if you want to return a single object or a collection of objects, use the below function

Dim ParentObject
Set ParentObject = dialog("Login")

' Funtion to return collection of objects or expected object
Function Describe_Obj(ParentObject, Obj_Class, Obj_Prop, Obj_Value)
   Dim Obj
   Set Obj = Description.Create
   Obj("micclass").value = Obj_Class
   If (Obj_Prop<>"" and Obj_Value<>"") Then
       Obj(Obj_Prop).value = Obj_Value
       Set Desc_Obj = ParentObject.ChildObjects(Obj)
       For Chld_Obj = 0 to Desc_Obj.count
           Act_Val = Desc_Obj(Chld_Obj).getroproperty(Cstr(Obj_Prop))
           If Trim(Act_Val) = Trim(Obj_Value) Then
              Set Describe_Obj = Desc_Obj(Chld_Obj)
              Exit Function
           End If
    End If
    Set Describe_Obj = ParentObject.ChildObjects(Obj)
End Function

If Describe_Obj(ParentObject, "WinButton", "text", "Cancel").getroproperty("disabled") = False Then
   msgbox "hai"
End If

"A good threat is worth a thousand tests" - Boris Beizer

No comments:

There was an error in this gadget