cancel
Showing results for 
Search instead for 
Did you mean: 

Object sometimes doesn't support Exists

SOLVED
Occasional Contributor

Object sometimes doesn't support Exists

I have been having an issue throughout some of my scripts where an object suddenly doesn't support the Exists property. When running through the below code, sometimes the script will crash on the if (key.Exists) line. The error is Object doesn't support this property or method. But it's not consistent. Sometimes I get through the script without any problems. When I add a breakpoint and run aqObject.isSupported(key, "Exists"), it returns true. And when I check the object in the Locals tab and the Object Browser it says the Exists property is true.

 

function findKey(propertyName, character, shiftMode) {
  var keyboard = getKeyboard()
  
  var key = keyboard.findChild(propertyName, character, 2)
  
  if (key.Exists) {
    keyboard.set_ShiftMode(shiftMode)
    key.Click()
    
    return true
  }
  
  return false
}

According to the documentation for findChild, "If no object matching the search criteria was found, the FindChild method returns a stub object that only contains the Exists property equal to False." So why does my object sometimes not have the property?

1 ACCEPTED SOLUTION

Accepted Solutions
Community Leader

Re: Object sometimes doesn't support Exists

Ah ! Found it..  I asked a very similar question previously:

 

aqObject.GetPropertyValue object does not exist after an exist check passes

 

@AlexKaras spent some time explaining TCs caching strategy, which may be the cause ?


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
19 REPLIES 19
Community Leader

Re: Object sometimes doesn't support Exists

Thank you coharness for your question,

 

It is a chicken and egg kind of situation.

Your code will always work correctly as long as "key" is not NULL.  That means the Object associated with it was found and then the value of True or False is validated.

Unfortunately, if the object is underfined or Null, saying if (key.Exists) is causing a crash as you are accessing a property of a NULL object.

So the correct way to do this is ALWAYS:

if (key != Null)

{

 if (key.Exists) 

...

}

That is always the safe way of checking before causing a Null pointer exception in the code.

Thanks

-Lino

Occasional Contributor

Re: Object sometimes doesn't support Exists

Thanks for the suggestion. Unfortunately, even when wrapped in that null check it still fails the key.Exists line.

Community Hero

Re: Object sometimes doesn't support Exists

What is the "getKeyboard" function you're calling?  What does it actually return?

I also notice that your "findChild" has capitalization differences... the ACTUAL FindChild method provided by TestComplete has capital letters on both the F and the C....  is this a different findChild method that is attached to some custom object that getKeyboard returns?

Essentially... I'm seeing non-standard functions in this code that makes me question if we should actually expect the indicated findChild method to return what we're expecting it to return.    If you can provide the additional code or give us some information about what these other functions are, that would be helpful.

 

Thanks.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Community Hero

Re: Object sometimes doesn't support Exists

Another possibility could be timing...  You may be executing findChild and the object is not actually present properly... or it's destroyed and recreated or something.  Some more information of the timing of the execution may play  part here as well.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Occasional Contributor

Re: Object sometimes doesn't support Exists

Our software uses a soft keyboard. It can be located in one of two different screens. So the getKeyboard() function is a little helper that checks for whichever screen is active and returns the keyboard.

 

There is also a function I run right before this one that just clicks every key to make sure they work. That code is below. As for the FindChild() function, that was just a typo. I'm surprised it didn't crash there if it wasn't the right case. I switched it over to the proper name and it still failed.

 

function enterAllKeys(textField, shiftMode) {
  // Clears the text and gives focus
  textField.Keys(clearAll)
  
  var keyboard = getKeyboard()
  keyboard.set_ShiftMode(shiftMode)
  
  // Holds the entered text to make sure all the keys worked.
  var enteredKeys = ""
  
  Log.Message("Testing all keys in Shift Mode: " + shiftMode)
  
  for (var i = 0; i < keyboard.ChildCount; i++) {
    var key = keyboard.Child(i)
    
    enteredKeys += Chr(key.Child(0).activeChar())
    
    key.Click()
  }
  
  aqObject.CheckProperty(textField, "wText", cmpEqual, enteredKeys)
}
Community Leader

Re: Object sometimes doesn't support Exists

As pointed out by previous poster, it could be because of a timing issue and the FindChild not being able to actually find the child.  Granted, you'd still expect a stub object. What happens if you set the Refresh flag of FindChild to true ? You may find that a previous key press affected the current mapping TC has and the reference gets destroyed.

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
Community Leader

Re: Object sometimes doesn't support Exists

Also, I'm assuming the getKeyboard() method also has some find logic in it.  Is it the key.Exists or keyboard that's encountering the issue.

 

If you put a breakpoint before 

keyboard.set_ShiftMode, is the actual keyboard object stil there:

 

var key = Utils.CreateStubObject();
if (keyboard.Exists) {
   key = keayboard.FindChild...
   if(key.Exists){

      keyboard.set_ShiftMode(shiftMode)
      key.Click()
    
       return true
     }
else{
return false
} else{ //the keyboard lost it's way return false; //or, retry getting the keyboard after refreshing mapping and repeat the .Exists logic }

PPS

 

aqObject.isSupported(key, "Exists") will always be true if the object exists.  Even if it's a stub object, the property is supported. 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
Contributor

Re: Object sometimes doesn't support Exists

I have the same error that appeared today. My TC's project work for three years, and suddenly, I've got this error "Object doesn't support this property or method". To the same Exists method.

 

This is incomprehensible. The project has not changed.

 

I think there is some Windows Update behind this.

Community Leader

Re: Object sometimes doesn't support Exists

Very interesting issue.

 

If your app is written in Java or DotNet, there is a big possibility that the Garbage Collector has a hand in this.

Based on a new update from windows or an update to the frameworks (happens often) there is a possibility that the Garbage Collector is freeing the object and recreating on demand agressivilly.  That is why you are seeing the problem manifest itself at the moment.  It is pretty straight forward really, if Testcomplete can not find the object in memory at the time its property is being accessed you are going to crash.

One thing I have done in the past for similar problems in DotNet apps is to reference the object in a variable in TestComplete script early on in the procedure.  That will keep a live reference incremented in memory and when the app decrease the reference to the object, the Garbage collector will not remove the object from memory as its reference would not have reached zero.

 

Hope that helps

Cheers

Lino

New Here?
Join us and watch the welcome video:
API Testing Mistake #2
APITestingMistake#2
Top Kudoed Authors