Search code examples
vbscriptqtp

User Defined Function with Passing Object


I just wanted help in creating my own function which passes with the object as reference.

Set Pg = Browser("Browser").Page("Login")
Pg.WebEdit("loginForm:userName").cSet("user1")

Public Function cSet(obj, val)
    If IsOBject(Obj) Then  ''Here obj is WebEdit("loginForm:userName")
        Obj.Set val  '''Here val is user1
    Else
        Reporter.ReportEvent micFail,"Failed","Object not found"
    End If
End Function

Getting the below error

Object doesn't support this property or method: 'Browser(...).Page(...).WebEdit(...).cSet


Solution

  • I'm not clear on what you're trying to achieve here. WebEdit already has a Set function that will fail if the object does not exist.

    In any case UFT allows adding (or overriding) functions for specific test objects, see the documentation for RegisterUserFunc.

    For example, if an object doesn't exist UFT will wait for it to exist until a timeout passes. You can tell UFT to fail immediately if the object does not exist.

    ' Define the function
    Function QuickSet(ByRef obj, ByVal value) 
        If Not obj.Exist(0) Then ' zero means don't wait 
            Reporter.ReportEvent micFail, "QuickSet", "Object does not exist"
            Exit Function
        End If
        obj.Set value   
    End Function
    
    ' Register the function to WebEdit as a new function
    RegisterUserFunc "WebEdit", "QuickSet", "QuickSet"
    
    ' Or override the default behaviour of Set
    RegisterUserFunc "WebEdit", "Set", "QuickSet"