Tutorial: Create UFT Support for a Custom WPF Control > Implement Support for Recording

Implement Support for Recording

In this section, you implement the IRecord interface and write the event and message handling methods to support recording steps on the custom control.

There are three types of steps that need to be recorded for the WPF custom calendar: Next, Prev, and SetDate.

To implement support for recording in your custom server class:

  1. In the CalendarSrv class, locate the section for the IRecord interface implementation.

  2. 
    public void OnMessage(DependencyObject src, int msg, int wParam, int lParam)
    {
    }
    public void RecordInit()
    {
    }
    public void RecordStop()
    {
    }
    
  3. Declare your event handler and implement RecordInit to register it to the control.

    private EventHandler<SelectionChangedEventArgs> _h;
    public void RecordInit()
    {
        _h = new EventHandler<SelectionChangedEventArgs>(OnSelectedDatesChanged);
        UtilityObject.AddHandler(MyCalendar, "SelectedDatesChanged", _h);
    }
    

    Note: When developing support for a Silverlight control, the AddHandler syntax is different. For details, see the Mercury.QTP.Slv.CustomServer namespace in the Custom Server API Reference.

  4. You do not need to implement RecordStop because you registered the event handler using the SDK's AddHandler method. This enables UFT to automatically remove the event handler at the end of a recording session.

  5. Add the OnSelectedDatesChanged event handler implementation:

    private void OnSelectedDatesChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        UtilityObject.Record("SetDate", RecordingMode.RECORD_SEND_LINE, MyCalendar.SelectedDate.Value.ToShortDateString());
    }
    

    This creates a SetDate step with the new date selected by the user during a recording session.

  6. Implement the OnMessage method as follows, to support recording Prev and Next operations:

    public void OnMessage(DependencyObject o, int msg, int wParam, int lParam)
    {
        if(o is Button && msg == 0x201) // WM_LBUTTONDOWN
        {
            string name = (o as Button).Name;
            switch (name)
            {
                case "PART_NextButton":
                        base.UtilityObject.Record("Next", 
                        RecordingMode.RECORD_SEND_LINE, null);
                break;
                case "PART_PreviousButton":
                        base.UtilityObject.Record("Prev", 
                         RecordingMode.RECORD_SEND_LINE, null);
                break;
           }
        }
    }

    Note: When developing support for a Silverlight control, the OnMessage method would return RECORD_HANDLED, indicating that the custom server handled this message and it does not have to be passed on to any other event handlers. For more information, see the Mercury.QTP.Slv.CustomServer namespace in the Custom Server API Reference.

  7. This step is necessary only when developing support for Silverlight controls.

    Implement the GetWndMessageFilter method to specify the level of Windows messages to be handled by the custom server.

    CTL_MsgFilter GetWndMessageFilter()
    {
        return CTL_MsgFilter.CTL_MSGS;
    }
    

    In this tutorial, all children of the control are regarded as part of the control. Therefore, it is sufficient to return CTL_MSGS, and handle messages intended only for this control.

    If some of the control's children were treated as separate test objects, but you still wanted the control to handle events that occurred on these children, you could implement GetWndMessageFilter to return CHILD_MSGS.

Continue to Deploy and Test Your Support for Recording.