Forum Discussion

windend's avatar
windend
Occasional Contributor
13 years ago

Get the object by "evaluating" the string holding its name

hi, i'm reading findchild method in html help file. The example is given as below. However, when i execute this sub and try to use wndNotepad.Exsits before Log.Picture line to verify whether the notepad object is located correctly, testcomplete return an err msg says

 "An exception occurred in the "findChildwithName" unit at line 12: Microsoft VBScript runtime error.Object required: 'wndNotedpad' ". I also tried this usage in some other ways, testcomplete can't find the object. My question is what the correct way to operate the objects got by "evaluating" the string holding its name? Thank you.




Sub NotepadTest

Dim strObjName, p, wndNotepad



strObjName = "Window(""Notepad"", ""* - Notepad"")"



' Store the parent object to a variable

Set p = Sys.Process("notepad")



' Obtain the object by its Name property

' p is the name of the variable that holds the parent object

Set wndNotepad = Eval("p." & strObjName)



Log.Picture wndNotepad, "Notepad window", wndNotepad.FullName

End Sub


10 Replies

  • VLapidus's avatar
    VLapidus
    Frequent Contributor
    You misspelt wndNotepad:



    "Microsoft VBScript runtime error.Object required: 'wndNotedpad'"



    Remove 'd' in the middle of the name
  • windend's avatar
    windend
    Occasional Contributor
    Oh.. i posted my question too carelessly.



    Actually, if i spell the word correctly, the err msg is "Unable to find the object Exists." TestComplete still can not find the notepad object. Sorry for the confusion.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Amy,



    Please post here the complete script you're using. The one in your original post doesn't include the Exists check.
  • windend's avatar
    windend
    Occasional Contributor

    hi Helen, below is my code.thanks.



    Sub NotepadTest

    Dim strObjName, p, wndNotepad





    strObjName = "Window(""Notepad"", ""* - Notepad"")"





    ' Store the parent object to a variable

    Set p = Sys.Process("notepad")





    ' Obtain the object by its Name property

    ' p is the name of the variable that holds the parent object

    Set wndNotepad = Eval("p." & strObjName)

    If wndNotepad.Exsits then

    Log.Message("Ok")

    End If

    Log.Picture wndNotepad, "Notepad window", wndNotepad.FullName

    End Sub
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Amy,

    If wndNotepad.Exsits then
    There's a typo in this line - it should be Exists, not Exsits.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Amy, 



    The problem is in the following logic block.



    Set wndNotepad = Eval("p." & strObjName)


    If wndNotepad.Exists then


    Log.Message("Ok")


    End If 


    Log.Picture wndNotepad, "Notepad window", wndNotepad.FullName 


    End Sub






    There are two problems with this.



    First of all, if wndNotepad does not exist, you cannot call the Exists property of it.  The code you're using to find the child object is simply the "Window" method which assumes, ahead of time, the object exists.  This is probably why you're getting the "object not found" error because you cannot call "Exists" on an non-existant object.



    Secondly, even if that worked, if the object does not exist, you cannot capture a picture of it.  Log.Picture requires an object to be present and exisiting so you cannot capture a screenshot of it.



    Let me suggest a different piece of code for this.



    Sub NotepadTest 


    Dim p, wndNotepad 


    Dim PropArray


    Dim ValuesArray






    PropArray = Array("wndClass", "wndCaption")


    ValuesArray = Array("Notepad", "* - Notepad")






    Set p = Sys.Process("notepad") 






    Set wndNotepad = p.FindChild(PropArray, ValuesArray, 1)


    If wndNotepad.Exists then


    Log.Message("Ok")


    Else


    Log.Warning("The notepad window was not found")


    End If 







    End Sub




    The FindChild is specifically designed for this purpose.  If it finds the object you're looking for, it returns the object.  If it does not find the object, it returns an "empty" or stub object that has an Exists property equal to false.







  • windend's avatar
    windend
    Occasional Contributor
    Thank you, Robert. It's helpful.



    Do you mean when we test a web application. we'd better use findchild with object properties as params to recognize object. Namemapping will be hard to used in this situation?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    In the case of web apps, you can use FindChild or NameMapping.  But if you using NameMapping, it would be better to use a WaitAliasChild to find the necessary object based upon the name and then check "Exists" on the resulting object.  
  • windend's avatar
    windend
    Occasional Contributor
    Thank you, Robert.



    Is there any namemapping method have "depth" param like findChild? waitAliasChild can only find the direct child object.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    You can use find child with NameMapping objects so you can use that depth property.  There isn't, as far as I can see, a method of NameMapping/Aliases objects that uses that depth parameter.