public void Focus() { // run all in same thread for speed bool focused = (bool)STAHelper.Invoke( delegate() { // make sure parent window is focused if (ControlType != ControlType.Window) { Element window = Parent; while (window != null && window.ControlType != ControlType.Window) { window = window.Parent; } if (window != null) { window.element.SetFocus(); } } element.SetFocus(); return((bool)AutomationElement.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty)); } ); if (!focused) { // we can't actually fail here because some elements respond to SetFocus // but don't accept keyboard focus. // on the other hand sometimes the focus is async and not done yet. // so if we're not sure we're done, sleep a little and hope it finishes System.Threading.Thread.Sleep(100); } }
public PaneScrollAxis(AutomationElement auto) { STAHelper.Invoke( delegate() { hwnd = (int)auto.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty); } ); }
public void Toggle() { STAHelper.Invoke( delegate() { TogglePattern.Toggle(); } ); }
public void Close() { STAHelper.Invoke( delegate() { WindowPattern.Close(); } ); }
// threadsafe private object this[AutomationProperty property] { get { return(STAHelper.Invoke( delegate() { return element.GetCurrentPropertyValue(property); } )); } }
public void Click(int button) { STAHelper.Invoke( delegate() { Point clickable = element.GetClickablePoint(); // absolute coords Desktop.GetInstance(processId).Click(button, clickable.X, clickable.Y); } ); }
private static List <PersistedObject <Element> > Wrap(List <AutomationElement> searchResults, Session session) { List <PersistedObject <Element> > result = new List <PersistedObject <Element> >(); STAHelper.Invoke( delegate() { foreach (AutomationElement element in searchResults) { result.Add(PersistedObject.Get(Element.Create(element, session.Process.Id), session)); } } ); return(result); }
protected override async Task OnExecute() { // Read the secret var secret = await ReadSecret(Key); // Check for null if (secret == null) { await Console.WriteErrorLine(Strings.Secrets_GetCommand_SecretDoesNotExist, Key); } else { if (secret.Type == SecretType.Certificate) { X509Certificate2 cert = ReadCertificate(secret); if (CopyToClipboard) { await STAHelper.InSTAThread(() => Clipboard.SetText(cert.Thumbprint)); await Console.WriteInfoLine(Strings.Secrets_GetCommand_ThumbprintCopied, Key); } else { await Console.WriteInfoLine( Strings.Secrets_GetCommand_CertificateMetadata, cert.Thumbprint, cert.Subject, cert.NotAfter); } } else { if (CopyToClipboard) { await STAHelper.InSTAThread(() => Clipboard.SetText(secret.Value)); await Console.WriteInfoLine(Strings.Secrets_GetCommand_SecretCopied, Key); } else { await Console.WriteInfoLine(Strings.Secrets_GetCommand_SecretValue, Key); await Console.WriteDataLine(secret.Value); } } } }
public void ExpandCollapse() { STAHelper.Invoke( delegate() { ExpandCollapsePattern expandCollapse = ExpandCollapsePattern; if (expandCollapse.Current.ExpandCollapseState == ExpandCollapseState.Collapsed) { expandCollapse.Expand(); } else { expandCollapse.Collapse(); } } ); }
public static object GetStructure(ElementRequest request) { bool verbose = request.Body != null && request.Body.ContainsKey("verbose") && (bool)request.Body["verbose"]; XmlDocument doc = new XmlDocument(); STAHelper.Invoke( delegate() { Add(doc, doc, request.Target, verbose); } ); StringWriter writer = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(writer); xmlWriter.Formatting = Formatting.Indented; xmlWriter.Indentation = 2; doc.WriteTo(xmlWriter); return(writer.ToString()); }
public void AddExtraJSONProperties(IDictionary <string, object> values) { values["name"] = Name; values["id"] = Id; values["controlType"] = ControlTypeName; values["className"] = Class; values["controlPatterns"] = STAHelper.Invoke( delegate() { List <object> controlPatterns = new List <object>(); foreach (AutomationPattern pattern in element.GetSupportedPatterns()) { string name = NameMappings.GetName(pattern); if (name != null) { controlPatterns.Add(name); } } return(controlPatterns); } ); }
public static List <AutomationElement> FindAll(AutomationElement root, TreeScope scope, Condition condition, int processId) { return((List <AutomationElement>) STAHelper.Invoke( delegate() { int elementProcessId = (int)root.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty); if (elementProcessId != processId) { // This happens when the element represents the desktop. // We could just filter using the ProcessIdProperty but this searches all nodes and *then* filters, // which is incredibly slow if we're searching a lot of nodes (i.e. TreeScope is Descendant). // Instead we find all direct children with the right process id and then search them inclusively. // Helpfully, there's a Subtree TreeScope which does what we want Condition processCondition = new PropertyCondition2(AutomationElement.ProcessIdProperty, processId); if (scope == TreeScope.Descendants) { List <AutomationElement> roots = AutomationExtensions.FindAllRaw(root, TreeScope.Children, processCondition); List <AutomationElement> mergedResults = new List <AutomationElement>(); foreach (AutomationElement currentRoot in roots) { mergedResults.AddRange(FindAll(currentRoot, TreeScope.Subtree, condition, processId)); } return mergedResults; } else { condition = (condition == null) ? processCondition : new AndCondition(condition, processCondition); } } if (condition == null) { condition = Condition.TrueCondition; } return AutomationExtensions.FindAllRaw(root, scope, condition); } )); }
// threadsafe // we may return more than count results - we include complete levels // e.g searching for 'B' from 'A' in // A // / \ // B C // / \ // B B // will yield 1 results with a count of 1 // will yield 3 results with a count of 2 internal static List <AutomationElement> FindAll(AutomationElement root, TreeScope scope, int count, Condition condition, int processId) { if (count <= 0) { return(FindAll(root, scope, condition, processId)); } // Breadth first search, so we find the result closest to the root List <AutomationElement> results = new List <AutomationElement>(); LinkedList <AutomationElement> queue = new LinkedList <AutomationElement>(); queue.AddLast(root); int levelSize = 1; while (queue.Count > 0) { AutomationElement current = queue.First.Value; queue.RemoveFirst(); levelSize--; bool includeElement = false; bool includeChildren = false; bool includeGrandchildren = false; switch (scope) { case TreeScope.Ancestors: case TreeScope.Parent: throw new NotImplementedException(); case TreeScope.Element: includeElement = true; break; case TreeScope.Children: includeChildren = true; break; case TreeScope.Subtree: includeElement = true; includeChildren = true; includeGrandchildren = true; break; case TreeScope.Descendants: includeChildren = true; includeGrandchildren = true; break; } STAHelper.Invoke( delegate() { if (includeElement) { FindAndAdd(current, TreeScope.Element, condition, results, processId); } if (includeChildren && results.Count < count) { FindAndAdd(current, TreeScope.Children, condition, results, processId); } if (includeGrandchildren && results.Count < count) { FindAndAdd(current, TreeScope.Children, Condition.TrueCondition, queue, processId); } } ); if (levelSize == 0) { if (results.Count >= count) { break; } if (scope == TreeScope.Subtree) { scope = TreeScope.Descendants; } levelSize = queue.Count; } } return(results); }
public void Invoke() { STAHelper.Invoke(delegate() { InvokePattern.Invoke(); }); }
public void Deselect() { STAHelper.Invoke(delegate() { SelectionItemPattern.RemoveFromSelection(); }); }
public ScrollAxis GetScrollAxis(OrientationType orientation) { return((ScrollAxis)STAHelper.Invoke( delegate() { if (!scrollAxes.ContainsKey(orientation)) { object pattern; if (AutomationElement.TryGetCurrentPattern(ScrollPattern.Pattern, out pattern)) { scrollAxes[orientation] = new ScrollPatternAxis((ScrollPattern)pattern, orientation); } else if (ControlType == ControlType.ScrollBar) { if (orientation == (OrientationType)this[AutomationElement.OrientationProperty]) { scrollAxes[orientation] = new ScrollBarAxis(AutomationElement); } else { throw new ArgumentException("Cannot get " + orientation + " scroll-axis for a scrollbar that is not " + orientation); } } else if (ControlType == ControlType.Pane && Class == "ScrollBar") { scrollAxes[orientation] = new PaneScrollAxis(AutomationElement); } else { Condition scrollBarCondition = new AndCondition( new PropertyCondition2(AutomationElement.OrientationProperty, orientation), new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.ScrollBar) ); Condition scrollPaneCondition = new AndCondition( new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.Pane), new PropertyCondition2(AutomationElement.ClassNameProperty, "ScrollBar") ); Condition scrollPatternCondition = new PropertyCondition2(AutomationElement.IsScrollPatternAvailableProperty, true); Condition condition = new OrCondition(scrollBarCondition, scrollPaneCondition, scrollPatternCondition); List <AutomationElement> matches = Twin.View.Search.FindAll(AutomationElement, TreeScope.Descendants, 1, condition, (int)AutomationElement.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty)); for (int i = 0; i < matches.Count; i++) { if (matches[i].GetCurrentPropertyValue(AutomationElement.ControlTypeProperty) != ControlType.Pane) { continue; } if ((bool)matches[i].GetCurrentPropertyValue(AutomationElement.IsScrollPatternAvailableProperty)) { continue; } Rect bounds = (Rect)matches[i].GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty); if (orientation == OrientationType.Horizontal && bounds.Height > bounds.Width) { matches.RemoveAt(i--); } if (orientation == OrientationType.Vertical && bounds.Width > bounds.Height) { matches.RemoveAt(i--); } } if (matches.Count == 0) { return null; } if (matches.Count > 1) { throw new ArgumentException("Scrollable Axis for element ambiguous"); } return Element.Create(matches[0], processId).GetScrollAxis(orientation); } } return scrollAxes[orientation]; } )); }