Verifying if an object has a specific property

If we try addressing an unsupported property of an object's method an error would show up in the log.

To first verify if a property or a method is supported by the given controls element, we can use a specific method of IsSupported, that is extended by the aqObject object.

In this recipe, we will consider an example of using the IsSupported method.

Getting ready

Launch Calculator Plus (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe).

How to do it...

The following function demonstrates usage of the aqObject.IsSupported method with the example of the Calculator Plus application:

  1. In the loop, we will fine-tooth-comb all the sibling controls elements of the application and have the value of the wText property outputted to the log, if the same is available in the given controls element.
  2. Write and launch the following function:
    function testPropertySupported()
    {
      var wCalc = Sys.Process("CalcPlus").Window("SciCalc");
      wCalc.Activate();
      var objects = wCalc.FindAllChildren("Visible", true);
      objects = (new VBArray(objects)).toArray();
    
      for(var i = 0; i < objects.length; i++)
      {
        if(aqObject.IsSupported(objects[i], "wText"))
        {
          Log.Message(objects[i].wText);
        }
        else
        {
          Log.Warning("Object '" + objects[i].Name + "' doesn't have wText property");
        }
      }
    }
  3. In the result of the preceding function call, we will obtain one message with the text 0. (the text from the result output field of the calculator) and several warnings concerning the property being unsupported.

How it works...

With the help of the FindAllChildren method, we will obtain an array of all the sibling objects of the calculator window. Since the FindAllChildren method returns an array suite-formatted after Visual Basic, we need to transmute the same to fit the JScript array format with the help of the toArray method.

Further on in the loop, with the help of the aqObject.IsSupported method, we check if the wText property is available for each controls element, and thus we output the value of this property, provided it exists for the given controls element, else a warning occurs related to the missing property of the ith object in the loop.

Property availability validation for an object of a specific method is executed in a similar way.

See also

  • To learn more about finding objects by property values refer to the Finding objects by properties' values recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.208