Example #1
0
        public NMSelector(NMElement element, NMSelector anchor, bool doEnum, NMElement anchorelement)
        {
            var sw = new System.Diagnostics.Stopwatch();

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

            NMSelectorItem item;

            if (anchor == null)
            {
                item            = new NMSelectorItem(element, true, false);
                item.Enabled    = true;
                item.canDisable = false;
                Items.Add(item);
            }
            else
            {
                var anchorarray  = anchorelement.cssselector.Split('>');
                var elementarray = element.cssselector.Split('>');
                elementarray        = elementarray.Skip(anchorarray.Length).ToArray();
                element.cssselector = string.Join(">", elementarray);
                // element.cssselector = element.cssselector.Substring(anchorelement.cssselector.Length);
            }
            item            = new NMSelectorItem(element, false, (anchor != null));
            item.Enabled    = true;
            item.canDisable = false;
            Items.Add(item);

            Log.Selector(string.Format("NMselector::EnumNeededProperties::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Item[]"));
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }
Example #2
0
        protected override void Execute(NativeActivityContext context)
        {
            var selector   = Selector.Get(context);
            var sel        = new NMSelector(selector);
            var timeout    = TimeSpan.FromSeconds(3);
            var from       = From.Get(context);
            var maxresults = MaxResults.Get(context);

            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }
            context.SetValue(Elements, elements);
            IEnumerator <NMElement> _enum = elements.ToList().GetEnumerator();

            context.SetValue(_elements, _enum);
            bool more = _enum.MoveNext();

            if (more)
            {
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
            else
            {
                throw new Interfaces.ElementNotFoundException("Failed locating item");
            }
        }
Example #3
0
        public NMSelector(NMElement element, NMSelector anchor, bool doEnum)
        {
            var sw = new System.Diagnostics.Stopwatch();

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

            NMSelectorItem item;

            if (anchor == null)
            {
                item            = new NMSelectorItem(element, true);
                item.Enabled    = true;
                item.canDisable = false;
                Items.Add(item);
            }
            item            = new NMSelectorItem(element, false);
            item.Enabled    = true;
            item.canDisable = false;
            Items.Add(item);

            Log.Selector(string.Format("NMselector::EnumNeededProperties::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Item[]"));
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }
Example #4
0
        private void Open_Selector(object sender, RoutedEventArgs e)
        {
            ModelItem  loadFrom = ModelItem.Parent;
            string     loadFromSelectorString = "";
            NMSelector anchor = null;

            while (loadFrom.Parent != null)
            {
                var p = loadFrom.Properties.Where(x => x.Name == "Selector").FirstOrDefault();
                if (p != null)
                {
                    loadFromSelectorString = loadFrom.GetValue <string>("Selector");
                    anchor = new NMSelector(loadFromSelectorString);
                    break;
                }
                loadFrom = loadFrom.Parent;
            }
            string SelectorString = ModelItem.GetValue <string>("Selector");
            int    maxresults     = ModelItem.GetValue <int>("MaxResults");

            Interfaces.Selector.SelectorWindow selectors;
            if (!string.IsNullOrEmpty(SelectorString))
            {
                var selector = new NMSelector(SelectorString);
                selectors = new Interfaces.Selector.SelectorWindow("NM", selector, anchor, maxresults);
            }
            else
            {
                var selector = new NMSelector("[{Selector: 'NM'}]");
                selectors = new Interfaces.Selector.SelectorWindow("NM", selector, anchor, maxresults);
            }
            if (selectors.ShowDialog() == true)
            {
                ModelItem.Properties["Selector"].SetValue(new InArgument <string>()
                {
                    Expression = new Literal <string>(selectors.vm.json)
                });
                var l = selectors.vm.Selector.Last();
                if (l.Element != null)
                {
                    ModelItem.Properties["Image"].SetValue(l.Element.ImageString());
                    NotifyPropertyChanged("Image");
                }
                if (anchor != null)
                {
                    ModelItem.Properties["From"].SetValue(new InArgument <NMElement>()
                    {
                        Expression = new Microsoft.VisualBasic.Activities.VisualBasicValue <NMElement>("item")
                    });
                    ModelItem.Properties["MinResults"].SetValue(new InArgument <int>()
                    {
                        Expression = new Microsoft.VisualBasic.Activities.VisualBasicValue <int>("0")
                    });
                    ModelItem.Properties["Timeout"].SetValue(new InArgument <TimeSpan>()
                    {
                        Expression = new Microsoft.VisualBasic.Activities.VisualBasicValue <TimeSpan>("TimeSpan.FromSeconds(0)")
                    });
                }
            }
        }
Example #5
0
        private void onMessage(NativeMessagingMessage message)
        {
            if (message.uiy > 0 && message.uix > 0 && message.uiwidth > 0 && message.uiheight > 0)
            {
                // Console.WriteLine("{0} {1},{2} {3},{4} - {5} ", message.functionName, message.uix, message.uiy, message.uiwidth, message.uiheight,  message.xPath);
                if (!string.IsNullOrEmpty(message.data))
                {
                    lastElement = new NMElement(message);
                }
                else
                {
                    lastElement = new NMElement(message);
                }
            }

            if (message.functionName == "click")
            {
                if (recording)
                {
                    if (lastElement == null)
                    {
                        return;
                    }
                    var re = new RecordEvent(); re.Button = Input.MouseButton.Left;
                    var a  = new GetElement {
                        DisplayName = lastElement.ToString()
                    };

                    var selector = new NMSelector(lastElement, null, true);
                    a.Selector   = selector.ToString();
                    a.Image      = lastElement.ImageString();
                    a.MaxResults = 1;

                    re.Selector     = selector;
                    re.a            = new GetElementResult(a);
                    re.SupportInput = lastElement.SupportInput;
                    re.ClickHandled = true;
                    OnUserAction?.Invoke(this, re);
                    return;
                }
                //var getelement = new NativeMessagingMessage("getelement");
                ////getelement.tabid = message.tabid;
                //getelement.cssPath = message.cssPath;
                //getelement.xPath = message.xPath;
                //NativeMessagingMessage subresult = null;
                ////if (message.browser == "chrome") subresult = rpaactivities.nm.nmhook.sendMessageChromeResult(getelement, true);
                ////if (message.browser == "ff") subresult = rpaactivities.nm.nmhook.sendMessageFFResult(getelement, true);
                //if (subresult == null)
                //{
                //    // Console.WriteLine("getelement returned null???");
                //    return;
                //}
                //// Console.WriteLine(getelement.messageid + " " + getelement.functionName + " " + subresult.messageid + " " + subresult.functionName);
                //// Console.WriteLine(subresult.cssPath + " " + subresult.xPath);
                ////rpaactivities.nm.nmhook.sendMessageChromeResult(getelement);
                ////Console.WriteLine(getelement.messageid + " " + getelement.functionName);
            }
        }
Example #6
0
        public IElement[] GetElementsWithSelector(Selector selector, IElement fromElement = null, int maxresults = 1)
        {
            if (!(selector is NMSelector nmselector))
            {
                nmselector = new NMSelector(selector.ToString());
            }
            var result = NMSelector.GetElementsWithuiSelector(nmselector, fromElement, maxresults);

            return(result);
        }
Example #7
0
        public Selector GetSelector(Selector anchor, treeelement item)
        {
            var        nmitem   = item as NMTreeElement;
            NMSelector nmanchor = anchor as NMSelector;

            if (nmitem == null && anchor != null)
            {
                nmanchor = new NMSelector(anchor.ToString());
            }
            return(new NMSelector(nmitem.NMElement, nmanchor, true));
        }
Example #8
0
        private async void Highlight_Click(object sender, RoutedEventArgs e)
        {
            string SelectorString = ModelItem.GetValue <string>("Selector");
            int    maxresults     = ModelItem.GetValue <int>("MaxResults");
            var    selector       = new NMSelector(SelectorString);
            var    elements       = NMSelector.GetElementsWithuiSelector(selector, null, maxresults);

            foreach (var ele in elements)
            {
                await ele.Highlight(false, System.Drawing.Color.Red, TimeSpan.FromSeconds(1));
            }
        }
Example #9
0
        public bool ParseUserAction(ref IRecordEvent e)
        {
            if (e.UIElement == null)
            {
                return(false);
            }

            if (e.UIElement.ProcessId < 1)
            {
                return(false);
            }
            var p = System.Diagnostics.Process.GetProcessById(e.UIElement.ProcessId);

            if (p.ProcessName.ToLower() != "chrome" && p.ProcessName.ToLower() != "firefox")
            {
                return(false);
            }

            if (p.ProcessName.ToLower() == "chrome" && !NMHook.chromeconnected)
            {
                System.Windows.MessageBox.Show("You clicked inside Chrome, but it looks like you dont have the OpenRPA plugin installed");
                return(false);
            }
            if (p.ProcessName.ToLower() == "firefox" && !NMHook.ffconnected)
            {
                System.Windows.MessageBox.Show("You clicked inside Firefix, but it looks like you dont have the OpenRPA plugin installed");
                return(false);
            }
            if (LastElement == null)
            {
                return(false);
            }
            var selector = new NMSelector(LastElement, null, true, null);
            var a        = new GetElement {
                DisplayName = LastElement.id + " " + LastElement.type + " " + LastElement.Name
            };

            a.Selector   = selector.ToString();
            a.Image      = LastElement.ImageString();
            a.MaxResults = 1;

            e.Element       = LastElement;
            e.Selector      = selector;
            e.a             = new GetElementResult(a);
            e.SupportInput  = LastElement.SupportInput;
            e.SupportSelect = false;
            e.OffsetX       = e.X - LastElement.Rectangle.X;
            e.OffsetY       = e.Y - LastElement.Rectangle.Y;
            e.ClickHandled  = false;
            //e.ClickHandled = true;
            //LastElement.Click(true, e.Button, e.X, e.Y, false, false);
            return(true);
        }
Example #10
0
        private void OnMessage(NativeMessagingMessage message)
        {
            try
            {
                if (message.uiy > 0 && message.uix > 0 && message.uiwidth > 0 && message.uiheight > 0)
                {
                    if (!string.IsNullOrEmpty(message.data))
                    {
                        LastElement = new NMElement(message);
                    }
                    else
                    {
                        LastElement = new NMElement(message);
                    }
                }

                if (message.functionName == "click")
                {
                    if (IsRecording)
                    {
                        if (LastElement == null)
                        {
                            return;
                        }
                        var re = new RecordEvent
                        {
                            Button = Input.MouseButton.Left
                        }; var a = new GetElement {
                            DisplayName = LastElement.ToString()
                        };

                        var selector = new NMSelector(LastElement, null, true, null);
                        a.Selector   = selector.ToString();
                        a.Image      = LastElement.ImageString();
                        a.MaxResults = 1;

                        re.Selector      = selector;
                        re.a             = new GetElementResult(a);
                        re.SupportInput  = LastElement.SupportInput;
                        re.SupportSelect = false;
                        re.ClickHandled  = true;
                        OnUserAction?.Invoke(this, re);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Example #11
0
        private void LoopActionComplete(NativeActivityContext context, ActivityInstance completedInstance)
        {
            var allelements = context.GetValue(_allelements);
            var selector = Selector.Get(context);
            selector = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(selector, context.DataContext);
            var sel = new NMSelector(selector);
            var from = From.Get(context);
            string browser = sel.browser;
            if (from != null) browser = from.browser;
            System.Threading.Thread.Sleep(500);
            DoWaitForReady(browser);
            Execute(context);

        }
Example #12
0
        public Selector GetSelector(Selector anchor, treeelement item)
        {
            var        nmitem   = item as NMTreeElement;
            NMSelector nmanchor = anchor as NMSelector;

            if (nmitem == null && anchor != null)
            {
                nmanchor = new NMSelector(anchor.ToString());
            }
            if (nmanchor != null)
            {
                var element = GetElementsWithSelector(nmanchor);
                return(new NMSelector(nmitem.NMElement, nmanchor, true, (NMElement)element.FirstOrDefault()));
            }
            return(new NMSelector(nmitem.NMElement, nmanchor, true, null));
        }
        private async void Highlight_Click(object sender, RoutedEventArgs e)
        {
            ModelItem  loadFrom = ModelItem.Parent;
            string     loadFromSelectorString = "";
            NMSelector anchor           = null;
            int        parentmaxresults = 1;

            while (loadFrom.Parent != null)
            {
                var p = loadFrom.Properties.Where(x => x.Name == "Selector").FirstOrDefault();
                if (p != null)
                {
                    loadFromSelectorString = loadFrom.GetValue <string>("Selector");
                    anchor           = new NMSelector(loadFromSelectorString);
                    parentmaxresults = loadFrom.GetValue <int>("MaxResults");
                    break;
                }
                loadFrom = loadFrom.Parent;
            }


            string SelectorString = ModelItem.GetValue <string>("Selector");
            int    maxresults     = ModelItem.GetValue <int>("MaxResults");
            var    selector       = new NMSelector(SelectorString);
            // var elements = NMSelector.GetElementsWithuiSelector(selector, anchor, maxresults);
            var elements = new List <NMElement>();

            if (anchor != null)
            {
                var _base = NMSelector.GetElementsWithuiSelector(anchor, null, parentmaxresults);
                foreach (var _e in _base)
                {
                    var res = NMSelector.GetElementsWithuiSelector(selector, _e, maxresults);
                    elements.AddRange(res);
                }
            }
            else
            {
                var res = NMSelector.GetElementsWithuiSelector(selector, null, maxresults);
                elements.AddRange(res);
            }

            foreach (var ele in elements)
            {
                await ele.Highlight(false, System.Drawing.Color.Red, TimeSpan.FromSeconds(1));
            }
        }
Example #14
0
        protected override void Execute(NativeActivityContext context)
        {
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }
            context.SetValue(Elements, elements);
            IEnumerator <NMElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            if (more)
            {
                if (elements.Count() > 1)
                {
                    context.SetValue(_elements, _enum);
                }
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
            else if (elements.Length < minresults)
            {
                throw new Interfaces.ElementNotFoundException("Failed locating " + minresults + " item");
            }
        }
Example #15
0
        private void onMessage(NativeMessagingMessage message)
        {
            if (message.uiy > 0 && message.uix > 0 && message.uiwidth > 0 && message.uiheight > 0)
            {
                // Console.WriteLine("{0} {1},{2} {3},{4} - {5} ", message.functionName, message.uix, message.uiy, message.uiwidth, message.uiheight,  message.xPath);
                if (!string.IsNullOrEmpty(message.data))
                {
                    lastElement = new NMElement(message);
                }
                else
                {
                    lastElement = new NMElement(message);
                }
            }

            if (message.functionName == "click")
            {
                if (recording)
                {
                    if (lastElement == null)
                    {
                        return;
                    }
                    var re = new RecordEvent(); re.Button = Input.MouseButton.Left;
                    var a  = new GetElement {
                        DisplayName = lastElement.ToString()
                    };

                    var selector = new NMSelector(lastElement, null, true);
                    a.Selector   = selector.ToString();
                    a.Image      = lastElement.ImageString();
                    a.MaxResults = 1;

                    re.Selector     = selector;
                    re.a            = new GetElementResult(a);
                    re.SupportInput = lastElement.SupportInput;
                    re.ClickHandled = true;
                    OnUserAction?.Invoke(this, re);
                    return;
                }
            }
        }
Example #16
0
        public bool parseUserAction(ref IRecordEvent e)
        {
            if (lastElement == null)
            {
                return(false);
            }
            if (e.UIElement == null)
            {
                return(false);
            }

            if (e.UIElement.ProcessId < 1)
            {
                return(false);
            }
            var p = System.Diagnostics.Process.GetProcessById(e.UIElement.ProcessId);

            if (p.ProcessName.ToLower() != "chrome" && p.ProcessName.ToLower() != "firefox")
            {
                return(false);
            }

            var selector = new NMSelector(lastElement, null, true);
            var a        = new GetElement {
                DisplayName = lastElement.id + " " + lastElement.type + " " + lastElement.Name
            };

            a.Selector   = selector.ToString();
            a.Image      = lastElement.ImageString();
            a.MaxResults = 1;

            e.Element      = lastElement;
            e.Selector     = selector;
            e.a            = new GetElementResult(a);
            e.SupportInput = lastElement.SupportInput;
            e.ClickHandled = true;
            e.OffsetX      = e.X - lastElement.Rectangle.X;
            e.OffsetY      = e.Y - lastElement.Rectangle.Y;
            lastElement.Click(true, e.Button, e.X, e.Y);
            return(true);
        }
Example #17
0
        protected override void Execute(NativeActivityContext context)
        {
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            string browser = sel.browser;

            if (WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var allelements = context.GetValue(_allelements);

            if (allelements == null)
            {
                allelements = new NMElement[] { }
            }
            ;

            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.FindTabByURL(browser, s.url);
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }

            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
                Log.Selector("BEGIN:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");

                // allelements = allelements.Concat(elements).ToArray();
                if (allelements.Length > 0)
                {
                    var newelements = new List <NMElement>();
                    for (var i = elements.Length - 1; i >= 0; i--)
                    {
                        var element = elements[i];
                        if (!allelements.Contains(element))
                        {
                            newelements.Insert(0, element);
                        }
                    }
                    elements = newelements.ToArray();
                    //if(elements.Count() > 20)
                    //{
                    //    for(var i=0; i < allelements.Length && i < elements.Length; i++)
                    //    {
                    //        if (!eq.Equals(allelements[i], elements[i]) || allelements[i].GetHashCode() != elements[i].GetHashCode())
                    //        {
                    //            Log.Output(allelements[i].GetHashCode() + " / " + elements[i].GetHashCode());
                    //        }

                    //    }

                    //}
                }
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }

            if ((elements.Length + allelements.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 <NMElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            //if (lastelements.Length == elements.Length && lastelements.Length > 0)
            //{
            //    var eq = new Activities.NMEqualityComparer();
            //    more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            //}
            if (more)
            {
                allelements = allelements.Concat(elements).ToArray();
                var eq = new Activities.NMEqualityComparer();
                allelements = allelements.Distinct(eq).ToArray();

                //var allelementslength = allelements.Length;
                //Array.Resize(ref allelements, allelements.Length + elements.Length);
                //Array.Copy(elements, 0, allelements, allelementslength, elements.Length);
            }

            context.SetValue(_allelements, allelements);
            context.SetValue(Elements, allelements);
            Log.Selector("END:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");

            if (more)
            {
                context.SetValue(_elements, _enum);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
        }
Example #18
0
        protected override void Execute(NativeActivityContext context)
        {
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            if (WaitForReady.Get(context))
            {
                string browser = sel.browser;
                if (from != null)
                {
                    browser = from.browser;
                }
                if (!string.IsNullOrEmpty(browser))
                {
                    NMHook.enumtabs();
                    if (browser == "chrome" && NMHook.CurrentChromeTab != null)
                    {
                        NMHook.WaitForTab(NMHook.CurrentChromeTab.id, browser, TimeSpan.FromSeconds(10));
                    }
                    if (browser == "ff" && NMHook.CurrentFFTab != null)
                    {
                        NMHook.WaitForTab(NMHook.CurrentFFTab.id, browser, TimeSpan.FromSeconds(10));
                    }
                }
            }
            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }
            context.SetValue(Elements, elements);

            var lastelements = context.GetValue(_lastelements);

            if (lastelements == null)
            {
                lastelements = new NMElement[] { }
            }
            ;
            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 <NMElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            if (lastelements.Length == elements.Length && lastelements.Length > 0)
            {
                var eq = new Activities.NMEqualityComparer();

                more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            }
            if (more)
            {
                context.SetValue(_elements, _enum);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
        }
Example #19
0
        public bool ParseUserAction(ref IRecordEvent e)
        {
            if (e.UIElement == null)
            {
                return(false);
            }

            if (e.UIElement.ProcessId < 1)
            {
                return(false);
            }
            using (var p = System.Diagnostics.Process.GetProcessById(e.UIElement.ProcessId))
            {
                if (p.ProcessName.ToLower() != "chrome" && p.ProcessName.ToLower() != "firefox" && p.ProcessName.ToLower() != "msedge")
                {
                    return(false);
                }

                if (p.ProcessName.ToLower() == "chrome")
                {
                    if (!NMHook.chromeconnected)
                    {
                        System.Windows.MessageBox.Show("You clicked inside Chrome, but it looks like you dont have the OpenRPA plugin installed"); return(false);
                    }
                    if (string.IsNullOrEmpty(e.UIElement.FrameworkId) || e.UIElement.FrameworkId.ToLower() != "chrome")
                    {
                        return(false);
                    }
                }
                if (p.ProcessName.ToLower() == "msedge" && !NMHook.edgeconnected)
                {
                    System.Windows.MessageBox.Show("You clicked inside Edge, but it looks like you dont have the OpenRPA plugin installed");
                    return(false);
                }
                if (p.ProcessName.ToLower() == "firefox" && !NMHook.ffconnected)
                {
                    System.Windows.MessageBox.Show("You clicked inside Firefix, but it looks like you dont have the OpenRPA plugin installed");
                    return(false);
                }
                if (LastElement == null)
                {
                    return(false);
                }
                if (LastElement.message == null)
                {
                    return(false);
                }
                if (LastElement.message.tab == null)
                {
                    LastElement.message.tab = NMHook.FindTabById(LastElement.message.browser, LastElement.message.tabid);
                }
                if (p.ProcessName.ToLower() == "chrome" || p.ProcessName.ToLower() == "msedge")
                {
                    // if (e.UIElement.FrameworkId != "chrome" && e.UIElement.FrameworkId != "Chrome") return false;
                }
            }
            var selector = new NMSelector(LastElement, null, true, null);
            var a        = new GetElement {
                DisplayName = LastElement.id + " " + LastElement.type + " " + LastElement.Name
            };

            a.Variables.Add(new Variable <int>("Index", 0));
            a.Variables.Add(new Variable <int>("Total", 0));
            a.Selector   = selector.ToString();
            a.Image      = LastElement.ImageString();
            a.MaxResults = 1;

            e.Element       = LastElement;
            e.Selector      = selector;
            e.a             = new GetElementResult(a);
            e.SupportInput  = LastElement.SupportInput;
            e.SupportSelect = LastElement.tagname.ToLower() == "select";
            e.OffsetX       = e.X - LastElement.Rectangle.X;
            e.OffsetY       = e.Y - LastElement.Rectangle.Y;
            e.ClickHandled  = false;
            //e.ClickHandled = true;
            //LastElement.Click(true, e.Button, e.X, e.Y, false, false);
            return(true);
        }
Example #20
0
        private void OnMessage(NativeMessagingMessage message)
        {
            try
            {
                //lock(_lock)
                //{

                //    //if (dpiscale == 0)
                //    //{
                //    //    dpiscale = NativeMethods.getScalingFactor();
                //    //} else
                //    //{
                //    //    dpiscale = NativeMethods.getScalingFactor();
                //    //}
                //    var dpi = NativeMethods.GetDpiForSystem();
                //    dpiscale = 1;
                //    if (dpi == 96) dpiscale = 1;
                //    if (dpi == 120) dpiscale = 1.25f;
                //    if (dpi == 144) dpiscale = 1.5f;
                //    if (dpi == 192) dpiscale = 2;
                //}
                if (message.uiy > 0 && message.uix > 0 && message.uiwidth > 0 && message.uiheight > 0)
                {
                    //message.uiy = (int)(message.uiy * dpiscale);
                    //message.uix = (int)(message.uix * dpiscale);
                    //message.uiwidth = (int)(message.uiwidth * dpiscale);
                    //message.uiheight = (int)(message.uiheight * dpiscale);
                    if (dpiscale == 1.25)
                    {
                        message.uiy += 158;
                    }
                    LastElement = new NMElement(message);
                }

                if (message.functionName == "click")
                {
                    if (IsRecording)
                    {
                        if (LastElement == null)
                        {
                            return;
                        }
                        var re = new RecordEvent
                        {
                            Button = Input.MouseButton.Left
                        }; var a = new GetElement {
                            DisplayName = LastElement.ToString()
                        };

                        message.tab = NMHook.tabs.Where(x => x.id == message.tabid && x.windowId == message.windowId).FirstOrDefault();

                        var selector = new NMSelector(LastElement, null, true, null);
                        a.Selector   = selector.ToString();
                        a.Image      = LastElement.ImageString();
                        a.MaxResults = 1;

                        re.Selector      = selector;
                        re.a             = new GetElementResult(a);
                        re.SupportInput  = LastElement.SupportInput;
                        re.SupportSelect = false;
                        re.ClickHandled  = true;
                        OnUserAction?.Invoke(this, re);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Example #21
0
 public override IElement[] GetElements(IElement fromElement = null, int maxresults = 1)
 {
     return(NMSelector.GetElementsWithuiSelector(this, fromElement, maxresults));
 }
Example #22
0
        protected override async Task <object> ExecuteAsync(NativeActivityContext context)
        {
            IsCancel = false;
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };

            sw.Start();
            string browser = sel.browser;

            if (WaitForReady != null && WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var allelements = context.GetValue(_allelements);

            if (allelements == null)
            {
                allelements = new NMElement[] { }
            }
            ;

            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.tabs.Where(x => x.browser == browser && x.url.ToLower().StartsWith(s.url.ToLower())).FirstOrDefault();
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }



            //var id = Guid.NewGuid().ToString();
            //context.CreateBookmark(id, (activityContext, bookmark, value) =>
            //{

            //});

            //Task.Factory.StartNew(() =>
            //{
            //    // context.RemoveBookmark(id);
            //});

            // await Task.Delay(100);
            await Task.Run(() =>
            {
                do
                {
                    elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
                    Log.Selector("BEGIN:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");

                    // allelements = allelements.Concat(elements).ToArray();
                    if (allelements.Length > 0)
                    {
                        var newelements = new List <NMElement>();
                        for (var i = elements.Length - 1; i >= 0; i--)
                        {
                            var element = elements[i];
                            if (!allelements.Contains(element))
                            {
                                newelements.Insert(0, element);
                            }
                        }
                        elements = newelements.ToArray();
                        //if(elements.Count() > 20)
                        //{
                        //    for(var i=0; i < allelements.Length && i < elements.Length; i++)
                        //    {
                        //        if (!eq.Equals(allelements[i], elements[i]) || allelements[i].GetHashCode() != elements[i].GetHashCode())
                        //        {
                        //            Log.Output(allelements[i].GetHashCode() + " / " + elements[i].GetHashCode());
                        //        }

                        //    }

                        //}
                    }
                } while (elements.Count() == 0 && sw.Elapsed < timeout && !IsCancel);
                if (IsCancel)
                {
                    Console.WriteLine("was Canceled: true! DisplayName" + DisplayName);
                }
            });

            return(elements);
        }
Example #23
0
        //private static NMElement[] GetElementsWithuiSelector(WindowsAccessBridgeInterop.AccessibleJvm jvm, NMSelector selector, IElement fromElement, int maxresults)
        //{
        //    NMElement[] result = null;
        //    NMElement _fromElement = fromElement as NMElement;
        //    var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
        //    var current = new List<NMElement>();
        //    NMElement startfrom = null;
        //    if (_fromElement != null) startfrom = _fromElement;
        //    if (startfrom == null) startfrom = new NMElement(jvm);
        //    current.Add(startfrom);
        //    for (var i = 0; i < selectors.Count; i++)
        //    {
        //        var sw = new System.Diagnostics.Stopwatch();
        //        sw.Start();
        //        var s = new NMSelectorItem(selectors[i]);
        //        Log.Selector(string.Format("OpenRPA.NM::GetElementsWithuiSelector::Find for selector {0} {1}", i, s.ToString()));
        //        var elements = new List<NMElement>();
        //        elements.AddRange(current);
        //        current.Clear();
        //        foreach (var _element in elements)
        //        {
        //            result = ((NMSelectorItem)s).matches(_element);
        //            current.AddRange(result);
        //        }
        //        if (i == (selectors.Count - 1)) result = current.ToArray();
        //        if (current.Count == 0)
        //        {
        //            var _c = new NMSelectorItem(selectors[i]);
        //            var message = "needed to find " + Environment.NewLine + _c.ToString() + Environment.NewLine + "but found only: " + Environment.NewLine;
        //            foreach (var element in elements)
        //            {
        //                var children = element.Children;
        //                foreach (var c in children)
        //                {
        //                    try
        //                    {
        //                        message += c.ToString() + Environment.NewLine;
        //                    }
        //                    catch (Exception)
        //                    {
        //                    }
        //                }
        //            }
        //            Log.Selector(message);
        //            return new NMElement[] { };
        //        }
        //        Log.Selector(string.Format("OpenRPA.NM::GetElement::found {1} for selector {2} in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count(), i));
        //    }
        //    if (result == null) return new NMElement[] { };
        //    return result;
        //}
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with onlu 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;

            if (fromElement != null)
            {
                browser = fromNMElement.browser;
                //if (browser != fromNMElement.browser) throw new ArgumentException("browser mismatch " + browser + "/" + fromNMElement.browser);
                p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
                if (p == null)
                {
                    throw new ArgumentException("fromElement missing cssselector");
                }
                // xpath += p.Value.Substring(1);
                xpath       = "";
                cssselector = fromNMElement.cssselector + " > " + p.Value;
            }

            NMHook.checkForPipes(true, true);
            NMHook.reloadtabs();
            var tabs = NMHook.tabs;

            if (!string.IsNullOrEmpty(browser))
            {
                tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            }
            foreach (var tab in tabs)
            {
                NativeMessagingMessage subresult = null;

                var getelement = new NativeMessagingMessage("getelements");
                getelement.browser = browser;
                getelement.xPath   = xpath;
                getelement.cssPath = cssselector;
                subresult          = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
                if (subresult != null)
                {
                    if (subresult.results != null)
                    {
                        foreach (var b in subresult.results)
                        {
                            if (b.cssPath == "true" || b.xPath == "true")
                            {
                                var data = b.result;
                                var arr  = JArray.Parse(data);
                                foreach (var _e in arr)
                                {
                                    if (results.Count > maxresults)
                                    {
                                        continue;
                                    }
                                    var json         = _e.ToString();
                                    var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject <NativeMessagingMessage>(json);
                                    subsubresult.browser = browser;
                                    subsubresult.result  = json;
                                    subsubresult.tabid   = b.tabid;
                                    subsubresult.tab     = b.tab;
                                    results.Add(new NMElement(subsubresult));
                                }
                            }
                        }
                    }
                }
            }
            return(results.ToArray());
            //NMHook.Instance.refreshJvms();
            //NMElement[] result = null;
            //foreach (var jvm in NMHook.Instance.jvms)
            //{
            //    result = GetElementsWithuiSelector(jvm, selector, fromElement, maxresults);
            //    if (result.Count() > 0) return result;
            //}

            //if (result == null) return new NMElement[] { };
            //return result;
            //return new NMElement[] { };
        }
Example #24
0
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with only 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;
            string    fromcssPath   = "";
            string    fromxPath     = "";

            if (fromElement != null)
            {
                fromcssPath = fromNMElement.cssselector;
                fromxPath   = fromNMElement.xpath;
            }
            //NMHook.checkForPipes(true, true);
            //NMHook.reloadtabs();
            //var tabs = NMHook.tabs.ToList();
            //if (!string.IsNullOrEmpty(browser)) {
            //    lock(NMHook.tabs)
            //    {
            //        tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            //    }
            //}
            //foreach (var tab in tabs)
            //{

            //}
            NativeMessagingMessage subresult = null;

            var getelement = new NativeMessagingMessage("getelements", PluginConfig.debug_console_output);

            getelement.browser     = browser;
            getelement.xPath       = xpath;
            getelement.cssPath     = cssselector;
            getelement.fromxPath   = fromxPath;
            getelement.fromcssPath = fromcssPath;
            if (fromElement != null && fromElement is NMElement)
            {
                getelement.windowId = ((NMElement)fromElement).message.windowId;
                getelement.tabid    = ((NMElement)fromElement).message.tabid;
                getelement.frameId  = ((NMElement)fromElement).message.frameId;
            }
            subresult = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
            if (subresult != null)
            {
                if (subresult.results != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            if (results.Count > maxresults)
                            {
                                continue;
                            }
                            results.Add(new NMElement(b));
                            //var data = b.result;
                            //var arr = JArray.Parse(data.ToString());
                            //foreach (var _e in arr)
                            //{
                            //    if (results.Count > maxresults) continue;
                            //    var json = _e.ToString();
                            //    var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject<NativeMessagingMessage>(json);
                            //    subsubresult.browser = browser;
                            //    subsubresult.result = json;
                            //    subsubresult.tabid = b.tabid;
                            //    subsubresult.tab = b.tab;
                            //    results.Add(new NMElement(subsubresult));
                            //}
                        }
                    }
                }
            }
            return(results.ToArray());
        }
Example #25
0
        protected override void Execute(NativeActivityContext context)
        {
            var eq       = new Activities.NMEqualityComparer();
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            string browser = sel.browser;

            if (WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.tabs.Where(x => x.browser == browser && x.url.ToLower().StartsWith(s.url.ToLower())).FirstOrDefault();
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }

            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }

            var lastelements = context.GetValue(_lastelements);

            if (lastelements == null)
            {
                lastelements = new NMElement[] { }
            }
            ;
            context.SetValue(_lastelements, elements);
            if (lastelements.Length > 0)
            {
                var newelements = new List <NMElement>();
                for (var i = elements.Length - 1; i >= 0; i--)
                {
                    var  element = elements[i];
                    bool exists  = false;
                    foreach (var ele in lastelements)
                    {
                        if (eq.Equals(ele, element))
                        {
                            exists = true;
                            break;
                        }
                        else if (element.ToString() == ele.ToString())
                        {
                            var b = eq.Equals(ele, element);
                        }
                    }
                    if (!exists)
                    {
                        newelements.Insert(0, element);
                    }
                }
                if (elements.Length > 0)
                {
                    elements = newelements.ToArray();

                    newelements = new List <NMElement>();
                    for (var i = elements.Length - 1; i >= 0; i--)
                    {
                        bool exists = false;
                        foreach (var ele in lastelements)
                        {
                            if (eq.Equals(ele, elements[i]))
                            {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists)
                        {
                            newelements.Insert(0, elements[i]);
                        }
                    }
                    elements = newelements.ToArray();
                }
            }
            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 <NMElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            //if (lastelements.Length == elements.Length && lastelements.Length > 0)
            //{
            //    var eq = new Activities.NMEqualityComparer();
            //    more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            //}
            if (more)
            {
                context.SetValue(_elements, _enum);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }

            var allelements = context.GetValue(_allelements);

            if (more)
            {
                if (allelements == null)
                {
                    allelements = new NMElement[] { }
                }
                ;
                var allelementslength = allelements.Length;
                Array.Resize(ref allelements, allelements.Length + elements.Length);
                Array.Copy(elements, 0, allelements, allelementslength, elements.Length);
            }
            context.SetValue(Elements, allelements);
            context.SetValue(_allelements, allelements);
        }
Example #26
0
        protected override async Task <object> ExecuteAsync(NativeActivityContext context)
        {
            IsCancel = false;
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            string      browser  = sel.browser;

            if (WaitForReady != null && WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var allelements = context.GetValue(_allelements);

            if (allelements == null)
            {
                allelements = new NMElement[] { }
            }
            ;
            if (timeout.Minutes > 5 || timeout.Hours > 1)
            {
                Activity _Activity = null;
                try
                {
                    var strProperty = context.GetType().GetProperty("Activity", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    var strGetter   = strProperty.GetGetMethod(nonPublic: true);
                    _Activity = (Activity)strGetter.Invoke(context, null);
                }
                catch (Exception)
                {
                }
                if (_Activity != null)
                {
                    Log.Warning("Timeout for Activity " + _Activity.Id + " is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
                else
                {
                    Log.Warning("Timeout for on of your NM.GetElementAsync is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
            }

            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.FindTabByURL(browser, s.url);
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }
            await Task.Run(() =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                do
                {
                    elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
                    Log.Selector("BEGIN:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");
                    if (allelements.Length > 0)
                    {
                        var newelements = new List <NMElement>();
                        for (var i = elements.Length - 1; i >= 0; i--)
                        {
                            var element = elements[i];
                            if (!allelements.Contains(element))
                            {
                                newelements.Insert(0, element);
                            }
                        }
                        elements = newelements.ToArray();
                    }
                } while (elements.Count() == 0 && sw.Elapsed < timeout && !IsCancel);
                if (IsCancel)
                {
                    Console.WriteLine("was Canceled: true! DisplayName" + DisplayName);
                }
                //if (sw.Elapsed >= timeout)
                //{
                //    Console.WriteLine("Timeout !");
                //}
            });

            return(elements);
        }
Example #27
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            if (timeout.Minutes > 5 || timeout.Hours > 1)
            {
                Activity _Activity = null;
                try
                {
                    var strProperty = context.GetType().GetProperty("Activity", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    var strGetter   = strProperty.GetGetMethod(nonPublic: true);
                    _Activity = (Activity)strGetter.Invoke(context, null);
                }
                catch (Exception)
                {
                }
                if (_Activity != null)
                {
                    Log.Warning("Timeout for Activity " + _Activity.Id + " is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
                else
                {
                    Log.Warning("Timeout for on of your NM.GetElements is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
            }
            NMElement[] elements = { };
            var         sw       = new Stopwatch();

            sw.Start();
            string browser = sel.browser;

            if (WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var allelements = context.GetValue(_allelements);

            if (allelements == null)
            {
                allelements = new NMElement[] { }
            }
            ;

            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.FindTabByURL(browser, s.url);
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }

            do
            {
                elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
                Log.Selector("BEGIN:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");

                // allelements = allelements.Concat(elements).ToArray();
                if (allelements.Length > 0)
                {
                    var newelements = new List <NMElement>();
                    for (var i = elements.Length - 1; i >= 0; i--)
                    {
                        var element = elements[i];
                        if (!allelements.Contains(element))
                        {
                            newelements.Insert(0, element);
                        }
                    }
                    elements = newelements.ToArray();
                    //if(elements.Count() > 20)
                    //{
                    //    for(var i=0; i < allelements.Length && i < elements.Length; i++)
                    //    {
                    //        if (!eq.Equals(allelements[i], elements[i]) || allelements[i].GetHashCode() != elements[i].GetHashCode())
                    //        {
                    //            Log.Output(allelements[i].GetHashCode() + " / " + elements[i].GetHashCode());
                    //        }

                    //    }

                    //}
                }
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }

            if ((elements.Length + allelements.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 <NMElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            //if (lastelements.Length == elements.Length && lastelements.Length > 0)
            //{
            //    var eq = new Activities.NMEqualityComparer();
            //    more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            //}
            if (more)
            {
                allelements = allelements.Concat(elements).ToArray();
                var eq = new Activities.NMEqualityComparer();
                allelements = allelements.Distinct(eq).ToArray();

                //var allelementslength = allelements.Length;
                //Array.Resize(ref allelements, allelements.Length + elements.Length);
                //Array.Copy(elements, 0, allelements, allelementslength, elements.Length);
            }

            context.SetValue(_allelements, allelements);
            context.SetValue(Elements, allelements);
            Log.Selector("END:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");

            if (more)
            {
                context.SetValue(_elements, _enum);
                IncIndex(context);
                SetTotal(context, allelements.Length);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
        }
Example #28
0
        //private static NMElement[] GetElementsWithuiSelector(WindowsAccessBridgeInterop.AccessibleJvm jvm, NMSelector selector, IElement fromElement, int maxresults)
        //{
        //    NMElement[] result = null;
        //    NMElement _fromElement = fromElement as NMElement;
        //    var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
        //    var current = new List<NMElement>();
        //    NMElement startfrom = null;
        //    if (_fromElement != null) startfrom = _fromElement;
        //    if (startfrom == null) startfrom = new NMElement(jvm);
        //    current.Add(startfrom);
        //    for (var i = 0; i < selectors.Count; i++)
        //    {
        //        var sw = new System.Diagnostics.Stopwatch();
        //        sw.Start();
        //        var s = new NMSelectorItem(selectors[i]);
        //        Log.Selector(string.Format("OpenRPA.NM::GetElementsWithuiSelector::Find for selector {0} {1}", i, s.ToString()));
        //        var elements = new List<NMElement>();
        //        elements.AddRange(current);
        //        current.Clear();
        //        foreach (var _element in elements)
        //        {
        //            result = ((NMSelectorItem)s).matches(_element);
        //            current.AddRange(result);
        //        }
        //        if (i == (selectors.Count - 1)) result = current.ToArray();
        //        if (current.Count == 0)
        //        {
        //            var _c = new NMSelectorItem(selectors[i]);
        //            var message = "needed to find " + Environment.NewLine + _c.ToString() + Environment.NewLine + "but found only: " + Environment.NewLine;
        //            foreach (var element in elements)
        //            {
        //                var children = element.Children;
        //                foreach (var c in children)
        //                {
        //                    try
        //                    {
        //                        message += c.ToString() + Environment.NewLine;
        //                    }
        //                    catch (Exception)
        //                    {
        //                    }
        //                }
        //            }
        //            Log.Selector(message);
        //            return new NMElement[] { };
        //        }
        //        Log.Selector(string.Format("OpenRPA.NM::GetElement::found {1} for selector {2} in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count(), i));
        //    }
        //    if (result == null) return new NMElement[] { };
        //    return result;
        //}
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var    results = new List <NMElement>();
            var    first   = selector[0];
            var    second  = selector[1];
            var    p       = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
            string browser = "";

            if (p != null)
            {
                browser = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }

            NMHook.checkForPipes(true, true);
            NMHook.reloadtabs();
            var tabs = NMHook.tabs;

            if (!string.IsNullOrEmpty(browser))
            {
                tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            }
            foreach (var tab in tabs)
            {
                NativeMessagingMessage subresult = null;

                var getelement = new NativeMessagingMessage("getelements");
                getelement.browser = browser;
                getelement.xPath   = xpath;
                getelement.cssPath = cssselector;
                subresult          = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
                if (subresult != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            var data = b.result;
                            var arr  = JArray.Parse(data);
                            foreach (var _e in arr)
                            {
                                var json         = _e.ToString();
                                var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject <NativeMessagingMessage>(json);
                                subsubresult.browser = browser;
                                subsubresult.result  = json;
                                subsubresult.tabid   = b.tabid;
                                subsubresult.tab     = b.tab;
                                results.Add(new NMElement(subsubresult));
                            }
                        }
                    }
                }
            }
            return(results.ToArray());
            //NMHook.Instance.refreshJvms();
            //NMElement[] result = null;
            //foreach (var jvm in NMHook.Instance.jvms)
            //{
            //    result = GetElementsWithuiSelector(jvm, selector, fromElement, maxresults);
            //    if (result.Count() > 0) return result;
            //}

            //if (result == null) return new NMElement[] { };
            //return result;
            //return new NMElement[] { };
        }
Example #29
0
        protected override async Task <object> ExecuteAsync(NativeActivityContext context)
        {
            IsCancel = false;
            var selector = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            NMElement[] elements = { };
            string      browser  = sel.browser;

            if (WaitForReady != null && WaitForReady.Get(context))
            {
                if (from != null)
                {
                    browser = from.browser;
                }
                DoWaitForReady(browser);
            }
            var allelements = context.GetValue(_allelements);

            if (allelements == null)
            {
                allelements = new NMElement[] { }
            }
            ;

            var s = new NMSelectorItem(sel[0]);

            if (!string.IsNullOrEmpty(s.url))
            {
                var tab = NMHook.FindTabByURL(browser, s.url);
                if (tab != null)
                {
                    if (!tab.highlighted || !tab.selected)
                    {
                        var _tab = NMHook.selecttab(browser, tab.id);
                    }
                }
            }
            await Task.Run(() =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                do
                {
                    elements = NMSelector.GetElementsWithuiSelector(sel, from, maxresults);
                    Log.Selector("BEGIN:: I have " + elements.Count() + " elements, and " + allelements.Count() + " in all elements");
                    if (allelements.Length > 0)
                    {
                        var newelements = new List <NMElement>();
                        for (var i = elements.Length - 1; i >= 0; i--)
                        {
                            var element = elements[i];
                            if (!allelements.Contains(element))
                            {
                                newelements.Insert(0, element);
                            }
                        }
                        elements = newelements.ToArray();
                    }
                } while (elements.Count() == 0 && sw.Elapsed < timeout && !IsCancel);
                if (IsCancel)
                {
                    Console.WriteLine("was Canceled: true! DisplayName" + DisplayName);
                }
                //if (sw.Elapsed >= timeout)
                //{
                //    Console.WriteLine("Timeout !");
                //}
            });

            return(elements);
        }
Example #30
0
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with only 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;
            string    fromcssPath   = "";
            string    fromxPath     = "";

            if (fromElement != null)
            {
                fromcssPath = fromNMElement.cssselector;
                fromxPath   = fromNMElement.xpath;
            }
            NativeMessagingMessage subresult = null;

            var getelement = new NativeMessagingMessage("getelements", PluginConfig.debug_console_output, PluginConfig.unique_xpath_ids);

            getelement.browser     = browser;
            getelement.xPath       = xpath;
            getelement.cssPath     = cssselector;
            getelement.fromxPath   = fromxPath;
            getelement.fromcssPath = fromcssPath;
            if (fromElement != null && fromElement is NMElement)
            {
                getelement.windowId = ((NMElement)fromElement).message.windowId;
                getelement.tabid    = ((NMElement)fromElement).message.tabid;
                getelement.frameId  = ((NMElement)fromElement).message.frameId;
            }
            subresult = NMHook.sendMessageResult(getelement, PluginConfig.protocol_timeout);
            if (subresult != null)
            {
                if (subresult.results != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            if (results.Count > maxresults)
                            {
                                continue;
                            }
                            var nme = new NMElement(b);
                            results.Add(nme);
                        }
                    }
                }
            }
            return(results.ToArray());
        }