Click here to Skip to main content
15,886,422 members
Articles / DevOps / Testing

Automation Enabler PlugIn - Exposes Inaccessible Elements to UI Automation Framework

Rate me:
Please Sign up or sign in to vote.
4.68/5 (7 votes)
20 Jun 2011CPOL7 min read 36.5K   921   24  
A reusable plug-in framework which exposes inaccessible elements to UI Automation framework. A must for .NET 2.0 WinForm container controls
using System;
using System.Collections.Generic;
using System.Windows.Automation;
using System.Windows.Automation.Provider;

namespace AutomationFramework
{
    /// <summary>
    /// A basic implementation of IRawElementSimpleProvider.
    /// Many customizations can be done by overriding virtual methods of this class,
    /// rather than having to implement the whole interface.
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public abstract class ItemProvider : IRawElementProviderSimple
    {
        #region Fields

        private Dictionary<int, object> myAutomationProperties = new Dictionary<int, object>();

        #endregion

        #region Protected Virtual Methods

        /// <summary>
        /// Get the window handle for a provider that is a full HWND
        /// </summary>
        /// <returns></returns>
        protected virtual IntPtr GetWindowHandle()
        {
            return IntPtr.Zero;
        }

        /// <summary>
        /// Get the localized name for this control
        /// </summary>
        /// <returns></returns>
        protected virtual string GetName()
        {
            return null;
        }

        #endregion

        #region Other Protected Methods

        /// <summary>
        /// Add the Automation Properties.
        /// </summary>
        /// <param name="propertyId"></param>
        /// <param name="value"></param>
        protected void AddAutomationProperty(int propertyId, object value)
        {
            this.myAutomationProperties.Add(propertyId, value);
        }

        #endregion

        #region IRawElementProviderSimple Members

        /// <summary>
        /// Retrieves an object that provides support for a control pattern on a UI Automation Element.
        /// </summary>
        /// <param name="patternId"></param>
        /// <returns></returns>
        public virtual object GetPatternProvider(int patternId)
        {
            return null;
        }

        /// <summary>
        /// Retrieves the value of a property supported by the UI Automation provider.
        /// </summary>
        /// <param name="propertyId"></param>
        /// <returns></returns>
        public virtual object GetPropertyValue(int propertyId)
        {
            // Check the static props list first
            if (this.myAutomationProperties.ContainsKey(propertyId))
            {
                return this.myAutomationProperties[propertyId];
            }

            // Switching construct to go get the right property from a virtual method.
            if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
            {
                return GetName();
            }

            // Add further cases here to support more properties. :)
            return null;
        }

        /// <summary>
        /// Gets a base provider for this element.
        /// </summary>
        public IRawElementProviderSimple HostRawElementProvider
        {
            get
            {
                IntPtr hwnd = this.GetWindowHandle();
                if (hwnd != IntPtr.Zero)
                {
                    return AutomationInteropProvider.HostProviderFromHandle(this.GetWindowHandle());
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Specify the type of UI Automation provider.
        /// </summary>
        public virtual ProviderOptions ProviderOptions
        {
            get { return ProviderOptions.ServerSideProvider; }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Philips
India India
Have been working with computers since the early 00's. Since then I've been building, fixing, configuring, installing, coding and designing with them. At present I mainly code windows applications in C#, WCF, WPF and SQL. I'm very interested in Design Patterns and try and use these generic principles in all new projects to create truly n-tier architectures. Also I like to code for making the User Interface very attractive...

Comments and Discussions