Esempio n. 1
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[] { };
        }
 set => SetValue(SelectorItemProperty, value);
Esempio n. 3
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());
        }
Esempio n. 4
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());
        }