示例#1
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));
            }
        }
示例#2
0
        public IElement LaunchBySelector(Selector selector, bool CheckRunning, TimeSpan timeout)
        {
            if (selector == null || selector.Count == 0)
            {
                return(null);
            }
            Process process = null;
            var     sw      = new Stopwatch();

            IElement[] elements = { };
            if (CheckRunning)
            {
                if (PluginConfig.get_elements_in_different_thread)
                {
                    elements = AutomationHelper.RunSTAThread <IElement[]>(() =>
                    {
                        try
                        {
                            Log.Selector("LaunchBySelector in non UI thread");
                            return(GetElementsWithSelector(selector, null, 1));
                        }
                        catch (System.Threading.ThreadAbortException ex)
                        {
                            Log.Error(ex, "");
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "");
                        }
                        return(new UIElement[] { });
                    }, TimeSpan.FromMilliseconds(5000)).Result;
                }
                else
                {
                    Log.Selector("LaunchBySelector using UI thread");
                    elements = GetElementsWithSelector(selector, null, 1);
                }
                if (elements == null)
                {
                    elements = new IElement[] { };
                }
                // elements = GetElementsWithSelector(selector, null, 1);
                if (elements.Length > 0)
                {
                    elements[0].Focus();
                    var _window = ((UIElement)elements[0]);
                    return(new UIElement(_window.GetWindow()));
                }
            }
            var f = selector.First();
            SelectorItemProperty p;
            bool   isImmersiveProcess     = false;
            string applicationUserModelId = null;
            string filename    = null;
            string processname = null;
            string arguments   = null;

            p = f.Properties.Where(x => x.Name == "isImmersiveProcess").FirstOrDefault();
            if (p != null)
            {
                isImmersiveProcess = bool.Parse(p.Value);
            }
            p = f.Properties.Where(x => x.Name == "applicationUserModelId").FirstOrDefault();
            if (p != null)
            {
                applicationUserModelId = p.Value;
            }
            p = f.Properties.Where(x => x.Name == "filename").FirstOrDefault();
            if (p != null)
            {
                filename = p.Value;
            }
            p = f.Properties.Where(x => x.Name == "processname").FirstOrDefault();
            if (p != null)
            {
                processname = p.Value;
            }
            p = f.Properties.Where(x => x.Name == "arguments").FirstOrDefault();
            if (p != null)
            {
                arguments = p.Value;
            }
            if (isImmersiveProcess)
            {
                process = FlaUI.Core.Tools.WindowsStoreAppLauncher.Launch(applicationUserModelId, arguments);
            }
            else
            {
                Log.Debug("Starting a new instance of " + processname);
                process = Process.Start(new ProcessStartInfo
                {
                    FileName  = Environment.ExpandEnvironmentVariables(filename),
                    Arguments = Environment.ExpandEnvironmentVariables(arguments)
                });
            }
            try
            {
                GenericTools.Restore(process.MainWindowHandle);
            }
            catch (Exception ex)
            {
                Log.Error("restore window: " + ex.ToString());
            }
            try
            {
                // process.WaitForInputIdle();
            }
            catch (Exception ex)
            {
                Log.Error("WaitForInputIdle window: " + ex.ToString());
            }


            sw = new Stopwatch();
            sw.Start();
            if (timeout < TimeSpan.FromSeconds(10))
            {
                timeout = TimeSpan.FromSeconds(10);
            }
            do
            {
                if (PluginConfig.get_elements_in_different_thread)
                {
                    elements = AutomationHelper.RunSTAThread <IElement[]>(() =>
                    {
                        try
                        {
                            Log.Selector("LaunchBySelector in non UI thread");
                            return(GetElementsWithSelector(selector, null, 1));
                        }
                        catch (System.Threading.ThreadAbortException ex)
                        {
                            Log.Error(ex, "");
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "");
                        }
                        return(new UIElement[] { });
                    }, TimeSpan.FromMilliseconds(5000)).Result;
                }
                else
                {
                    Log.Selector("LaunchBySelector using UI thread");
                    elements = GetElementsWithSelector(selector, null, 1);
                }
                if (elements == null)
                {
                    elements = new IElement[] { };
                }
            } while (elements != null && elements.Length == 0 && sw.Elapsed < timeout);
            WindowsSelectorItem.ClearCache();
            if (elements.Length > 0)
            {
                var window = ((UIElement)elements[0]);
                return(new UIElement(window.GetWindow()));
            }
            else
            {
                return(null);
            }
        }
示例#3
0
        protected override void Execute(NativeActivityContext context)
        {
            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 from = From.Get(context);

            //            double _timeout = 250;
            double _timeout = 5000;

            if (PluginConfig.search_descendants)
            {
                _timeout = 5000;
            }
            //#if DEBUG
            //            _timeout = _timeout * 8;
            //#endif
            int failcounter = 0;

            do
            {
                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));
                        }
                        catch (System.Threading.ThreadAbortException)
                        {
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "");
                        }
                        return(new UIElement[] { });
                    }, TimeSpan.FromMilliseconds(_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);
                }
                //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);
            //}
            context.SetValue(Elements, elements);
            if (elements.Count() < 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 (more)
            {
                context.SetValue(_elements, _enum);
                context.SetValue(_sw, sw);
                Log.Selector(string.Format("Windows.GetElement::end:: call ScheduleAction: {0:mm\\:ss\\.fff}", sw.Elapsed));
                context.ScheduleAction <UIElement>(Body, _enum.Current, OnBodyComplete);
            }
            else
            {
                Log.Selector(string.Format("Windows.GetElement:end {0:mm\\:ss\\.fff}", sw.Elapsed));
            }
        }