Forum Discussion

BenY_1's avatar
BenY_1
New Contributor
13 years ago

Error when using the native JScript ".hasOwnProperty()" method

Hi there,



I am getting a "Non-existent object" error when I try to execute the .hasOwnProperty() JScript method.  It looks as if TestComplete doesn't recognize .hasOwnProperty() as a JScript method for an object.  Is there a reason why?  The method is supported by JScript version 5.5.



Any help is appreciated!  



I am using TestComplete 8.50.618.7.  JScript is the scripting language used in all our TestComplete code.



--- Ben.

5 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    It depends upon what kind of object you're trying to call it on.  If you have a native JScript object, the hasOwnProperty method will work.



    However, if you have a windows object or a page object or some other tested object as determined by TestComplete, those are not native JScript objects and, therefore, do not have that method associated with them.



    Would you post a segment of the code where you're intending on using it?  That would be helpful in knowing better how to help you debug.
  • BenY_1's avatar
    BenY_1
    New Contributor
    Thanks for the pointer, Martin.  I was trying to use it on a page object, so the error is legit then.



    Here's the code:



    try


    {


      strObjectType = "";


       if ((oMyTestObject!= null) && (oMyTestObject.hasOwnProperty("objectType")))


    {  

    strObjectType = oMyTestObject.objectType;

    }


    }




    catch (oE)


    {


    // do nothing


    }




     Here, we implemented a try/catch to deal with the case where .hasOwnProperty throws an error.  However, for some reason, when it does throw an error, it is not catch by the catch block.  Instead, TestComplete just logs an "Unable to find the object hasOwnProperty("objectType")" and move on to execute the statement within the if block.  The error causes the test to be marked as failed.



    Any idea why the error is not caught by the catch block?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    try/catch only catches certain kinds of errors, I believe.  Someone posted up here at one point what the classification is but I can't recall right now.



    You're looking to find out if the object supports a particular property, correct?



    Try aqObject.IsSupported.  I'd change your code to the following.



    try 



      strObjectType = ""; 

       if ((oMyTestObject!= null) && (aqObject.IsSupported(oMyTestObject, "objectType"))) 

    {  

    strObjectType = oMyTestObject.objectType;







    catch (oE) 



    // do nothing 

    }





  • BenY_1's avatar
    BenY_1
    New Contributor
    The function aqObject.IsSupported worked and solved my problem.  



    Thanks Robert!