public void GoToRoot() { if (SelectedItemInTree?.AutomationElement == null) { return; } SelectedItem = new ElementBO(GetRootFromElement(SelectedItemInTree) as SHAutomationElement); }
public async void Search() { SearchResults = new ISHAutomationElement[0]; CurrentSearchIndex = 0; IsSearching = true; await Task.Run(() => { try { string xpath = string.Empty; if (SelectedItemInTree != null) { if (SelectedSearchTerm == SHInspectConstants.ControlType) { xpath = $"//{SearchText}"; } else if (SelectedSearchTerm == "XPath") { xpath = SearchText; } else { xpath = $"//*[@{SelectedSearchTerm}='{SearchText}']"; } } //else //{ // if (SelectedSearchTerm == SHInspectConstants.ControlType) // { // xpath = $"//{SearchText}"; // } // else if (SelectedSearchTerm == "XPath") // { // xpath = SearchText; // } // else // { // xpath = $"//*[@{SelectedSearchTerm}='{SearchText}']"; // } //} SearchResults = SelectedItemInTree.AutomationElement.FindAllByXPath(xpath); if (SearchResults.Any()) { SelectedItem = new ElementBO(SearchResults[0] as SHAutomationElement); } } catch (XPathException) { IsSearching = false; } }); IsSearching = false; }
public void ChangeResult(bool moveForward) { CurrentSearchIndex += moveForward ? 1 : -1; if (CurrentSearchIndex > SearchResults.Count() - 1) { CurrentSearchIndex = 0; } else if (CurrentSearchIndex < 0) { CurrentSearchIndex = SearchResults.Count() - 1; } SelectedItem = new ElementBO(SearchResults[CurrentSearchIndex] as SHAutomationElement); }
public ISHAutomationElement GetRootFromElement(ElementBO element) { try { ISHAutomationElement win = Automation.GetParent(element.AutomationElement, x => x.ByControlType(ControlType.Window).And(x.ByClassName(SHInspectConstants.Popup).Not())); if (win?.Parent != null && !win.Parent.Equals(DesktopItem)) { win = win.Parent; } return(win); } catch (COMException) { return(null); } }
public async Task Inspect(bool acceptNewWindows) { if (!Inspecting) { Inspecting = true; await Task.Run(() => { try { System.Windows.Application.Current.Dispatcher.Invoke(delegate { foreach (System.Windows.Window w in System.Windows.Application.Current.Windows) { if (w is AddWindowPopup) { w.Close(); } } }); var screenPos = Mouse.Position; var hov = Automation.FromPoint(screenPos); ElementBO hoveredElement = hov != null ? new ElementBO(hov) : null; var win = hoveredElement != null ? GetRootFromElement(hoveredElement) : null; if (win == null) { Inspecting = false; return; } if (SavedSettingsWindows.Any(x => x.Identifier == win.AutomationId || x.Identifier == win.Name)) { if (!Elements.Any(x => x.AutomationElement.Equals(win) && x.AutomationElement.ProcessId == win.ProcessId)) { System.Windows.Application.Current.Dispatcher.Invoke(delegate { GetDesktop(); }); } else { DrawHighlight(hov); } } else { Inspecting = false; if (acceptNewWindows) { CreateAddWindowPopUp(win); } return; } if (hoveredElement != null) { var selected = SelectedItem; if (selected == null || (selected != null && !selected.Equals(hoveredElement))) { SelectedItem = hoveredElement; ElementToSelectChanged(hoveredElement.AutomationElement); Inspecting = false; } } ; } catch (Exception ex) when(ex is COMException || ex is UnauthorizedAccessException || ex is Win32Exception || ex is FileNotFoundException || ex is TimeoutException) { System.Diagnostics.Debug.WriteLine(ex.GetType().Name); Inspecting = false; System.Windows.Application.Current.Dispatcher.Invoke(delegate { GetDesktop(); }); } }); } }
public void GoToParent() { SelectedItem = new ElementBO(SelectedItemInTree.AutomationElement.Parent as SHAutomationElement); }
public ElementBO FindElement(ElementBO parent, SHAutomationElement element) { return(parent != null && parent.Children != null && parent.Children.Any() ? parent.Children.FirstOrDefault(child => child.AutomationElement.Equals(element)) : null); }
public void ElementToSelectChanged(ISHAutomationElement obj) { var tempObject = obj; // Build a stack from the root to the hovered item if (tempObject.Equals(SelectedItemInTree?.AutomationElement)) { return; } var pathToRoot = new Stack <SHAutomationElement>(); while (tempObject != null) { // Break on circular relationship (should not happen?) foreach (var root in Elements) { if (pathToRoot.Contains(tempObject) || tempObject.Equals(root)) { break; } } pathToRoot.Push(tempObject as SHAutomationElement); var treeItem = _treeWalker?.GetParent(tempObject as SHAutomationElement); tempObject = treeItem != null ? treeItem : null; } if (Elements.Any() && !Elements.Any(x => !x.IsExpanded)) { foreach (var item in Elements.Where(x => !x.IsExpanded)) { item.IsExpanded = true; } } string id = null; string helpText = null; if (pathToRoot.Any() && pathToRoot.Count > 1) { id = pathToRoot.ToArray()[1].Properties.AutomationId.IsSupported && !string.IsNullOrEmpty(pathToRoot.ToArray()[1].AutomationId) ? pathToRoot.ToArray()[1].AutomationId : pathToRoot.ToArray()[1].Properties.Name.IsSupported ? pathToRoot.ToArray()[1].Name : null; helpText = pathToRoot.ToArray()[1].Properties.HelpText.IsSupported && !string.IsNullOrEmpty(pathToRoot.ToArray()[1].HelpText) ? pathToRoot.ToArray()[1].HelpText : null; } ElementBO elementVm = null; if (id != null && helpText != null) { elementVm = Elements.FirstOrDefault(x => x.AutomationElement != null && (x.AutomationElement.SupportsAutomationId && x.AutomationId == id && x.AutomationElement.SupportsHelpText && x.HelpText == helpText)); } else { elementVm = id != null?Elements.FirstOrDefault(x => x.AutomationElement != null && (x.AutomationElement.SupportsAutomationId && x.AutomationId == id) || (x.AutomationElement.SupportsName && x.Name == id)) : Elements.First(); } if (elementVm != null) { elementVm.IsExpanded = true; } List <string> list = new List <string>(); if (id != null) { pathToRoot.Pop(); list = pathToRoot.Where(x => x.SupportsName && !string.IsNullOrEmpty(x.Name)).Select(x => x.Name).ToList(); } else { list = pathToRoot.Where(x => x.SupportsName && !string.IsNullOrEmpty(x.Name)).Select(x => x.Name).ToList(); } while (pathToRoot.Count > 0) { var elementOnPath = pathToRoot.Pop(); var nextElementVm = FindElement(elementVm, elementOnPath); if (pathToRoot.Any() && nextElementVm == null) { elementOnPath = pathToRoot.Pop(); nextElementVm = FindElement(elementVm, elementOnPath); } if (nextElementVm == null && elementVm != null) { // Could not find next element, try reloading the parent elementVm.IsExpanded = true; elementVm.LoadChildren(true); // Now search again nextElementVm = FindElement(elementVm, elementOnPath); if (nextElementVm == null) { break; } } elementVm = nextElementVm; if (elementVm != null && !elementVm.IsExpanded) { elementVm.IsExpanded = true; } } if (elementVm != null) { elementVm.IsSelected = true; } }