Forum Discussion

coharness's avatar
coharness
Occasional Contributor
6 years ago

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?

  • LinoTadros's avatar
    LinoTadros
    Community Hero

    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

    • coharness's avatar
      coharness
      Occasional Contributor

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

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    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.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      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.

      • coharness's avatar
        coharness
        Occasional Contributor

        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)
        }
  • 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.

     

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      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. 

      • eykxas's avatar
        eykxas
        Regular Contributor

        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.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Can you provide us with the source code for the getKeyboard() function and let us know what application are you working with (web, desktop, etc.) ?

     

    • coharness's avatar
      coharness
      Occasional Contributor

      Here's the code to get the keyboard. This is a desktop application.

      function getKeyboard() {
        var activeView = Aliases.AppName.HwndSource_Window.WindowTouchViewmodelsShellview.ShellView.Grid.ActiveItem
        
        if (activeView.LoginView.Exists) {
          return activeView.LoginView.Grid.SoftKeyboard
        } else if (activeView.HomeView.Exists) {
          return activeView.HomeView.Grid.SoftKeyboard
        } else return null
      }

      I have tried a couple of other things in my findKey() function. I tried setting the shiftMode before finding the key, and it still crashes. I also tried changing the properties in my FindChild call to look like this

      var key = keyboard.FindChild([propertyName, "Exists"], [character, true], 2, true)

      And it still failed.

       

      To add something else to help, here are some screenshots of the info I'm checking when I set a breakpoint on the line that fails.

       

       

      As you can see, especially from the last one, the object has the property, but it doesn't actually work when calling it.

      • RUDOLF_BOTHMA's avatar
        RUDOLF_BOTHMA
        Community Hero
        else return null
        

        This means your function could return null.  You won't be able to get the Exists property, since it's not an actual object with properties.  Try rather returning Utils.CreateStubObject();

         

        Edit:

        Propertoes...  As apposed to my stubby little toes :smileywink: