Tuesday, 30 April 2019

Win/Control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Automation;
using System.Windows.Forms;

namespace CoreWinLibraries
{
    public abstract class Control
    {
        public Control()
        {

        }

        private void SetValue(AutomationElement element, string value)
        {



        }
        private string GetValue(AutomationElement element)
        {
            string result = string.Empty;
            return result;
        }
        private void Click(AutomationElement element)
        {

        }
        //public bool Exists(AutomationElement element)
        //{
        //   bool result =  Exists(aeParent,aeChild);
        //  return result;
        //}

        public static AutomationElement FindElement(AutomationElement aeParent, string locator)
        {
            AutomationElement aeChild = null;
            string searchType = locator.Split(':')[0];
            string sPropertyValue = locator.Split(':')[1];
            switch (searchType)
            {
                case "Id":
                    {
                        aeChild = Wait.UntilResponsive(aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, sPropertyValue)),TimeSpan.FromSeconds(10));
                        //aeChild = aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, sPropertyValue));
                    }
                    break;
                case "Name":
                    {
                       aeChild =  Wait.UntilResponsive(aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, sPropertyValue)),TimeSpan.FromSeconds(10));
                       
                       //aeChild = aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, sPropertyValue));
                    }
                    break;
                case "ClassName":
                    {
                        aeChild = Wait.UntilResponsive(aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, sPropertyValue)),TimeSpan.FromSeconds(10));
                       
                        //aeChild = aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, sPropertyValue));
                    }
                    break;
                case "ControlType":
                    {
                        aeChild = Wait.UntilResponsive(aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, sPropertyValue)),TimeSpan.FromSeconds(10));
                       
                        //aeChild = aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, sPropertyValue));
                    }
                    break;
                case "LocalizedControlType":
                    {
                        System.Threading.Thread.Sleep(500);
                        aeChild = Wait.UntilResponsive(aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, sPropertyValue)), TimeSpan.FromSeconds(30));

                        //aeChild = aeParent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, sPropertyValue));
                    }
                    break;
                default:
                    break;
            }
            return aeChild;

        }

        private static AutomationElement GetChildren(AutomationElement parent, string sAutomationElementValue)
        {
            if (parent == null)
            {
                throw new ArgumentException();
            }
            AutomationElementCollection collection = parent.FindAll(TreeScope.Children, Condition.TrueCondition);
            if (collection != null)
            {
                //Cast AutomationElementCollection into
                //AutomationElement List
                List<AutomationElement> aeList = new List<AutomationElement>(collection.Cast<AutomationElement>());
                AutomationElement aeSelected = aeList.FirstOrDefault(i => i.Current.Name.Contains(sAutomationElementValue));
                if (aeSelected != null)
                {
                    return aeSelected;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                // some error occured
                return null;
            }
        }

        /// <summary>
        /// Retrieves the top-level window that contains the specified UI Automation element.
        /// </summary>
        /// <param name="element">The contained element.</param>
        /// <returns>The containing top-level window element.</returns>
        private AutomationElement GetTopLevelWindow(AutomationElement element)
        {
            TreeWalker walker = TreeWalker.ControlViewWalker;
            AutomationElement elementParent;
            AutomationElement node = element;
            do
            {
                elementParent = walker.GetParent(node);
                if (elementParent == AutomationElement.RootElement)
                {
                    break;
                }

                node = elementParent;
            }
            while (true);
            return node;
        }

        /// <summary>
        /// Walks the UI Automation tree and adds the control type of each enabled control
        /// element it finds to a TreeView.
        /// </summary>
        /// <param name="rootElement">The root of the search on this iteration.</param>
        /// <param name="treeNode">The node in the TreeView for this iteration.</param>
        /// <remarks>
        /// This is a recursive function that maps out the structure of the subtree beginning at the
        /// UI Automation element passed in as rootElement on the first call. This could be, for example,
        /// an application window.
        /// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
        /// the desktop could take a very long time and even lead to a stack overflow.
        /// </remarks>
        private void WalkEnabledElements(AutomationElement rootElement, TreeNode treeNode)
        {
            Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
            Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
            TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
            AutomationElement elementNode = walker.GetFirstChild(rootElement);
            while (elementNode != null)
            {
                TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);
                WalkEnabledElements(elementNode, childTreeNode);
                elementNode = walker.GetNextSibling(elementNode);
            }
        }

         /// <summary>
        ///
        /// </summary>
        /// <param name="aeTargetted"></param>
        /// <returns></returns>
        public static AutomationPattern[] GetSupportedPattern(AutomationElement aeTargetted)
        {
            List<AutomationPattern> lAutomationPattern = new List<AutomationPattern>();
            return aeTargetted.GetSupportedPatterns();

        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="aeTargetted"></param>
        /// <returns></returns>
        public static AutomationProperty[] GetSupportedProperties(AutomationElement aeTargetted)
        {
            List<AutomationProperty> lAutomationPattern = new List<AutomationProperty>();
            return aeTargetted.GetSupportedProperties();
           
        }
    }

}

No comments: