public static SAPElement[] GetElementsWithuiSelector(SAPSelector selector, IElement fromElement, int skip, int maxresults, bool FlatternGuiTree) { var result = new List <SAPElement>(); var root = new SAPSelectorItem(selector[0]); var SystemName = root.SystemName; if (SAPhook.Instance.Sessions == null || SAPhook.Instance.Sessions.Length == 0) { SAPhook.Instance.RefreshConnections(); } if (SAPhook.Instance.Sessions != null) { foreach (var session in SAPhook.Instance.Sessions) { if (string.IsNullOrEmpty(SystemName) || (SystemName == session.Info.SystemName)) { result.AddRange(GetElementsWithuiSelector(session, selector, fromElement, skip, maxresults, FlatternGuiTree)); if (result.Count > maxresults) { return(result.ToArray()); } } } } return(result.ToArray()); }
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()); }
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()); }
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)); }
private void Highlight_Click(object sender, RoutedEventArgs e) { ModelItem loadFrom = ModelItem.Parent; string loadFromSelectorString = ""; SAPSelector 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 SAPSelector(loadFromSelectorString); break; } loadFrom = loadFrom.Parent; } HighlightImage = Interfaces.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 SAPSelector(SelectorString); var elements = new List <SAPElement>(); if (anchor != null) { var _base = SAPSelector.GetElementsWithuiSelector(anchor, null, 10); foreach (var _e in _base) { var res = SAPSelector.GetElementsWithuiSelector(selector, _e, maxresults); elements.AddRange(res); } } else { var res = SAPSelector.GetElementsWithuiSelector(selector, null, maxresults); elements.AddRange(res); } if (elements.Count() > maxresults && maxresults > 0) { elements = elements.ToList().Take(maxresults).ToList(); } if (elements.Count() > 0) { HighlightImage = Interfaces.Extensions.GetImageSourceFromResource("check.png"); NotifyPropertyChanged("HighlightImage"); } foreach (var ele in elements) { ele.Highlight(false, System.Drawing.Color.Red, TimeSpan.FromSeconds(1)); } }
public Interfaces.Selector.Selector GetSelector(Selector anchor, Interfaces.Selector.treeelement item) { var SAPitem = item as SAPTreeElement; SAPSelector SAPanchor = anchor as SAPSelector; if (SAPanchor == null && anchor != null) { SAPanchor = new SAPSelector(anchor.ToString()); } return(new SAPSelector(SAPitem.SAPElement, SAPanchor, true)); }
private void Open_Selector(object sender, RoutedEventArgs e) { ModelItem loadFrom = ModelItem.Parent; string loadFromSelectorString = ""; SAPSelector 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 SAPSelector(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 SAPSelector(SelectorString); selectors = new Interfaces.Selector.SelectorWindow("SAP", selector, anchor, maxresults); } else { var selector = new SAPSelector("[{Selector: 'SAP'}]"); selectors = new Interfaces.Selector.SelectorWindow("SAP", 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 <SAPElement>() { Expression = new Microsoft.VisualBasic.Activities.VisualBasicValue <SAPElement>("item") }); } } }
protected override void Execute(NativeActivityContext context) { var SelectorString = Selector.Get(context); SelectorString = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(SelectorString, context.DataContext); var sel = new SAPSelector(SelectorString); var timeout = TimeSpan.FromSeconds(3); var maxresults = MaxResults.Get(context); var minresults = MinResults.Get(context); var from = From.Get(context); if (maxresults < 1) { maxresults = 1; } SAPElement[] elements = { }; var sw = new Stopwatch(); sw.Start(); do { var selector = new SAPSelector(SelectorString); elements = SAPSelector.GetElementsWithuiSelector(selector, from, maxresults); } 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) { context.ScheduleAction(Body, _enum.Current, OnBodyComplete); } else { throw new Interfaces.ElementNotFoundException("Failed locating item"); } }
public static SAPElement[] GetElementsWithuiSelector(SAPSelector selector, IElement fromElement = null, int maxresults = 1) { var result = new List <SAPElement>(); var root = new SAPSelectorItem(selector[0]); var SystemName = root.SystemName; foreach (var session in SAPhook.Instance.Sessions) { if (string.IsNullOrEmpty(SystemName) || (SystemName == session.Info.SystemName)) { result.AddRange(GetElementsWithuiSelector(session, selector, fromElement, maxresults)); if (result.Count > maxresults) { return(result.ToArray()); } } } return(result.ToArray()); }
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)); }
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); } }
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)); }
public override IElement[] GetElements(IElement fromElement = null, int maxresults = 1) { return(SAPSelector.GetElementsWithuiSelector(this, fromElement, maxresults)); }