Beispiel #1
0
 public Project()
 {
     TestsFolder = "Tests";
     LogsFolder = "Logs";
     AppManagerFolder = "AppManager";
     AppManager = new AppManager();
 }
        public static MappedItem GetMappedItemFromUIItem(IUIItem uiItem, AppManager appManager)
        {
            try
            {
                if (uiItem.AutomationElement.Current.LocalizedControlType.Equals("window"))
                {
                    Process process1 = Process.GetProcessById(uiItem.AutomationElement.Current.ProcessId);

                    AppProcess appProcess1 = appManager.FindOrCreateProcess(process1.ProcessName);

                    string name = uiItem.AutomationElement.Current.AutomationId;

                    int num;
                    if (int.TryParse(name, out num))
                        name = "";

                    string type = uiItem.AutomationElement.Current.ControlType.LocalizedControlType;
                    string text = uiItem.AutomationElement.Current.Name;

                    if (type == "edit")
                        text = "";

                    Rect bounds = uiItem.AutomationElement.Current.BoundingRectangle;
                    bounds.X = 0;//;window.Current.BoundingRectangle.X;
                    bounds.Y = 0;//window.Current.BoundingRectangle.Y;

                    return appManager.FindOrCreateMappedItem(appProcess1.Id, name, bounds, type, text);

                }

                TreeWalker walker = TreeWalker.ControlViewWalker;
                AutomationElement automationElement = uiItem.AutomationElement;
                Stack<AutomationElement> uiElementTree = new Stack<AutomationElement>();

                int delNum;

                while (automationElement != AutomationElement.RootElement)
                {
                    if (!((string.IsNullOrEmpty(automationElement.Current.Name)
                         && string.IsNullOrEmpty(automationElement.Current.AutomationId))
                         || int.TryParse(automationElement.Current.AutomationId, out delNum)))
                    {
                        uiElementTree.Push(automationElement);
                    }

                    automationElement = walker.GetParent(automationElement);
                }

                Process process = Process.GetProcessById(uiItem.AutomationElement.Current.ProcessId);

                AppProcess appProcess = appManager.FindOrCreateProcess(process.ProcessName);

                string parentId = appProcess.Id;

                AutomationElement window = uiElementTree.Peek();
                MappedItem createdMappedItem = null;

                /*if (endurProcesses.Contains(appProcess.Name))
                {
                    automationElement = uiElementTree.Pop();
                    string name = automationElement.Current.AutomationId;
                    string type = automationElement.Current.ControlType.LocalizedControlType;
                    string text = automationElement.Current.Name;
                    Rect bounds = automationElement.Current.BoundingRectangle;
                    bounds.X -= window.Current.BoundingRectangle.X;
                    bounds.Y -= window.Current.BoundingRectangle.Y;
                    createdMappedItem = appManager.FindOrCreateMappedItem(parentId, name, bounds, type, text);

                    mappedItem = createdMappedItem;

                    return new UIItem(window, new NullActionListener());
                }*/

                while (uiElementTree.Count > 0)
                {
                    automationElement = uiElementTree.Pop();
                    string name = automationElement.Current.AutomationId;

                    int num;
                    if (int.TryParse(name, out num))
                        name = "";

                    string type = automationElement.Current.ControlType.LocalizedControlType;
                    string text = automationElement.Current.Name;

                    if (type == "edit")
                        text = "";

                    Rect bounds = automationElement.Current.BoundingRectangle;
                    bounds.X -= window.Current.BoundingRectangle.X;
                    bounds.Y -= window.Current.BoundingRectangle.Y;
                    createdMappedItem = appManager.FindOrCreateMappedItem(parentId, name, bounds, type, text);

                    parentId = createdMappedItem.Id;
                }

                return createdMappedItem;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static IUIItem GetControl(AppProcess process, MappedItem window, MappedItem control, AppManager appManager)
        {
            Process wProcess = Process.GetProcessesByName(process.Name).First();
            Application application = Application.Attach(wProcess);

            Window appWindow = null;

            if (control.Type == "pane")
            {

            }

            /*IEnumerable<string> enumerable = application.GetWindows().Select(w => w.Title);
            IEnumerable<string> list2 = application.GetWindows().Select(w => w.AutomationElement.Current.Name);
            */
            List<string> windowTitles2 = GetWindowsForProcess(process.Name);

            if (!window.Name.IsNullOrEmpty())
                appWindow = application.GetWindow(SearchCriteria.ByAutomationId(window.Name), InitializeOption.NoCache);
            else
            {
                for (int i = 0; i < 5; i++)
                {
                    List<string> windowTitles = GetWindowsForProcess(process.Name);
                    string windowName = window.Text;
                    string closestTitle = windowTitles[0];
                    decimal toleranceLevel = windowName.Length * 0.7m;

                    foreach (string windowTitle in windowTitles)
                    {
                        int num1 = LevenshteinDistance.GetToleranceLevel(windowTitle, windowName);
                        if (num1 < toleranceLevel)
                        {
                            toleranceLevel = num1;
                            closestTitle = windowTitle;
                        }
                    }

                    try
                    {
                        List<Window> windows = application.GetWindows();

                        appWindow = application.GetWindows().FirstOrDefault(w => w.AutomationElement.Current.Name.Equals(closestTitle));

                        if (appWindow == null)
                        {
                            List<IntPtr> handles = EnumerateProcessWindowHandles(wProcess.Id).ToList();

                            Dictionary<string, IntPtr> windowHandleDict = new Dictionary<string, IntPtr>();

                            StringBuilder builder = new StringBuilder(1024);

                            foreach (IntPtr myIntPtr in handles)
                            {
                                GetWindowText(myIntPtr, builder, 1024);

                                string windowTitle = builder.ToString();
                                windowHandleDict[windowTitle] = myIntPtr;
                            }

                            AutomationElement element = AutomationElement.FromHandle(windowHandleDict[closestTitle]);

                            appWindow = new Win32Window(element, WindowFactory.Desktop, InitializeOption.NoCache,
                                new NullWindowSession());
                        }

                        break;
                    }
                    catch (Exception ex)
                    {

                    }

                }
            }

            BringWindowToFront(appWindow.AutomationElement.Current.NativeWindowHandle);

            if (control == null || control.Type == "window" || control.Equals(window))
                return appWindow;

            Stack<MappedItem> mappedItemTree = new Stack<MappedItem>();
            MappedItem currentMappedItem = control;

            if (window == null)
            {

            }

            while (currentMappedItem != window)
            {
                mappedItemTree.Push(currentMappedItem);
                currentMappedItem = appManager.GetMappedItem(currentMappedItem.ParentId);
            }

            IUIItem currentControl = appWindow;
            AutomationElement elemn;

            while (mappedItemTree.Count > 0)
            {
                MappedItem currentMappedItemControl = mappedItemTree.Pop();

                if (!currentMappedItemControl.Name.IsNullOrEmpty())
                {
                    AutomationElement element = currentControl.AutomationElement.FindFirst(TreeScope.Descendants,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, currentMappedItemControl.Name));

                    //AutomationElement element = currentControl.GetElement(SearchCriteria.ByAutomationId(currentMappedItemControl.Name));

                    currentControl = new UIItem(element, new NullActionListener());

                    continue;
                }

                if (!string.IsNullOrEmpty(currentMappedItemControl.Text))
                {
                    currentControl = new UIItem(currentControl.GetElement(SearchCriteria.ByText(currentMappedItemControl.Text)),
                        new NullActionListener());

                    continue;
                }

            }

            return currentControl;
        }