Exemple #1
0
        public static treeelement[] _GetRootElements(Selector anchor)
        {
            var result = new List <treeelement>();

            Javahook.Instance.refreshJvms();
            if (anchor != null)
            {
                JavaSelector Javaselector = anchor as JavaSelector;
                if (Javaselector == null)
                {
                    Javaselector = new JavaSelector(anchor.ToString());
                }
                var elements = JavaSelector.GetElementsWithuiSelector(Javaselector, null, 1);
                foreach (var _ele in elements)
                {
                    var e = new JavaTreeElement(null, true, _ele);
                    result.Add(e);
                }
                return(result.ToArray());
            }
            else
            {
                foreach (var jvm in Javahook.Instance.jvms)
                {
                    result.Add(new JavaTreeElement(null, true, new JavaElement(jvm)));
                }
            }
            return(result.ToArray());
        }
        private void Open_Selector(object sender, RoutedEventArgs e)
        {
            ModelItem    loadFrom = ModelItem.Parent;
            string       loadFromSelectorString = "";
            JavaSelector 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 JavaSelector(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 JavaSelector(SelectorString);
                selectors = new Interfaces.Selector.SelectorWindow("Java", selector, anchor, maxresults);
            }
            else
            {
                var selector = new JavaSelector("[{Selector: 'Java'}]");
                selectors = new Interfaces.Selector.SelectorWindow("Java", 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 <JavaElement>()
                    {
                        Expression = new Microsoft.VisualBasic.Activities.VisualBasicValue <JavaElement>("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)")
                    });
                }
            }
        }
Exemple #3
0
        public static treeelement[] _GetRootElements(Selector anchor)
        {
            var result = new List <treeelement>();

            Javahook.Instance.refreshJvms();
            if (anchor != null)
            {
                if (!(anchor is JavaSelector Javaselector))
                {
                    Javaselector = new JavaSelector(anchor.ToString());
                }
                var elements = JavaSelector.GetElementsWithuiSelector(Javaselector, null, 1);
                foreach (var _ele in elements)
                {
                    var e = new JavaTreeElement(null, true, _ele);
                    result.Add(e);
                }
                return(result.ToArray());
            }
            else
            {
                foreach (var jvm in Javahook.Instance.jvms)
                {
                    var item = new JavaTreeElement(null, true, new JavaElement(jvm));
                    result.Add(item);
                    foreach (var e in Plugin.EnumRoots(jvm))
                    {
                        item.Children.Add(new JavaTreeElement(item, true, e));
                        // result.Add(new JavaTreeElement(item, true, e));
                    }
                }
            }
            return(result.ToArray());
        }
Exemple #4
0
        protected override void Execute(NativeActivityContext context)
        {
            var SelectorString = Selector.Get(context);
            SelectorString = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(SelectorString, context.DataContext);
            var sel = new JavaSelector(SelectorString);
            var timeout = TimeSpan.FromSeconds(3);
            var maxresults = MaxResults.Get(context);
            var from = From.Get(context);
            if (maxresults < 1) maxresults = 1;

            JavaElement[] elements = { };
            var sw = new Stopwatch();
            sw.Start();
            do
            {
                var selector = new JavaSelector(SelectorString);
                elements = JavaSelector.GetElementsWithuiSelector(selector, from, maxresults);

            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            Log.Debug(string.Format("OpenRPA.Java::GetElement::found {1} elements in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count()));
            context.SetValue(Elements, elements);
            IEnumerator<JavaElement> _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");
            }
        }
Exemple #5
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() != "java")
            {
                return(false);
            }
            if (lastElement == null)
            {
                return(false);
            }

            var selector = new JavaSelector(lastElement, null);

            var a = new GetElement {
                DisplayName = lastElement.id + " " + lastElement.role + " " + lastElement.Name
            };

            a.Selector = selector.ToString();

            e.a            = new GetElementResult(a);
            e.SupportInput = lastElement.SupportInput;
            return(false);
        }
        private void Highlight_Click(object sender, RoutedEventArgs e)
        {
            ModelItem    loadFrom = ModelItem.Parent;
            string       loadFromSelectorString = "";
            JavaSelector 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 JavaSelector(loadFromSelectorString);
                    break;
                }
                loadFrom = loadFrom.Parent;
            }

            HighlightImage = Extensions.GetImageSourceFromResource(".x.png");
            NotifyPropertyChanged("HighlightImage");
            string SelectorString = ModelItem.GetValue <string>("Selector");
            int    maxresults     = ModelItem.GetValue <int>("MaxResults");

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            var selector = new JavaSelector(SelectorString);

            var elements = new List <JavaElement>();

            if (anchor != null)
            {
                var _base = JavaSelector.GetElementsWithuiSelector(anchor, null, 10);
                foreach (var _e in _base)
                {
                    var res = JavaSelector.GetElementsWithuiSelector(selector, _e, maxresults);
                    elements.AddRange(res);
                }
            }
            else
            {
                var res = JavaSelector.GetElementsWithuiSelector(selector, null, maxresults);
                elements.AddRange(res);
            }
            if (elements.Count() > maxresults)
            {
                elements = elements.ToList().Take(maxresults).ToList();
            }
            if (elements.Count() > 0)
            {
                HighlightImage = Extensions.GetImageSourceFromResource("check.png");
                NotifyPropertyChanged("HighlightImage");
            }
            foreach (var ele in elements)
            {
                ele.Highlight(false, System.Drawing.Color.Red, TimeSpan.FromSeconds(1));
            }
        }
Exemple #7
0
        public Interfaces.Selector.Selector GetSelector(Selector anchor, Interfaces.Selector.treeelement item)
        {
            var          javaitem   = item as JavaTreeElement;
            JavaSelector javaanchor = anchor as JavaSelector;

            if (javaanchor == null && anchor != null)
            {
                javaanchor = new JavaSelector(anchor.ToString());
            }
            return(new JavaSelector(javaitem.JavaElement, javaanchor, true));
        }
        private void Highlight_Click(object sender, RoutedEventArgs e)
        {
            string SelectorString = ModelItem.GetValue <string>("Selector");
            int    maxresults     = ModelItem.GetValue <int>("MaxResults");
            var    selector       = new JavaSelector(SelectorString);
            var    elements       = JavaSelector.GetElementsWithuiSelector(selector, null, maxresults);

            foreach (var ele in elements)
            {
                ele.Highlight(false, System.Drawing.Color.Red, TimeSpan.FromSeconds(1));
            }
        }
Exemple #9
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var SelectorString = Selector.Get(context);

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

            if (maxresults < 1)
            {
                maxresults = 1;
            }

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

            sw.Start();
            do
            {
                var selector = new JavaSelector(SelectorString);
                elements = JavaSelector.GetElementsWithuiSelector(selector, from, maxresults);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            Log.Debug(string.Format("OpenRPA.Java::GetElement::found {1} elements in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count()));
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }
            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)");
            }
            context.SetValue(Elements, elements);
            IEnumerator <JavaElement> _enum = elements.ToList().GetEnumerator();

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

            if (more)
            {
                IncIndex(context);
                SetTotal(context, elements.Length);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
        }
Exemple #10
0
        public static JavaElement[] GetElementsWithuiSelector(JavaSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            Javahook.Instance.refreshJvms();
            var result = new List <JavaElement>();

            foreach (var jvm in Javahook.Instance.jvms)
            {
                result.AddRange(GetElementsWithuiSelector(jvm, selector, fromElement, maxresults));

                if (result.Count > maxresults)
                {
                    return(result.ToArray());
                }
            }
            return(result.ToArray());
        }
Exemple #11
0
        private void Hook_OnMouseClicked(int vmID, WindowsAccessBridgeInterop.AccessibleContextNode ac)
        {
            lastElement = new JavaElement(ac);
            lastElement.SetPath();
            Log.Debug("OnMouseClicked: " + lastElement.id + " " + lastElement.role + " " + lastElement.Name);
            if (lastElement == null)
            {
                return;
            }

            var re = new RecordEvent(); re.Button = MouseButton.Left;
            var a  = new GetElement {
                DisplayName = lastElement.title
            };
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            JavaSelector sel = null;

            // sel = new JavaSelector(e.Element.rawElement, null, true);
            sel = new JavaSelector(lastElement, null, true);
            if (sel == null)
            {
                return;
            }
            if (sel.Count < 2)
            {
                return;
            }
            a.Selector      = sel.ToString();
            a.Image         = lastElement.ImageString();
            a.MaxResults    = 1;
            re.Element      = lastElement;
            re.Selector     = sel;
            re.X            = lastElement.X;
            re.Y            = lastElement.Y;
            re.a            = new GetElementResult(a);
            re.SupportInput = lastElement.SupportInput;

            Log.Debug(string.Format("Java.Recording::OnMouseClicked::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            OnUserAction?.Invoke(this, re);
        }
Exemple #12
0
        public bool parseUserAction(ref IRecordEvent e)
        {
            if (lastElement == null)
            {
                return(false);
            }
            if (e.UIElement == null)
            {
                return(false);
            }

            if (e.UIElement.ClassName == null || !e.UIElement.ClassName.StartsWith("SunAwt"))
            {
                if (e.UIElement.ProcessId < 1)
                {
                    return(false);
                }
                var p = System.Diagnostics.Process.GetProcessById(e.UIElement.ProcessId);
                if (p.ProcessName.ToLower() != "java")
                {
                    return(false);
                }
            }
            var selector = new JavaSelector(lastElement, null, true);
            var a        = new GetElement {
                DisplayName = lastElement.id + " " + lastElement.role + " " + lastElement.Name
            };

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

            e.a            = new GetElementResult(a);
            e.SupportInput = lastElement.SupportInput;
            e.ClickHandled = true;
            e.Selector     = selector;
            e.Element      = lastElement;
            lastElement.Click(true, e.Button, 0, 0);
            return(true);
        }
Exemple #13
0
        public static JavaElement[] GetElementsWithuiSelector(JavaSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            Javahook.Instance.refreshJvms();
            JavaElement[] result = null;
            foreach (var jvm in Javahook.Instance.jvms)
            {
                result = GetElementsWithuiSelector(jvm, selector, fromElement, maxresults);

                if (result.Count() > 0)
                {
                    return(result);
                }
            }

            if (result == null)
            {
                return new JavaElement[] { }
            }
            ;
            return(result);
        }
    }
        private void Hook_OnMouseClicked(int vmID, WindowsAccessBridgeInterop.AccessibleContextNode ac)
        {
            if (string.IsNullOrEmpty(Selector))
            {
                return;
            }
            var element = new JavaElement(ac);

            element.SetPath();
            Log.Debug("OnMouseClicked: " + element.id + " " + element.role + " " + element.Name);

            var pathToRoot = new List <JavaElement>();

            while (element != null)
            {
                if (pathToRoot.Contains(element))
                {
                    break;
                }
                try
                {
                    if (element.Parent != null)
                    {
                        pathToRoot.Add(element);
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    element = element.Parent;
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "");
                    return;
                }
            }
            JavaSelector selector = new JavaSelector(Selector);

            if (pathToRoot.Count < (selector.Count - 1))
            {
                return;
            }
            if (pathToRoot.Count > (selector.Count - 1))
            {
                return;
            }
            pathToRoot.Reverse();
            for (var i = 0; i < pathToRoot.Count; i++)
            {
                element = pathToRoot[i];
                if (selector.Count > (i + 1))
                {
                    JavaSelectorItem s = new JavaSelectorItem(selector[(i + 1)]);
                    if (!s.Match(element))
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            var _e = new DetectorEvent(element);

            OnDetector?.Invoke(this, _e, EventArgs.Empty);
        }
Exemple #15
0
 public override IElement[] GetElements(IElement fromElement = null, int maxresults = 1)
 {
     return(JavaSelector.GetElementsWithuiSelector(this, fromElement, maxresults));
 }
Exemple #16
0
        private static JavaElement[] GetElementsWithuiSelector(WindowsAccessBridgeInterop.AccessibleJvm jvm, JavaSelector selector, IElement fromElement, int maxresults)
        {
            JavaElement[] result       = null;
            JavaElement   _fromElement = fromElement as JavaElement;
            var           selectors    = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
            var           current      = new List <JavaElement>();
            JavaElement   startfrom    = null;

            if (_fromElement != null)
            {
                startfrom = _fromElement;
            }
            if (startfrom == null)
            {
                startfrom = new JavaElement(jvm);
            }
            current.Add(startfrom);
            for (var i = 0; i < selectors.Count; i++)
            {
                var sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                var s = new JavaSelectorItem(selectors[i]);
                Log.Selector(string.Format("OpenRPA.Java::GetElementsWithuiSelector::Find for selector {0} {1}", i, s.ToString()));
                var elements = new List <JavaElement>();
                elements.AddRange(current);
                current.Clear();
                foreach (var _element in elements)
                {
                    result = ((JavaSelectorItem)s).matches(_element);
                    current.AddRange(result);
                }
                if (current.Count == 0)
                {
                    // TODO: Figure out, why this is needed when working with Java Menu's
                    foreach (var _e in elements)
                    {
                        if (s.Match(_e))
                        {
                            current.Add(_e);
                        }
                    }
                }
                if (i == (selectors.Count - 1))
                {
                    result = current.ToArray();
                }
                if (current.Count == 0)
                {
                    var _c      = new JavaSelectorItem(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 JavaElement[] { });
                }
                Log.Selector(string.Format("OpenRPA.Java::GetElement::found {1} for selector {2} in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count(), i));
            }
            if (result == null)
            {
                return new JavaElement[] { }
            }
            ;
            return(result);
        }
Exemple #17
0
        public IElement[] GetElementsWithSelector(Selector selector, IElement fromElement = null, int maxresults = 1)
        {
            var result = JavaSelector.GetElementsWithuiSelector(selector as JavaSelector, fromElement, maxresults);

            return(result);
        }
Exemple #18
0
        public JavaSelector(JavaElement element, JavaSelector anchor, bool doEnum)
        {
            var sw = new System.Diagnostics.Stopwatch();

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

            JavaElement root        = null;
            JavaElement baseElement = null;
            var         pathToRoot  = new List <JavaElement>();

            while (element != null)
            {
                // Break on circular relationship (should not happen?)
                //if (pathToRoot.Contains(element) || element.Equals(_rootElement)) { break; }
                if (pathToRoot.Contains(element))
                {
                    break;
                }
                if (element.Parent != null)
                {
                    pathToRoot.Add(element);
                }
                if (element.Parent == null)
                {
                    root = element;
                }
                try
                {
                    element = element.Parent;
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "");
                    return;
                }
            }
            pathToRoot.Reverse();

            if (anchor != null)
            {
                var anchorlist = anchor.Where(x => x.Enabled && x.Selector == null).ToList();
                for (var i = 0; i < anchorlist.Count; i++)
                {
                    //if (((JavaSelectorItem)anchorlist[i]).Match(pathToRoot[0]))
                    if (JavaSelectorItem.Match(anchorlist[i], pathToRoot[0]))
                    {
                        pathToRoot.Remove(pathToRoot[0]);
                    }
                    else
                    {
                        Log.Warning("Element does not match the anchor path");
                        return;
                    }
                }
            }
            if (pathToRoot.Count == 0)
            {
                Log.Error("Element is same as annchor");
                return;
            }

            baseElement = pathToRoot.First();
            element     = pathToRoot.Last();
            Clear();
            JavaSelectorItem item;

            if (anchor == null)
            {
                item            = new JavaSelectorItem(root, true);
                item.Enabled    = true;
                item.canDisable = false;
                Items.Add(item);
            }
            for (var i = 0; i < pathToRoot.Count(); i++)
            {
                var o = pathToRoot[i];
                item = new JavaSelectorItem(o, false);
                if (i == 0 || i == (pathToRoot.Count() - 1))
                {
                    item.canDisable = false;
                }
                if (doEnum)
                {
                    item.EnumNeededProperties(o, o.Parent);
                }
                Items.Add(item);
            }
            pathToRoot.Reverse();

            Log.Selector(string.Format("Javaselector::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));
        }
Exemple #19
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var SelectorString = Selector.Get(context);

            SelectorString = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(SelectorString, context.DataContext);
            var sel     = new JavaSelector(SelectorString);
            var timeout = Timeout.Get(context);

            if (Timeout == null || Timeout.Expression == null)
            {
                timeout = TimeSpan.FromSeconds(3);
            }
            var maxresults = MaxResults.Get(context);
            var minresults = MinResults.Get(context);
            var from       = From.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 Java.GetElements is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
            }

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

            sw.Start();
            do
            {
                var selector = new JavaSelector(SelectorString);
                elements = JavaSelector.GetElementsWithuiSelector(selector, from, maxresults);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            Log.Debug(string.Format("OpenRPA.Java::GetElement::found {1} elements in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count()));
            if (elements.Count() > maxresults)
            {
                elements = elements.Take(maxresults).ToArray();
            }
            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)");
            }
            context.SetValue(Elements, elements);
            IEnumerator <JavaElement> _enum = elements.ToList().GetEnumerator();

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

            if (more)
            {
                IncIndex(context);
                SetTotal(context, elements.Length);
                context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            }
        }