Beispiel #1
0
 public SAPTreeElement(treeelement parent, bool expanded, SAPElement element) : base(parent)
 {
     SAPElement = element;
     IsExpanded = expanded;
     Element    = element;
     Name       = element.Name;
 }
Beispiel #2
0
        private static SAPElement[] GetElementsWithuiSelector(SAPSession session, SAPSelector selector, IElement fromElement, int maxresults)
        {
            var        result       = new List <SAPElement>();
            SAPElement _fromElement = fromElement as SAPElement;

            var root       = new SAPSelectorItem(selector[0]);
            var sel        = new SAPSelectorItem(selector[1]);
            var SystemName = root.SystemName;
            var id         = sel.id;

            var msg = new SAPEvent("getitem");

            msg.Set(new SAPEventElement()
            {
                Id = id, SystemName = SystemName
            });
            msg = SAPhook.Instance.SendMessage(msg, TimeSpan.FromSeconds(5));
            if (msg != null)
            {
                var ele    = msg.Get <SAPEventElement>();
                var Parent = new SAPElement(null, ele);
                result.Add(Parent);
            }
            return(result.ToArray());
        }
Beispiel #3
0
        public SAPElement[] matches(SAPElement element)
        {
            int counter = 0;

            do
            {
                try
                {
                    var          matchs   = new List <SAPElement>();
                    SAPElement[] elements = element.Children;
                    foreach (SAPElement elementNode in elements)
                    {
                        if (Match(elementNode))
                        {
                            matchs.Add(elementNode);
                        }
                    }
                    Log.Selector("match count: " + matchs.Count);
                    return(matchs.ToArray());
                }
                catch (Exception)
                {
                    ++counter;
                    if (counter == 2)
                    {
                        throw;
                    }
                }
            } while (counter < 2);
            return(new SAPElement[] { });
        }
Beispiel #4
0
        public SAPSelector(SAPElement element, SAPSelector anchor, bool doEnum)
        {
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            Log.Selector(string.Format("SAPselector::AutomationElement::begin {0:mm\\:ss\\.fff}", sw.Elapsed));
            Log.Selector(string.Format("SAPselector::GetControlVSAPwWalker::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            Clear();
            SAPSelectorItem item;

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

            if (idfield.Contains("/"))
            {
                idfield = idfield.Substring(idfield.LastIndexOf("/") + 1);
            }
            item.Properties.Add(new SelectorItemProperty("idfield", idfield));
            Items.Add(item);
            Log.Selector(string.Format("SAPselector::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));
        }
Beispiel #5
0
        private static SAPElement[] GetElementsWithuiSelector(SAPSession session, SAPSelector selector, IElement fromElement, int skip, int maxresults, bool FlatternGuiTree)
        {
            var        result       = new List <SAPElement>();
            SAPElement _fromElement = fromElement as SAPElement;

            var root       = new SAPSelectorItem(selector[0]);
            var sel        = new SAPSelectorItem(selector[1]);
            var SystemName = root.SystemName;
            var id         = sel.id;
            var path       = sel.path;

            var msg = new SAPEvent("getitem");

            msg.Set(new SAPEventElement()
            {
                Id = id, SystemName = SystemName, GetAllProperties = true, Path = path, Skip = skip, MaxItem = maxresults, Flat = FlatternGuiTree
            });
            msg = SAPhook.Instance.SendMessage(msg, TimeSpan.FromSeconds(PluginConfig.bridge_timeout_seconds));
            if (msg != null)
            {
                var ele = msg.Get <SAPEventElement>();
                if (!string.IsNullOrEmpty(ele.Id))
                {
                    var _element = new SAPElement(null, ele);
                    result.Add(_element);
                }
            }
            return(result.ToArray());
        }
Beispiel #6
0
 public SAPElementUpdatableProperty(SAPElement Element, SAPElementProperty p)
 {
     this.Element = Element;
     IsReadOnly   = p.IsReadOnly;
     Name         = p.Name;
     _Value       = p.Value;
 }
Beispiel #7
0
        public SAPSelectorItem(SAPElement element, bool isRoot)
        {
            this.Element = element;
            string n = null;

            if (!string.IsNullOrEmpty(element.Name))
            {
                n = element.Name;
            }
            Properties = new ObservableCollection <SelectorItemProperty>();
            if (isRoot)
            {
                Properties.Add(new SelectorItemProperty("Selector", "SAP"));
                if (!string.IsNullOrEmpty(element.SystemName))
                {
                    Properties.Add(new SelectorItemProperty("SystemName", element.SystemName));
                }
                Enabled    = true;
                canDisable = false;
                return;
            }
            if (!string.IsNullOrEmpty(element.id))
            {
                Properties.Add(new SelectorItemProperty("id", element.id));
            }
            if (!string.IsNullOrEmpty(element.Path))
            {
                Properties.Add(new SelectorItemProperty("path", element.Path));
            }
            if (!string.IsNullOrEmpty(element.Cell))
            {
                Properties.Add(new SelectorItemProperty("cell", element.Cell));
            }
            //if (!string.IsNullOrEmpty(element.Name)) Properties.Add(new SelectorItemProperty("Name", element.Name));
            //if (!string.IsNullOrEmpty(element.Role)) Properties.Add(new SelectorItemProperty("Role", element.Role));
            //if (!string.IsNullOrEmpty(element.Tip)) Properties.Add(new SelectorItemProperty("Tip", element.Tip));
            Enabled = (Properties.Count > 1);
            foreach (var p in Properties)
            {
                p.Enabled    = true;
                p.canDisable = true;
            }
            ;
            foreach (var p in Properties)
            {
                p.PropertyChanged += (sender, e) =>
                {
                    OnPropertyChanged("Displayname");
                    OnPropertyChanged("json");
                }
            }
            ;
            canDisable = true;
        }
Beispiel #8
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var SelectorString = Selector.Get(context);

            SelectorString = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(SelectorString, context.DataContext);
            var sel             = new SAPSelector(SelectorString);
            var timeout         = Timeout.Get(context);
            var maxresults      = MaxResults.Get(context);
            var minresults      = MinResults.Get(context);
            var flatternguitree = FlatternGuiTree.Get(context);
            //var from = From.Get(context);
            SAPElement from = null;

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

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

            sw.Start();
            do
            {
                var selector = new SAPSelector(SelectorString);
                elements = SAPSelector.GetElementsWithuiSelector(selector, from, 0, maxresults, flatternguitree);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            Log.Debug(string.Format("OpenRPA.SAP::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 <SAPElement> _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);
            }
        }
Beispiel #9
0
 public void loadImageAsync(string Id, string SystemName, string statusBarText)
 {
     Task.Run(() =>
     {
         StatusBarText = statusBarText;
         var msg       = new SAPEvent("getitem");
         msg.Set(new SAPEventElement()
         {
             Id = Id, SystemName = SystemName, GetAllProperties = false
         });
         msg = SAPhook.Instance.SendMessage(msg, TimeSpan.FromSeconds(PluginConfig.bridge_timeout_seconds));
         if (msg != null)
         {
             var ele = msg.Get <SAPEventElement>();
             if (!string.IsNullOrEmpty(ele.Id))
             {
                 var _element = new SAPElement(null, ele);
                 Image        = _element.ImageString();
                 var message  = _element.Properties.Where(x => x.Key == "Tooltip").FirstOrDefault();
                 if (message.Value != null && !string.IsNullOrEmpty(message.Value.Value))
                 {
                     this.message = message.Value.Value;
                 }
                 var tooltip = _element.Properties.Where(x => x.Key == "DefaultTooltip").FirstOrDefault();
                 if (tooltip.Value != null && !string.IsNullOrEmpty(tooltip.Value.Value))
                 {
                     this.tooltip = tooltip.Value.Value;
                 }
                 DisplayName = "Set " + Id.Substring(Id.LastIndexOf("/") + 1);
             }
             else
             {
                 Log.Debug("loadImageAsync: Fail locating " + Id + " returned null id");
             }
         }
         else
         {
             Log.Debug("loadImageAsync: Fail locating " + Id);
         }
     });
 }
Beispiel #10
0
 //public string Name
 //{
 //    get
 //    {
 //        var e = Properties.Where(x => x.Name == "Name").FirstOrDefault();
 //        if (e == null) return null;
 //        return e.Value;
 //    }
 //}
 //public string Role
 //{
 //    get
 //    {
 //        var e = Properties.Where(x => x.Name == "Role").FirstOrDefault();
 //        if (e == null) return null;
 //        return e.Value;
 //    }
 //}
 //public string Tip
 //{
 //    get
 //    {
 //        var e = Properties.Where(x => x.Name == "Tip").FirstOrDefault();
 //        if (e == null) return null;
 //        return e.Value;
 //    }
 //}
 //public string IndexInParent
 //{
 //    get
 //    {
 //        var e = Properties.Where(x => x.Name == "IndexInParent").FirstOrDefault();
 //        if (e == null) return null;
 //        return e.Value;
 //    }
 //}
 public SAPSelectorItem(SAPElement element)
 {
     this.Element = element;
     if (this.Element == null)
     {
         throw new Exception("Error!!!");
     }
     Properties = new ObservableCollection <SelectorItemProperty>();
     if (!string.IsNullOrEmpty(element.Name))
     {
         Properties.Add(new SelectorItemProperty("Name", element.Name));
     }
     if (!string.IsNullOrEmpty(element.Role))
     {
         Properties.Add(new SelectorItemProperty("Role", element.Role));
     }
     if (!string.IsNullOrEmpty(element.Tip))
     {
         Properties.Add(new SelectorItemProperty("Tip", element.Tip));
     }
     if (!string.IsNullOrEmpty(element.id))
     {
         Properties.Add(new SelectorItemProperty("id", element.id));
     }
     foreach (var p in Properties)
     {
         p.Enabled    = true;
         p.canDisable = (Properties.Count > 1);
     }
     ;
     foreach (var p in Properties)
     {
         p.PropertyChanged += (sender, e) =>
         {
             OnPropertyChanged("Displayname");
             OnPropertyChanged("json");
         }
     }
     ;
 }
Beispiel #11
0
 public void loadImageAsync(string Id, string SystemName, string ActionName, string statusBarText)
 {
     Task.Run(() =>
     {
         StatusBarText = statusBarText;
         var msg       = new SAPEvent("getitem");
         msg.Set(new SAPEventElement()
         {
             Id = Id, SystemName = SystemName, GetAllProperties = false
         });
         msg = SAPhook.Instance.SendMessage(msg, TimeSpan.FromSeconds(PluginConfig.bridge_timeout_seconds));
         if (msg != null)
         {
             var ele = msg.Get <SAPEventElement>();
             if (!string.IsNullOrEmpty(ele.Id))
             {
                 var _element = new SAPElement(null, ele);
                 Image        = _element.ImageString();
                 DisplayName  = ActionName + " " + Id.Substring(Id.LastIndexOf("/") + 1);
             }
         }
     });
 }
Beispiel #12
0
        public SAPSelector(SAPElement element, SAPSelector anchor, bool doEnum)
        {
            var sw = new System.Diagnostics.Stopwatch();

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

            Clear();
            SAPSelectorItem item;

            if (anchor == null)
            {
                item            = new SAPSelectorItem(element, true);
                item.Enabled    = true;
                item.canDisable = false;
                Items.Add(item);
            }
            item         = new SAPSelectorItem(element, false);
            item.Enabled = true; item.canDisable = false;
            Items.Add(item);
            //for (var i = 0; i < pathToRoot.Count(); i++)
            //{
            //    var o = pathToRoot[i];
            //    item = new SAPSelectorItem(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("SAPselector::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));
        }
Beispiel #13
0
 public static bool Match(SelectorItem item, SAPElement m)
 {
     foreach (var p in item.Properties.Where(x => x.Enabled == true && x.Value != null))
     {
         if (p.Name == "Name")
         {
             if (!string.IsNullOrEmpty(m.Name))
             {
                 var v = m.Name;
                 if (!PatternMatcher.FitsMask(v, p.Value))
                 {
                     Log.Selector(p.Name + " mismatch '" + v + "' / '" + p.Value + "'");
                     return(false);
                 }
             }
             else
             {
                 Log.Selector(p.Name + " does not exists, but needed value '" + p.Value + "'");
                 return(false);
             }
         }
         if (p.Name == "Role")
         {
             if (!string.IsNullOrEmpty(m.Role))
             {
                 var v = m.Role;
                 if (!PatternMatcher.FitsMask(m.Role, p.Value))
                 {
                     Log.Selector(p.Name + " mismatch '" + v + "' / '" + p.Value + "'");
                     return(false);
                 }
             }
             else
             {
                 Log.Selector(p.Name + " does not exists, but needed value '" + p.Value + "'");
                 return(false);
             }
         }
         if (p.Name == "Tip")
         {
             if (!string.IsNullOrEmpty(m.Tip))
             {
                 var v = m.Tip;
                 if (!PatternMatcher.FitsMask(m.Tip, p.Value))
                 {
                     Log.Selector(p.Name + " mismatch '" + v + "' / '" + p.Value + "'");
                     return(false);
                 }
             }
             else
             {
                 Log.Selector(p.Name + " does not exists, but needed value '" + p.Value + "'");
                 return(false);
             }
         }
         if (p.Name == "Id")
         {
             if (!string.IsNullOrEmpty(m.id))
             {
                 var v = m.id;
                 if (!PatternMatcher.FitsMask(m.id, p.Value))
                 {
                     Log.Selector(p.Name + " mismatch '" + v + "' / '" + p.Value + "'");
                     return(false);
                 }
             }
             else
             {
                 Log.Selector(p.Name + " does not exists, but needed value '" + p.Value + "'");
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #14
0
 public bool Match(SAPElement m)
 {
     return(Match(this, m));
 }
Beispiel #15
0
        private void Pipeclient_ServerMessage(NamedPipeConnection <SAPEvent, SAPEvent> connection, SAPEvent message)
        {
            try
            {
                if (replyqueue.ContainsKey(message.messageid))
                {
                    lock (replyqueue)
                    {
                        var e = replyqueue[message.messageid];
                        e.reply = message;
                        if (e.reset != null)
                        {
                            e.reset.Set();
                        }
                        replyqueue.Remove(message.messageid);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
            try
            {
                if (message.action == "recorderevent")
                {
                    var data = message.Get <SAPRecordingEvent>();
                    if (data != null)
                    {
                        Log.Debug(message.action + " " + data.ActionName + " " + data.Id);
                    }
                    else
                    {
                        Log.Debug(message.action);
                    }

                    var r = new RecordEvent();
                    r.SupportInput  = false;
                    r.SupportSelect = false;
                    r.ClickHandled  = true;
                    if (data.Action == "InvokeMethod")
                    {
                        var a = new InvokeMethod();
                        a.Path = data.Id; a.ActionName = data.ActionName; a.SystemName = data.SystemName;
                        if (data.Parameters != null)
                        {
                            a.Parameters = data.Parameters;
                            for (var i = 0; i < data.Parameters.Length; i++)
                            {
                                var name = "param" + i.ToString();
                            }
                        }
                        r.a = new GetElementResult(a);
                    }
                    if (data.Action == "SetProperty")
                    {
                        var a = new SetProperty();
                        a.Path = data.Id; a.ActionName = data.ActionName; a.SystemName = data.SystemName;
                        if (data.Parameters != null)
                        {
                            a.Parameters = data.Parameters;
                            for (var i = 0; i < data.Parameters.Length; i++)
                            {
                                var name = "param" + i.ToString();
                            }
                        }
                        r.a = new GetElementResult(a);
                    }
                    if (r != null)
                    {
                        Plugin.Instance.RaiseUserAction(r);
                    }
                }
                else if (message.action == "mousedown")
                {
                    LastEventElement = message.Get <SAPEventElement>();
                    LastElement      = new SAPElement(null, LastEventElement);
                    Log.Output("SAP " + message.action + " " + LastElement.id);
                }
                else if (message.action == "mousemove")
                {
                    LastEventElement = message.Get <SAPEventElement>();
                    LastElement      = new SAPElement(null, LastEventElement);
                    Log.Output("SAP " + message.action + " " + LastElement.id);
                }
                else
                {
                    Log.Output("SAP " + message.action);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Beispiel #16
0
        public void EnumNeededProperties(SAPElement element, SAPElement parent)
        {
            string name = null;

            if (!string.IsNullOrEmpty(element.Name))
            {
                name = element.Name;
            }
            if (!string.IsNullOrEmpty(element.Role))
            {
                name = element.Role;
            }
            if (!string.IsNullOrEmpty(element.Tip))
            {
                name = element.Tip;
            }
            if (!string.IsNullOrEmpty(element.id))
            {
                name = element.id;
            }
            var props = GetProperties();
            //int i = props.Length -1;
            int i            = 1;
            int matchcounter = 0;

            foreach (var p in Properties)
            {
                p.Enabled = false;
            }
            do
            {
                Log.Selector("#*******************************#");
                Log.Selector("# " + i);
                var selectedProps = props.Take(i).ToArray();
                foreach (var p in Properties)
                {
                    p.Enabled = selectedProps.Contains(p.Name);
                }
                SAPElement[] children = { };
                if (element.Parent != null)
                {
                    children = element.Parent.Children;
                }
                matchcounter = 0;
                foreach (SAPElement elementNode in children)
                {
                    Log.Selector("Match using " + i + " properties.");
                    if (Match(elementNode))
                    {
                        matchcounter++;
                    }
                    if (matchcounter > 1)
                    {
                        break;
                    }
                }
                if (matchcounter != 1)
                {
                    ++i;
                }
                //if (matchcounter > 1)
                //{
                //    Log.Selector("EnumNeededProperties match with " + i + " gave more than 1 result");
                //    ++i;
                //    if (i >= props.Count()) break;
                //}
            } while (matchcounter != 1 && i < props.Count());

            Log.Selector("EnumNeededProperties match with " + i + " gave " + matchcounter + " result");
            Properties.ForEach((e) => e.Enabled = false);
            foreach (var p in props.Take(i).ToArray())
            {
                Properties.Where(x => x.Name == p).First().Enabled = true;
            }
        }
Beispiel #17
0
        private void Pipeclient_ServerMessage(NamedPipeConnection <SAPEvent, SAPEvent> connection, SAPEvent message)
        {
            try
            {
                if (replyqueue.ContainsKey(message.messageid))
                {
                    if (System.Threading.Monitor.TryEnter(replyqueue, 1000))
                    {
                        try
                        {
                            var e = replyqueue[message.messageid];
                            e.reply = message;
                            if (e.reset != null)
                            {
                                e.reset.Set();
                            }
                            replyqueue.Remove(message.messageid);
                            return;
                        }
                        finally
                        {
                            System.Threading.Monitor.Exit(replyqueue);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
            try
            {
                if (message.action == "recorderevent")
                {
                    var data = message.Get <SAPRecordingEvent>();
                    if (data != null)
                    {
                        Log.Debug(message.action + " " + data.ActionName + " " + data.Id);
                    }
                    else
                    {
                        Log.Debug(message.action);
                    }

                    var r = new RecordEvent();
                    r.SupportInput  = false;
                    r.SupportSelect = false;
                    r.ClickHandled  = true;
                    if (data.Action == "InvokeMethod")
                    {
                        var skipMethods = PluginConfig.recording_skip_methods.ToLower().Split(',');
                        if (skipMethods.Contains(data.ActionName.ToLower()))
                        {
                            return;
                        }
                        var a = new InvokeMethod();
                        a.Path = data.Id; a.ActionName = data.ActionName; a.SystemName = data.SystemName;
                        if (data.Parameters != null)
                        {
                            a.Parameters = data.Parameters;
                            for (var i = 0; i < data.Parameters.Length; i++)
                            {
                                var name = "param" + i.ToString();
                            }
                        }
                        a.loadImageAsync(data.Id, data.SystemName, data.ActionName, data.StatusBarText);
                        r.a = new GetElementResult(a);
                    }
                    if (data.Action == "SetProperty")
                    {
                        var skipMethods = PluginConfig.recording_skip_properties.ToLower().Split(',');
                        if (skipMethods.Contains(data.ActionName.ToLower()))
                        {
                            return;
                        }
                        var a = new SetProperty();
                        a.Path = data.Id; a.ActionName = data.ActionName; a.SystemName = data.SystemName;
                        if (data.Parameters != null)
                        {
                            a.Parameters = data.Parameters;
                            for (var i = 0; i < data.Parameters.Length; i++)
                            {
                                var name = "param" + i.ToString();
                            }
                        }
                        a.loadImageAsync(data.Id, data.SystemName, data.StatusBarText);
                        r.a = new GetElementResult(a);
                    }
                    if (r != null)
                    {
                        Plugin.Instance.RaiseUserAction(r);
                    }
                }
                else if (message.action == "mousedown")
                {
                    LastEventElement = message.Get <SAPEventElement>();
                    LastElement      = new SAPElement(null, LastEventElement);
                    // Log.Output("SAP " + message.action + " " + LastElement.id);
                }
                else if (message.action == "mousemove")
                {
                    LastEventElement = message.Get <SAPEventElement>();
                    LastElement      = new SAPElement(null, LastEventElement);
                    // Log.Output("SAP " + message.action + " " + LastElement.id + " " + LastElement.Path);
                }
                else
                {
                    // Log.Output("SAP " + message.action);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Beispiel #18
0
        protected override void StartLoop(NativeActivityContext context)
        {
            var SelectorString = Selector.Get(context);

            SelectorString = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(SelectorString, context.DataContext);
            var sel             = new SAPSelector(SelectorString);
            var timeout         = Timeout.Get(context);
            var maxresults      = MaxResults.Get(context);
            var minresults      = MinResults.Get(context);
            var flatternguitree = FlatternGuiTree.Get(context);
            //var from = From.Get(context);
            SAPElement from = null;

            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 SAP.GetElements is above 5 minutes, was this the intention ? calculated value " + timeout.ToString());
                }
            }
            SAPElement[] elements = { };
            var          sw       = new Stopwatch();

            sw.Start();
            do
            {
                var selector = new SAPSelector(SelectorString);
                elements = SAPSelector.GetElementsWithuiSelector(selector, from, 0, maxresults, flatternguitree);
            } while (elements.Count() == 0 && sw.Elapsed < timeout);
            Log.Debug(string.Format("OpenRPA.SAP::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 <SAPElement> _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);
            }
        }
Beispiel #19
0
        public SAPSelector(SAPElement element, SAPSelector anchor, bool doEnum)
        {
            var sw = new System.Diagnostics.Stopwatch();

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

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

            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;
                    root    = 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 (((SAPSelectorItem)anchorlist[i]).Match(pathToRoot[0]))
                    if (SAPSelectorItem.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();
            SAPSelectorItem item;

            if (anchor == null)
            {
                item            = new SAPSelectorItem(root, true);
                item.Enabled    = true;
                item.canDisable = false;
                Items.Add(item);
            }
            item         = new SAPSelectorItem(element, false);
            item.Enabled = true; item.canDisable = false;
            Items.Add(item);
            //for (var i = 0; i < pathToRoot.Count(); i++)
            //{
            //    var o = pathToRoot[i];
            //    item = new SAPSelectorItem(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("SAPselector::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));
        }