Tutorial: Create UFT Support for a Custom WPF Control > Implement Support for Running Test Object Operations

Implement Support for Running Test Object Operations

In this section, you implement the Run interface in the CalendarSrv class to support running test object operations on the Calendar control.

You specified, in the WPF Custom Server Setup dialog box, that you want to customize running operations. Therefore, the ICalendarRun interface that you specified was defined in the CalendarSrv.cs file, tagged with the RunInterface attribute and implemented in the CalendarSrv class for an example operation, MyRunMethod.

  1. Locate the ICalendarRun interface definition in the CalendarSrv.cs file:

    [RunInterface()]
    public interface ICalendarRun
    {
        void MyRunMethod();
    }
    
  2. Replace the example void MyRunMethod(); with the following lines to complete the interface definition to include all of the operations you want to support:

    void SetDate(string date);
    void Prev(); 
    void Next();
    string SelectedDate
    {
        get;
    }
    
  3. Locate the interface implementation in the CalendarSrv class:

    public void MyRunMethod()
    {
    }
    
  4. Replace the MyRunMethod() example with the following implementation of the Calendar-specific methods and property:

     public void SetDate(String date)
    {
        MyCalendar.SelectedDate = DateTime.Parse(date);
        MyCalendar.DisplayDate = DateTime.Parse(date);
    }
    
    public string SelectedDate
    {
       get
       {
         return MyCalendar.SelectedDate.Value.ToShortDateString();
       }
    }
    
    public void Prev()
    {
        Button prev = GetDescendantByName(UtilityObject.ApplicationObject, "PART_PreviousButton") as Button;
        RaiseButtonClickEvent(prev);
    }
    
    public void Next()
    {
        Button next = GetDescendantByName(UtilityObject.ApplicationObject, "PART_NextButton") as Button;
        RaiseButtonClickEvent(next);
    }
    

    Note: If you were developing support for a Silverlight control, you would tag each one of these methods with the Microsoft Silverlight ScriptableMember attribute.

  5. Add a using System.Windows.Media; statement to the CalendarSrv.cs file and then add the following helper functions:

    private void RaiseButtonClickEvent(Button button)
    {
        if (button != null)
        {
            RoutedEvent e = Button.ClickEvent;
            RoutedEventArgs arg = new RoutedEventArgs();
            arg.RoutedEvent = e;
            button.RaiseEvent(arg);
        }
    }
    
    private DependencyObject GetDescendantByName(DependencyObject parent, string name)
    {
        if (parent == null)
            return null;
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child is FrameworkElement)
            {
                if ((child as FrameworkElement).Name == name)
                return child;
            }
            if (child is FrameworkContentElement)
            {
                if ((child as FrameworkContentElement).Name == name)
                    return child;
            }
            child = GetDescendantByName(child, name);
            if (child != null)
                return child;
        }
        return null;
    }
    

Continue to Deploy and Test Your Support for Test Object Operations.