Ejemplo n.º 1
0
        public static UIElement[] GetElementsWithuiSelector(WindowsSelector selector, IElement fromElement, int maxresults, WindowsCacheExtension ext)
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(5000);

            timeout = TimeSpan.FromMilliseconds(20000);
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            Log.Selector(string.Format("GetElementsWithuiSelector::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            UIElement _fromElement = fromElement as UIElement;
            // var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
            var selectors = selector.ToList();
            AutomationElement startfrom = null;

            if (_fromElement != null)
            {
                startfrom = _fromElement.RawElement;
            }


            var _current = new List <UIElement>();
            // var automation = AutomationUtil.getAutomation();
            AutomationBase automation = null;

            if (ext != null)
            {
                automation = ext.automation;
            }
            if (automation == null)
            {
                automation = AutomationUtil.getAutomation();
            }

            UIElement[] result = null;
            // AutomationElement ele = null;

            bool search_descendants = PluginConfig.search_descendants;
            var  v = selectors[0].Properties.Where(x => x.Name == "search_descendants").FirstOrDefault();

            if (v == null)
            {
                selectors[0].Properties.Where(x => x.Name == "SearchDescendants").FirstOrDefault();
            }
            if (v != null)
            {
                search_descendants = bool.Parse(v.Value);
            }

            if (startfrom == null)
            {
                startfrom = automation.GetDesktop();
            }
            _current.Add(new UIElement(startfrom));
            for (var i = 0; i < selectors.Count; i++)
            {
                var _sel    = selectors[i];
                var sel     = new WindowsSelectorItem(_sel);
                var current = _current.ToArray();
                _current.Clear();
                // if(i == 1 && current.Length == 1 && current.First().ControlType == sel.ControlType)
                if (i == 1)
                {
                    foreach (var e in current)
                    {
                        if (WindowsSelectorItem.Match(sel, e.RawElement))
                        {
                            _current.Add(e);
                        }
                    }
                    if (_current.Count > 0)
                    {
                        continue;
                    }
                    //_current = GetElementsWithuiSelectorItem(automation, sel, current, maxresults, i == (selectors.Count - 1)).ToList();
                    //if(_current.Count == 0) _current = current.ToList();
                    //_current = current.ToList();
                }
                _current = GetElementsWithuiSelectorItem(i, automation, sel, current, maxresults, i == (selectors.Count - 1), search_descendants).ToList();
                if (i == 0 && _current.Count == 0)
                {
                    _current = current.ToList();
                }
            }
            Log.Debug(string.Format("GetElementsWithuiSelector::completed with " + _current.Count + " results {0:mm\\:ss\\.fff}", sw.Elapsed));
            if (_current.Count > 0)
            {
                result = _current.ToArray();
                if (result.Count() > maxresults)
                {
                    Console.WriteLine("found " + result.Count() + " but only needed " + maxresults);
                    result = result.Take(maxresults).ToArray();
                }
                return(result);
            }
            if (result == null)
            {
                Log.Selector(string.Format("GetElementsWithuiSelector::ended with 0 results after {0:mm\\:ss\\.fff}", sw.Elapsed));
                return(new UIElement[] { });
            }
            if (result.Count() > maxresults)
            {
                result = result.Take(maxresults).ToArray();
            }
            Log.Selector(string.Format("GetElementsWithuiSelector::ended with " + result.Length + " results after {0:mm\\:ss\\.fff}", sw.Elapsed));
            return(result);
        }
Ejemplo n.º 2
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var result            = new List <UIElement>();
            var includeHidden     = false;
            var includeEmptyTitle = false;

            try
            {
                if (IncludeEmptyTitle != null)
                {
                    includeEmptyTitle = IncludeEmptyTitle.Get(context);
                }
                if (IncludeHidden != null)
                {
                    includeHidden = IncludeHidden.Get(context);
                }
            }
            catch (Exception)
            {
            }
            var windows = RuningWindows.GetOpenedWindows(includeHidden, includeEmptyTitle);

            using (var automation = AutomationUtil.getAutomation())
            {
                foreach (var window in windows)
                {
                    var _window = automation.FromHandle(window.Key);
                    result.Add(new UIElement(_window));
                }
            }
            WindowsCacheExtension ext = context.GetExtension <WindowsCacheExtension>();
            var sw = new Stopwatch();

            sw.Start();
            Log.Selector(string.Format("Windows.GetWindows::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            UIElement[] elements = result.ToArray();
            context.SetValue(Elements, elements);

            var lastelements = context.GetValue(_lastelements);

            if (lastelements == null)
            {
                lastelements = new UIElement[] { }
            }
            ;
            context.SetValue(_lastelements, elements);
            IEnumerator <UIElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            if (lastelements.Length == elements.Length && lastelements.Length > 0)
            {
                more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            }
            if (more)
            {
                context.SetValue(_elements, _enum);
                context.SetValue(_sw, sw);
                Log.Selector(string.Format("Windows.GetWindows::end:: call ScheduleAction: {0:mm\\:ss\\.fff}", sw.Elapsed));
                IncIndex(context);
                SetTotal(context, elements.Length);
                context.ScheduleAction <UIElement>(Body, _enum.Current, OnBodyComplete);
            }
            else
            {
                Log.Selector(string.Format("Windows.GetWindows:end {0:mm\\:ss\\.fff}", sw.Elapsed));
            }
        }
Ejemplo n.º 3
0
        protected override void StartLoop(NativeActivityContext context)
        {
            WindowsCacheExtension ext = context.GetExtension <WindowsCacheExtension>();
            var sw = new Stopwatch();

            sw.Start();
            Log.Selector(string.Format("Windows.GetElement::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            UIElement[] elements = null;
            var         selector = Selector.Get(context);

            selector = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(selector, context.DataContext);
            var sel        = new WindowsSelector(selector);
            var timeout    = Timeout.Get(context);
            var maxresults = MaxResults.Get(context);
            var minresults = MinResults.Get(context);

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            var interactive = Interactive.Get(context);
            var from        = From.Get(context);
            int failcounter = 0;

            do
            {
                if (ClearCache != null && ClearCache.Get(context))
                {
                    Log.Selector(string.Format("Windows.GetElement::Clearing windows element cache {0:mm\\:ss\\.fff}", sw.Elapsed));
                    WindowsSelectorItem.ClearCache();
                }
                if (PluginConfig.get_elements_in_different_thread)
                {
                    elements = OpenRPA.AutomationHelper.RunSTAThread <UIElement[]>(() =>
                    {
                        try
                        {
                            Log.Selector(string.Format("Windows.GetElement::GetElementsWithuiSelector in non UI thread {0:mm\\:ss\\.fff}", sw.Elapsed));
                            return(WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext));
                        }
                        catch (System.Threading.ThreadAbortException)
                        {
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.ToString());
                        }
                        return(new UIElement[] { });
                    }, PluginConfig.search_timeout).Result;
                }
                else
                {
                    Log.Selector(string.Format("Windows.GetElement::GetElementsWithuiSelector using UI thread {0:mm\\:ss\\.fff}", sw.Elapsed));
                    elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
                    if (elements == null || elements.Length == 0)
                    {
                        elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
                    }
                }
                //elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults);
                if (elements == null)
                {
                    elements = new UIElement[] { };
                }
                if (elements.Length == 0)
                {
                    Log.Selector(string.Format("Windows.GetElement::Found no elements {0:mm\\:ss\\.fff}", sw.Elapsed));
                    failcounter++;
                }
                if (failcounter > 2)
                {
                    WindowsSelectorItem.ClearCache();
                }
            } while (elements != null && elements.Length == 0 && sw.Elapsed < timeout);
            if (PluginConfig.get_elements_in_different_thread && elements.Length > 0)
            {
                // Get them again, we need the COM objects to be loaded in the UI thread
                elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
            }
            context.SetValue(Elements, elements);

            var lastelements = context.GetValue(_lastelements);

            if (lastelements == null)
            {
                lastelements = new UIElement[] { }
            }
            ;
            context.SetValue(_lastelements, elements);
            if ((elements.Length + lastelements.Length) < minresults)
            {
                Log.Selector(string.Format("Windows.GetElement::Failed locating " + minresults + " item(s) {0:mm\\:ss\\.fff}", sw.Elapsed));
                throw new ElementNotFoundException("Failed locating " + minresults + " item(s)");
            }
            IEnumerator <UIElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            if (lastelements.Length == elements.Length && lastelements.Length > 0)
            {
                more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            }
            if (more)
            {
                if (interactive)
                {
                    var testelement = _enum.Current;
                    Wait.UntilResponsive(testelement.RawElement, PluginConfig.search_timeout);
                }
                context.SetValue(_elements, _enum);
                context.SetValue(_sw, sw);
                Log.Selector(string.Format("Windows.GetElement::end:: call ScheduleAction: {0:mm\\:ss\\.fff}", sw.Elapsed));
                IncIndex(context);
                SetTotal(context, elements.Length);
                context.ScheduleAction <UIElement>(Body, _enum.Current, OnBodyComplete);
            }
            else
            {
                Log.Selector(string.Format("Windows.GetElement:end {0:mm\\:ss\\.fff}", sw.Elapsed));
            }
        }
Ejemplo n.º 4
0
        public static UIElement[] GetElementsWithuiSelector(WindowsSelector selector, IElement fromElement, int maxresults, WindowsCacheExtension ext)
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(5000);

            timeout = TimeSpan.FromMilliseconds(20000);
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            Log.Selector(string.Format("GetElementsWithuiSelector::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            UIElement _fromElement = fromElement as UIElement;
            // var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
            var selectors = selector.ToList();
            AutomationElement startfrom = null;

            if (_fromElement != null)
            {
                startfrom = _fromElement.RawElement;
            }


            var _current = new List <UIElement>();
            // var automation = AutomationUtil.getAutomation();
            AutomationBase automation = null;

            if (ext != null)
            {
                automation = ext.automation;
            }
            if (automation == null)
            {
                automation = AutomationUtil.getAutomation();
            }

            UIElement[] result = null;
            // AutomationElement ele = null;

            bool search_descendants = selectors[0].SearchDescendants();

            if (startfrom == null)
            {
                startfrom = automation.GetDesktop();
            }
            _current.Add(new UIElement(startfrom));
            for (var i = 0; i < selectors.Count; i++)
            {
                var _sel    = selectors[i];
                var sel     = new WindowsSelectorItem(_sel);
                var current = _current.ToArray();
                _current.Clear();
                // if(i == 1 && current.Length == 1 && current.First().ControlType == sel.ControlType)
                if (i == 1)
                {
                    foreach (var e in current)
                    {
                        if (WindowsSelectorItem.Match(sel, e.RawElement))
                        {
                            _current.Add(e);
                        }
                    }
                    if (_current.Count > 0)
                    {
                        continue;
                    }
                    //_current = GetElementsWithuiSelectorItem(automation, sel, current, maxresults, i == (selectors.Count - 1)).ToList();
                    //if(_current.Count == 0) _current = current.ToList();
                    //_current = current.ToList();
                }
                _current = GetElementsWithuiSelectorItem(i, automation, sel, current, maxresults, i == (selectors.Count - 1), search_descendants).ToList();
                if (i == 0 && _current.Count == 0)
                {
                    _current = current.ToList();
                }
                else if (i > 0 && _current.Count == 0 && current.Length > 0 && (PluginConfig.try_mouse_over_search || selector.mouse_over_search()))
                {
                    for (var z = 0; z < current.Length; z++)
                    {
                        current[z].Focus();
                        var x = current[z].Rectangle.X + 5; var y = current[z].Rectangle.Y + 5;
                        var screen = automation.FromPoint(new System.Drawing.Point(x, y));
                        Log.Selector("mouse point lookup at " + x + "," + y + " for " + sel.ToString());
                        var screenel = screen.Parent;
                        while (screenel != null)
                        {
                            var uiscreenel = new UIElement(screenel);

                            if (WindowsSelectorItem.Match(sel, screenel))
                            {
                                _current.Clear();
                                _current.Add(new UIElement(screenel));
                                Log.Selector("Mouse lookup found " + uiscreenel.ToString());
                                break;
                            }
                            else
                            {
                                screenel = screenel.Parent;
                            }
                            // only allow direct parents, or search for match in all child elements found ? ( like searching for a button inside a pane )
                            //else
                            //{
                            //    Log.Output("Lookup in " + uiscreenel.ToString());
                            //    _current = GetElementsWithuiSelectorItem(i, automation, sel, new UIElement[] { uiscreenel }, maxresults, i == (selectors.Count - 1), search_descendants).ToList();
                            //    if (_current.Count != 0) break;
                            //    screenel = screenel.Parent;
                            //}
                        }
                        if (_current.Count > 0)
                        {
                            break;
                        }
                    }
                }
            }
            Log.Debug(string.Format("GetElementsWithuiSelector::completed with " + _current.Count + " results {0:mm\\:ss\\.fff}", sw.Elapsed));
            if (_current.Count > 0)
            {
                result = _current.ToArray();
                if (result.Count() > maxresults)
                {
                    Console.WriteLine("found " + result.Count() + " but only needed " + maxresults);
                    result = result.Take(maxresults).ToArray();
                }
                return(result);
            }
            if (result == null)
            {
                Log.Selector(string.Format("GetElementsWithuiSelector::ended with 0 results after {0:mm\\:ss\\.fff}", sw.Elapsed));
                return(new UIElement[] { });
            }
            if (result.Count() > maxresults)
            {
                result = result.Take(maxresults).ToArray();
            }
            Log.Selector(string.Format("GetElementsWithuiSelector::ended with " + result.Length + " results after {0:mm\\:ss\\.fff}", sw.Elapsed));
            return(result);
        }