/// <inheritdoc />
        public override bool MoveToFirstChild()
        {
            if (IsInAttribute)
            {
                return(false);
            }
            var childElement = _treeWalker.GetFirstChild(_currentElement);

            if (childElement == null)
            {
                return(false);
            }
            _currentElement = childElement;
            return(true);
        }
예제 #2
0
        public override void AddSubElements()
        {
            var elementNode = _treeWalker.GetFirstChild(RawElement);

            while (elementNode != null)
            {
                Children.Add(new WindowsTreeElement(this, false, automation, elementNode, _treeWalker));
                try
                {
                    elementNode = _treeWalker.GetNextSibling(elementNode);
                }
                catch (Exception)
                {
                    elementNode = null;
                }
            }
        }
예제 #3
0
        public AutomationElement[] matches(AutomationBase automation, AutomationElement element, ITreeWalker _treeWalker, int count)
        {
            var matchs = new List <AutomationElement>();
            var c      = GetConditionsWithoutStar();

            Log.Selector("matches::FindAllChildren");
            //var elements = element.FindAllChildren(c);
            //foreach (var elementNode in elements)
            //{
            //    Log.Selector("matches::match");
            //    if (match(elementNode)) matchs.Add(elementNode);
            //}
            var nodes       = new List <AutomationElement>();
            var elementNode = _treeWalker.GetFirstChild(element);
            var i           = 0;

            while (elementNode != null)
            {
                nodes.Add(elementNode);
                i++;
                // Console.WriteLine(i + ") " + elementNode.ToString());
                if (Match(elementNode))
                {
                    matchs.Add(elementNode);
                }
                if (matchs.Count >= count)
                {
                    break;
                }
                elementNode = _treeWalker.GetNextSibling(elementNode);
            }
            //foreach (var _elementNode in nodes)
            //{
            //    if (match(_elementNode)) matchs.Add(_elementNode);
            //}
            return(matchs.ToArray());
        }
예제 #4
0
        public static UIElement[] GetElementsWithuiSelectorItem(int ident, AutomationBase automation, WindowsSelectorItem sel, UIElement[] parents, int maxresults, bool LastItem, bool search_descendants)
        {
            var _current = new List <UIElement>();
            var cond     = sel.GetConditionsWithoutStar();

            foreach (var _ele in parents)
            {
                if (PluginConfig.enable_cache && cond.ChildCount > 0)
                {
                    var cache = WindowsSelectorItem.GetFromCache(_ele.RawElement, ident, cond.ToString());
                    if (cache != null)
                    {
                        Log.Debug("GetElementsWithuiSelector: found in AppWindowCache " + cond.ToString());
                        foreach (var elementNode in cache)
                        {
                            if (WindowsSelectorItem.Match(sel, elementNode))
                            {
                                _current.Add(new UIElement(elementNode));
                            }
                        }
                    }
                }
                if (!string.IsNullOrEmpty(sel.processname()))
                {
                    if (!string.IsNullOrEmpty(sel.processname()) && (sel.processname().ToLower() != "startmenuexperiencehost" && sel.processname().ToLower() != "explorer"))
                    {
                        var me = System.Diagnostics.Process.GetCurrentProcess();
                        var ps = System.Diagnostics.Process.GetProcessesByName(sel.processname()).Where(_p => _p.SessionId == me.SessionId).ToArray();
                        if (ps.Length == 0)
                        {
                            Log.Selector(string.Format("GetElementsWithuiSelector::Process " + sel.processname() + " not found, end with 0 results"));
                            return(new UIElement[] { });
                        }
                        var psids = ps.Select(x => x.Id).ToArray();

                        var condition = new FlaUI.Core.Conditions.ConditionFactory(automation.PropertyLibrary);
                        var ors       = new List <FlaUI.Core.Conditions.ConditionBase>();
                        foreach (var p in ps)
                        {
                            ors.Add(condition.ByProcessId(p.Id));
                        }
                        var con = new FlaUI.Core.Conditions.OrCondition(ors);
                        if (PluginConfig.enable_cache && con.ChildCount > 0)
                        {
                            var cache = WindowsSelectorItem.GetFromCache(_ele.RawElement, ident, con.ToString());
                            if (cache != null)
                            {
                                Log.Debug("GetElementsWithuiSelector: found in AppWindowCache " + con.ToString());
                                foreach (var elementNode in cache)
                                {
                                    if (WindowsSelectorItem.Match(sel, elementNode))
                                    {
                                        _current.Add(new UIElement(elementNode));
                                    }
                                }
                            }
                        }

                        if (_current.Count == 0)
                        {
                            Log.Debug(string.Format("GetElementsWithuiSelector::Searchin for all " + con.ToString()));
                            // var ___treeWalker = automation.TreeWalkerFactory.GetCustomTreeWalker(con);
                            var ___treeWalker     = automation.TreeWalkerFactory.GetControlViewWalker();
                            int retries           = 0;
                            AutomationElement win = null;
                            bool hasError         = false;
                            do
                            {
                                hasError = false;
                                try
                                {
                                    win = ___treeWalker.GetFirstChild(_ele.RawElement);
                                }
                                catch (Exception ex)
                                {
                                    Log.Debug(ex.ToString());
                                    retries++;
                                    hasError = true;
                                    // throw;
                                }
                            } while (hasError && retries < 10);

                            while (win != null)
                            {
                                bool addit = false;
                                if (win.Properties.ProcessId.IsSupported && psids.Contains(win.Properties.ProcessId))
                                {
                                    addit = true;
                                }
                                if (addit)
                                {
                                    var uiele = new UIElement(win);
                                    Log.Debug(string.Format("GetElementsWithuiSelector::Adding element " + uiele.ToString()));
                                    _current.Add(uiele);
                                    if (win.Patterns.Window.TryGetPattern(out var winPattern))
                                    {
                                        if (winPattern.WindowVisualState.Value == FlaUI.Core.Definitions.WindowVisualState.Minimized)
                                        {
                                            IntPtr handle = win.Properties.NativeWindowHandle.Value;
                                            winPattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
                                        }
                                    }
                                    if (PluginConfig.allow_multiple_hits_mid_selector || ident == 0) // do all
                                    {
                                        win = ___treeWalker.GetNextSibling(win);
                                    }
                                    else
                                    {
                                        win = null;
                                    }
                                }
                                else
                                {
                                    win = ___treeWalker.GetNextSibling(win);
                                }
                            }
                            if (_current.Count > 0 && PluginConfig.enable_cache && con.ChildCount > 0)
                            {
                                var elements = _current.Select(x => x.RawElement).ToArray();
                                WindowsSelectorItem.AddToCache(_ele.RawElement, ident, con.ToString(), elements);
                            }
                        }
                    }
                    return(_current.ToArray());
                }
                if (_current.Count == 0 && string.IsNullOrEmpty(sel.Selector))
                {
                    Log.Debug("GetElementsWithuiSelector::Searchin for " + cond.ToString());
                    ITreeWalker _treeWalker = default(ITreeWalker);
                    if (sel.search_descendants || search_descendants)
                    {
                        var hasStar = sel.Properties.Where(x => x.Enabled == true && (x.Value != null && x.Value.Contains("*"))).ToArray();
                        _treeWalker = automation.TreeWalkerFactory.GetCustomTreeWalker(cond);
                    }
                    else
                    {
                        _treeWalker = automation.TreeWalkerFactory.GetControlViewWalker();
                    }
                    int retries           = 0;
                    AutomationElement ele = null;
                    bool hasError         = false;
                    do
                    {
                        hasError = false;
                        try
                        {
                            ele = _treeWalker.GetFirstChild(_ele.RawElement);
                        }
                        catch (Exception ex)
                        {
                            Log.Debug(ex.ToString());
                            retries++;
                            hasError = true;
                            // throw;
                        }
                    } while (hasError && retries < 10);


                    if (ele != null)
                    {
                        do
                        {
                            // Log.Debug(string.Format("GetElementsWithuiSelector::Match element {0:mm\\:ss\\.fff}", sw.Elapsed));
                            if (WindowsSelectorItem.Match(sel, ele))
                            {
                                var uiele = new UIElement(ele);
                                Log.Debug(string.Format("GetElementsWithuiSelector::Adding element " + uiele.ToString()));
                                _current.Add(uiele);
                            }

                            bool getmore = false;
                            if (LastItem)
                            {
                                if (_current.Count < maxresults)
                                {
                                    getmore = true;
                                }
                            }
                            else
                            {
                                if (_current.Count == 0)
                                {
                                    getmore = true;
                                }
                                else if (PluginConfig.allow_multiple_hits_mid_selector)
                                {
                                    getmore = true;
                                }
                            }
                            if (getmore)
                            {
                                ele = _treeWalker.GetNextSibling(ele);
                            }
                            if (!getmore)
                            {
                                ele = null;
                            }
                        } while (ele != null);
                    }
                    if (_current.Count > 0 && PluginConfig.enable_cache && cond.ChildCount > 0)
                    {
                        var elements = _current.Select(x => x.RawElement).ToArray();
                        WindowsSelectorItem.AddToCache(_ele.RawElement, ident, cond.ToString(), elements);
                    }
                }
            }
            return(_current.ToArray());
        }
예제 #5
0
        public AutomationElement[] matches(AutomationBase automation, AutomationElement element, ITreeWalker _treeWalker, int count, bool isDesktop, TimeSpan timeout, bool search_descendants)
        {
            var matchs = new List <AutomationElement>();
            var c      = GetConditionsWithoutStar();

            if (isDesktop)
            {
                if (AppWindowCache.ContainsKey(c.ToString()))
                {
                    var _element = AppWindowCache[c.ToString()];
                    try
                    {
                        if (_element.Properties.IsOffscreen.IsSupported && !_element.IsOffscreen)
                        {
                            if (Match(_element))
                            {
                                Log.Selector("matches::FindAllChildren: found in AppWindowCache");
                                return(new AutomationElement[] { _element });
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Log.SelectorVerbose("matches::FindAllChildren: Removing from AppWindowCache " + c.ToString());
                        AppWindowCache.Remove(c.ToString());
                    }
                }
            }
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            Log.SelectorVerbose("matches::FindAllChildren.isDesktop(" + isDesktop + ")::begin");
            //if (isDesktop) // To slow !
            //{
            //    Log.Selector(string.Format("AutomationElement.matches.isDesktop::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            //    Log.SelectorVerbose("Searching desktop for " + c.ToString());
            //    var elements = element.FindAllChildren(c);
            //    Log.SelectorVerbose("Done Search");
            //    Log.Selector(string.Format("AutomationElement.matches.isDesktop::process elements {0:mm\\:ss\\.fff}", sw.Elapsed));
            //    // var elements = element.FindAllChildren();
            //    foreach (var elementNode in elements)
            //    {
            //        Log.SelectorVerbose("matches::match");
            //        if (Match(elementNode)) matchs.Add(elementNode);
            //    }
            //    Log.Selector(string.Format("AutomationElement.matches.isDesktop::complete {0:mm\\:ss\\.fff}", sw.Elapsed));
            //}
            //else
            //{
            //    var nodes = new List<AutomationElement>();
            //    Log.Selector(string.Format("AutomationElement.matches.isNotDesktop::GetFirstChild {0:mm\\:ss\\.fff}", sw.Elapsed));
            //    var elementNode = _treeWalker.GetFirstChild(element);
            //    var i = 0;
            //    while (elementNode != null)
            //    {
            //        nodes.Add(elementNode);
            //        i++;
            //        if (Match(elementNode)) matchs.Add(elementNode);
            //        if (matchs.Count >= count) break;
            //        Log.Selector(string.Format("AutomationElement.matches.isNotDesktop::GetNextSibling {0:mm\\:ss\\.fff}", sw.Elapsed));
            //        elementNode = _treeWalker.GetNextSibling(elementNode);
            //    }
            //}



            //if (isDesktop) // To slow !
            //{
            //    Log.Selector(string.Format("AutomationElement.matches.isDesktop::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            //    var windows = Win32WindowUtils.GetTopLevelWindows(automation);
            //    foreach (var elementNode in windows)
            //    {
            //        Log.SelectorVerbose("matches::match");
            //        if (Match(elementNode)) matchs.Add(elementNode);
            //    }
            //    Log.Selector(string.Format("AutomationElement.matches.isDesktop::complete {0:mm\\:ss\\.fff}", sw.Elapsed));
            //}
            //else
            //{
            //    var nodes = new List<AutomationElement>();
            //    Log.Selector(string.Format("AutomationElement.matches.isNotDesktop::GetFirstChild {0:mm\\:ss\\.fff}", sw.Elapsed));
            //    var elementNode = _treeWalker.GetFirstChild(element);
            //    var i = 0;
            //    while (elementNode != null)
            //    {
            //        nodes.Add(elementNode);
            //        i++;
            //        if (Match(elementNode)) matchs.Add(elementNode);
            //        if (matchs.Count >= count) break;
            //        Log.Selector(string.Format("AutomationElement.matches.isNotDesktop::GetNextSibling {0:mm\\:ss\\.fff}", sw.Elapsed));
            //        elementNode = _treeWalker.GetNextSibling(elementNode);
            //    }
            //}


            if (search_descendants)
            {
                Log.Selector("AutomationElement.matches: Searching for " + c.ToString());
                AutomationElement[] elements = null;
                if (isDesktop)
                {
                    elements = element.FindAllChildren(c);
                }
                else
                {
                    elements = element.FindAllDescendants(c);
                }
                Log.Selector(string.Format("AutomationElement.matches::found " + elements.Count() + " elements {0:mm\\:ss\\.fff}", sw.Elapsed));
                // var elements = element.FindAllChildren();
                foreach (var elementNode in elements)
                {
                    Log.SelectorVerbose("matches::match");
                    if (Match(elementNode))
                    {
                        matchs.Add(elementNode);
                    }
                }
                Log.Selector(string.Format("AutomationElement.matches::complete, with " + elements.Count() + " elements {0:mm\\:ss\\.fff}", sw.Elapsed));
            }
            else
            {
                System.Threading.ManualResetEvent syncEvent = new System.Threading.ManualResetEvent(false);
                Action action = () =>
                {
                    var nodes = new List <AutomationElement>();
                    Log.Selector(string.Format("AutomationElement.matches.isDesktop(" + isDesktop + ")::GetFirstChild {0:mm\\:ss\\.fff}", sw.Elapsed));
                    var elementNode = _treeWalker.GetFirstChild(element);
                    var i           = 0;
                    while (elementNode != null)
                    {
                        nodes.Add(elementNode);
                        i++;
                        if (Match(elementNode))
                        {
                            matchs.Add(elementNode);
                        }
                        if (matchs.Count >= count)
                        {
                            break;
                        }
                        Log.Selector(string.Format("AutomationElement.matches.isDesktop(" + isDesktop + ")::GetNextSibling {0:mm\\:ss\\.fff}", sw.Elapsed));
                        elementNode = _treeWalker.GetNextSibling(elementNode);
                    }
                    if (syncEvent != null)
                    {
                        syncEvent.Set();
                    }
                };
                // if (isDesktop && PluginConfig.get_elements_in_different_thread)
                if (PluginConfig.get_elements_in_different_thread)
                {
                    //Task.Run(action);
                    //syncEvent.WaitOne(timeout, true);
                    //if(matchs.Count > 0)
                    //{
                    //    matchs.Clear();
                    action();
                    //}
                }
                else
                {
                    action();
                }
            }
            Log.SelectorVerbose(string.Format("matches::FindAllChildren.isDesktop(" + isDesktop + ")::complete {0:mm\\:ss\\.fff}", sw.Elapsed));
            if (isDesktop && matchs.Count == 1)
            {
                if (matchs[0].Properties.IsOffscreen.IsSupported)
                {
                    if (!AppWindowCache.ContainsKey(c.ToString()))
                    {
                        AppWindowCache.Add(c.ToString(), matchs[0]);
                    }
                    else
                    {
                        AppWindowCache[c.ToString()] = matchs[0];
                    }
                }
            }
            return(matchs.ToArray());
        }