Forum Discussion

salley's avatar
salley
Frequent Contributor
7 years ago

Return a page object using vbscript function and use it from another func

Hi , i'm trying to create a generic function, that will return an object then call that function to do specific task. my script is executing successfully when the object exists, but giving me run time error when the object is not there, pls see example below,,

thanks, any help will be appreciated

'+++++++

Function PoScreen
Set sPage=Aliases.browser.MainPage
'Selects Company
strEffDteProp=Array("ObjectIdentifier","ObjectType")
strEffDtePropVal=Array("DisplayEffectiveDate","Textbox")
Set objEffDte=MyObj (sPage,strEffDteProp,strEffDtePropVal)
objEffDte.SetText"11/01/2017"

End Function

 

 

'@@@@@@@@@@@@@@

Function MyObj(sPage,arrProp,arrVal)
Set obj=sPage.FindChild(arrProp,arrVal,50)
If (obj.Exists) Then
Set MyObj=obj
Else
Log.Message"Obj Not Found"
End If
End Function

 

''''''''this is error"

' TEST LOG

' Obj Not Found
Object required: 'myObj(...)' 
Script execution was interrupted. 

  • Hi,

     

    Observed behavior is correct and expected one.

    When object is not found, the value returned by MyObj() is not an object. Thus it obviously does not contain the .SetText() method.

    Suggested code modification:

    Function PoScreen
    Set sPage=Aliases.browser.MainPage
    'Selects Company
    strEffDteProp=Array("ObjectIdentifier","ObjectType")
    strEffDtePropVal=Array("DisplayEffectiveDate","Textbox")
    Set objEffDte=MyObj (sPage,strEffDteProp,strEffDtePropVal)
    If (objEffDte.Exists) Then
      objEffDte.SetText"11/01/2017"
    Else
      ... ' process appropriately
    End If
    End Function
     
     
    '@@@@@@@@@@@@@@
    Function MyObj(sPage,arrProp,arrVal)
    ' Set obj=sPage.FindChild(arrProp,arrVal,50)
    ' If (obj.Exists) Then
    ' Set MyObj=obj
    ' Else
    ' Log.Message"Obj Not Found"
    ' End If
    
    Set MyObj=sPage.FindChild(arrProp,arrVal,50)
    If (Not MyObj.Exists) Then
      Log.Message"Obj Not Found"
    End If
    End Function
     

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Observed behavior is correct and expected one.

    When object is not found, the value returned by MyObj() is not an object. Thus it obviously does not contain the .SetText() method.

    Suggested code modification:

    Function PoScreen
    Set sPage=Aliases.browser.MainPage
    'Selects Company
    strEffDteProp=Array("ObjectIdentifier","ObjectType")
    strEffDtePropVal=Array("DisplayEffectiveDate","Textbox")
    Set objEffDte=MyObj (sPage,strEffDteProp,strEffDtePropVal)
    If (objEffDte.Exists) Then
      objEffDte.SetText"11/01/2017"
    Else
      ... ' process appropriately
    End If
    End Function
     
     
    '@@@@@@@@@@@@@@
    Function MyObj(sPage,arrProp,arrVal)
    ' Set obj=sPage.FindChild(arrProp,arrVal,50)
    ' If (obj.Exists) Then
    ' Set MyObj=obj
    ' Else
    ' Log.Message"Obj Not Found"
    ' End If
    
    Set MyObj=sPage.FindChild(arrProp,arrVal,50)
    If (Not MyObj.Exists) Then
      Log.Message"Obj Not Found"
    End If
    End Function
     

     

    • salley's avatar
      salley
      Frequent Contributor

      Thank you so much,,,,,,,,,,,,,,,, got it,