Adobe Flex 3 Help

Creating a delegate class

To instrument custom components with a delegate, you must do the following:

  • Create a delegate class that implements the required interfaces. In most cases, you extend the UIComponentAutomationImpl class. You can instrument any component that implements IUIComponent.
  • Register the delegate class with the AutomationManager.
  • Define the component in a class definitions XML file.

The delegate class is a separate class that is not embedded in the component code. This helps to reduce the component class size and also keeps automated testing code out of the final production SWF file. All Flex controls have their own delegate classes. These classes are in the mx.automation.delegates.* package. The class names follow a pattern of ClassnameAutomationImpl. For example, the delegate class for a Button control is mx.automation.delegates.controls.ButtonAutomationImpl.

Instrument with a delegate class

  1. Create a delegate class.
  2. Mark the delegate class as a mixin by using the [Mixin] metadata keyword.
  3. Register the delegate with the AutomationManager by calling the AutomationManager.registerDelegateClass() method in the init() method. The following code is a simple example:
    [Mixin]
    public class MyCompDelegate {
    public static init(root:DisplayObject):void {
    // Pass the component and delegate class information.
    AutomationManager.registerDelegateClass(MyComp, MyCompDelegate);
    }
    }
    
    

    You pass the custom class and the delegate class to the registerDelegateClass() method.

  4. Add the following code to your delegate class:
    1. Override the getter for the automationName property and define its value. This is the name of the object as it usually appears in automation tools such as QTP. If you are defining an item renderer, use the automationValue property instead.
    2. Override the getter for the automationValue property and define its value. This is the value of the object in automation tools such as QTP.
    3. In the constructor, add event listeners for events that the automation tool records.
    4. Override the replayAutomatableEvent() method. The AutomationManager calls this method for replaying events. In this method, return whether the replay was successful. You can use methods of the helper classes to replay common events.

      For examples of delegates, see the source code for the Flex controls in the mx.automation.delegates.* packages.

  5. Link the delegate class with the application SWF file in one of these ways:
    • Add the following includes compiler option and link in the delegate class:
      mxmlc -includes MyCompDelegate -- FlexApp.mxml
      
      
    • Build a SWC file for the delegate class by using the compc component compiler:
      compc -source-path+=. -include-classes MyCompDelegate -output MyComp.swc
      
      

      Then include this SWC file with your Flex application by using the following include-libraries compiler option:

      mxmlc -include-libraries MyComp.SWC -- FlexApp.mxml
      
      

      This approach is useful if you have many components and delegate classes and want to include them as a single file.

  6. After you compile your Flex application with the new delegate class, you must define the new interaction for the agent and the automation tool. For QTP, you must add the new component to QTP's custom class definition XML file. For more information, see Using the class definitions file.

For an example that shows how to instrument a custom component, see Example: Instrumenting the RandomWalk custom component for QTP.