public void Loaded() { SearchResults = new ISHAutomationElement[0]; SavedSettingsWindows = new ObservableCollection <WindowBO>(); var savedWins = Settings.Default.Windows; var inspectColour = Settings.Default.InspectionColour; SelectedColour = new System.Windows.Media.Color() { A = inspectColour.A, R = inspectColour.R, G = inspectColour.G, B = inspectColour.B }; IsLive = Settings.Default.IsLive; if (savedWins != null) { foreach (string win in savedWins) { SavedSettingsWindows.Add(new WindowBO(win, false)); } } Elements = new ObservableCollection <ElementBO>(); Automation = new UIA3Automation(); Automation.TransactionTimeout = new TimeSpan(0, 0, 0, 5); GetDesktop(); PropertyChangedEventManager.AddHandler(this, SelectedItemChanged, nameof(SelectedItemInTree)); }
public WindowBO(ISHAutomationElement automationElement, bool isCurrent) { AutomationElement = automationElement; IsCurrent = isCurrent; UpdateProperties(); Identifier = !string.IsNullOrEmpty(AutomationId) ? AutomationId : Name; }
public void GetDesktop() { Properties = null; Patterns = null; Image = null; Elements = new ObservableCollection <ElementBO>(); var item = _automation.GetDesktop(); SearchResults = new ISHAutomationElement[0]; CurrentSearchIndex = 0; _treeWalker = _automation.TreeWalkerFactory.GetControlViewWalker(); DesktopItem = item != null ? item : null; var items = DesktopItem.FindAllByXPath("*").Select(x => new ElementBO(new SHAutomationElement(x.FrameworkAutomationElement))); foreach (var win in items) { if (SavedSettingsWindows.Any(x => (!string.IsNullOrEmpty(win.AutomationId?.Trim()) && win.AutomationId.StartsWith(x.Identifier)) || (!string.IsNullOrEmpty(win.Name?.Trim()) && win.Name.StartsWith(x.Identifier)))) { if (!Elements.Any(x => x.AutomationElement.Equals(win.AutomationElement))) { Elements.Add(win); Elements.ToList().ForEach(x => x.IsExpanded = true); } } } if (Elements.Any()) { SelectedItem = Elements.First(); } _dispatcherTimer = new DispatcherTimer(); _dispatcherTimer.Tick += DispatcherTimerTick; _dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1); _dispatcherTimer.Start(); }
/// <summary> /// Method to convert the element to the given type. /// </summary> public static SHAutomationElement AsType(this ISHAutomationElement self, Type type) { if (!type.IsAssignableFrom(typeof(SHAutomationElement))) { throw new ArgumentException("The given type is not an SHAutomationElement", nameof(type)); } return((SHAutomationElement)Activator.CreateInstance(type, self.FrameworkAutomationElement)); }
public ISHAutomationElement Find(Func <ConditionFactory, ConditionBase> conditionFunc, TimeSpan timeout, TimeSpan offscreenTimeout, ISHAutomationElement parent = null) { ISHAutomationElement control = null; bool regenerateXPath = false; var condition = conditionFunc(new ConditionFactory(Automation.PropertyLibrary)); var conditionString = condition.ToString(); _loggingService.Info(string.Format("Find called {0}", conditionString)); bool canUseXpath = !(conditionString.Contains("OR") || conditionString.Contains("NOT")); List <(PropertyCondition Value, bool Ignore)> propertyConditions = new List <(PropertyCondition Value, bool Ignore)>(); if (parent == null) { if (canUseXpath) { propertyConditions = GetPropertyConditions(condition); } control = GetXPathFromPropertyConditions(propertyConditions); if (control == null) { regenerateXPath = true; control = FindFirstDescendant(conditionFunc, timeout: timeout); } } else { control = parent.FindFirstDescendant(conditionFunc, timeout); } if (control == null) { _loggingService.Error(string.Format("Failed to find control by: {0}", conditionFunc(new ConditionFactory(Automation.PropertyLibrary)).ToString())); throw new ElementNotFoundException(string.Format("Failed to find control by: {0}", conditionFunc(new ConditionFactory(Automation.PropertyLibrary)).ToString())); } _loggingService.Info("Find found control"); SHSpinWait.SpinUntil(() => control.SupportsOnscreen, TimeSpan.FromMilliseconds(500)); if (control.SupportsOnscreen) { SHSpinWait.SpinUntil(() => control.IsOnscreen, offscreenTimeout); _loggingService.Info("Find OnScreen: " + control.IsOnscreen); } else { _loggingService.Info("Find OnScreen is not supported"); } SaveXPathFromControl((SHAutomationElement)control, propertyConditions, regenerateXPath); return(control); }
public void DrawHighlight(ISHAutomationElement element) { ElementHighlighter.UseElementHighlighter = true; var drawingcolor = System.Drawing.Color.FromArgb( SelectedColour.A, SelectedColour.R, SelectedColour.G, SelectedColour.B); ElementHighlighter.HighlightColour = drawingcolor; ElementHighlighter.HighlightElement(element as SHAutomationElement); ElementHighlighter.UseElementHighlighter = false; }
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; }
private void CreateAddWindowPopUp(ISHAutomationElement window) { System.Windows.Application.Current.Dispatcher.Invoke(delegate { AddWindowPopup pop = new AddWindowPopup(new WindowBO(window, false)); pop.Left = Mouse.Position.X; pop.Top = Mouse.Position.Y; pop.Show(); pop.BringIntoView(); pop.Topmost = true; }); }
public ISHAutomationElement FindFirstByXPath(string xPath, TimeSpan timeout) { ISHAutomationElement element = null; bool getElement(string xpath) { element = FindFirstByXPath(xpath); return(element != null); } SHSpinWait.SpinUntil(() => getElement(xPath), timeout); return(element); }
private static object GetPropertyValueHandled(ISHAutomationElement automationElement, PropertyId propertyId) { try { object output = null; automationElement.FrameworkAutomationElement.TryGetPropertyValue(propertyId, out output); return(output); } catch { return(null); } }
public static List <PatternBO> GetElementDetailViewModelPatterns(ISHAutomationElement element) { try { List <PatternBO> details = new List <PatternBO>(); var library = element.Automation.PatternLibrary; PropertyInfo[] allPatterns = GetPublicProperties(library.GetType()); List <string> patternNames = allPatterns.Select(x => x.Name.Split("Pattern")[0]).ToList(); var supportedPatterns = element.GetSupportedPatterns().Where(x => x.Id != 0); var automationPatterns = element.Patterns; var automationPatternsProperties = GetPublicProperties(typeof(IFrameworkPatterns)); foreach (var item in patternNames) { if (supportedPatterns.Any(x => x.Name == item)) { var pattern = supportedPatterns.First(x => x.Name == item); object output; element.FrameworkAutomationElement.TryGetNativePattern(pattern, out output); if (output != null) { details.Add(new PatternBO(item, string.Empty, true, true)); var patternObject = automationPatternsProperties.First(x => x.Name.Split("Pattern")[0] == pattern.Name); var patternType = patternObject.GetMethod.Invoke(automationPatterns, null); var patternGet = patternType.GetType().GetMethods().First(x => x.Name == "get_Pattern").Invoke(patternType, null); var patternProperties = GetPublicProperties(patternGet.GetType()); foreach (var prop in patternProperties) { var returntype = prop.GetMethod.ReturnType; if (returntype.Name.Contains("AutomationProperty")) { details.Add(new PatternBO(prop.Name, prop.GetMethod.Invoke(patternGet, null).ToString(), true, false)); } } } } else { details.Add(new PatternBO(item, string.Empty, false, true)); } } return(details.OrderByDescending(x => x.IsSupported).ToList()); } catch (Exception ex) when(ex is OverflowException || ex is InvalidOperationException || ex is ArgumentException) { return(null); } }
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 bool Exists(Func <ConditionFactory, ConditionBase> conditionFunc, TimeSpan timeout, TimeSpan offscreenTimeout, bool checkOnScreen = true, ISHAutomationElement parent = null, bool xpathOnly = false) { bool exists = true; bool regenerateXPath = false; ISHAutomationElement control = null; if (parent == null) { control = GetXPathElementFromCondition(conditionFunc, timeout); if (control == null && !xpathOnly) { regenerateXPath = true; control = FindFirstDescendant(conditionFunc, timeout: timeout); } } else { if (xpathOnly) { throw new InvalidOperationException("Cannot search with xpath using parent"); } control = parent.FindFirstDescendant(conditionFunc, timeout: timeout); } if (control == null) { exists = false; } else if (checkOnScreen) { control.WaitUntilPropertyEquals(PropertyId.Register(AutomationType.UIA3, 30022, "IsOffscreen"), false, offscreenTimeout); exists = control.IsOnscreen; } if (parent == null) { SaveXPathFromControl((SHAutomationElement)control, conditionFunc, regenerateXPath); } return(exists); }
public ISHAutomationElement FindFirstDescendant(TimeSpan timeout, bool waitUntilExists = true) { ISHAutomationElement element = null; bool getElement(bool shouldExist) { if (element == null) { element = FindFirstDescendantBase(); } return(shouldExist ? element?.FrameworkAutomationElement != null : element == null); } getElement(waitUntilExists); if (element == null && waitUntilExists && timeout.TotalMilliseconds > 0) { SHSpinWait.SpinUntil(() => getElement(true), timeout); } else if (element != null && !waitUntilExists && timeout.TotalMilliseconds > 0) { SHSpinWait.SpinUntil(() => getElement(false), timeout); } return(element?.FrameworkAutomationElement != null ? element : null); }
public ISHAutomationElement Find(string automationID, TimeSpan timeout, TimeSpan offscreenTimeout, ISHAutomationElement parent = null) { return(Find(x => x.ByAutomationId(automationID), timeout, offscreenTimeout, parent)); }
public ISHAutomationElement Find(string automationID, TimeSpan timeout, ISHAutomationElement parent = null) { return(Find(x => x.ByAutomationId(automationID), timeout, TimeSpan.FromSeconds(10), parent)); }
/// <summary> /// Converts the element to a <see cref="TreeItem"/>. /// </summary> public static TreeItem AsTreeItem(this ISHAutomationElement self) { return(self == null ? null : new TreeItem(self.FrameworkAutomationElement)); }
/// <summary> /// Generic method to convert the element to the given type. /// </summary> public static T AsType <T>(this ISHAutomationElement self) where T : SHAutomationElement { return((T)Activator.CreateInstance(typeof(T), self.FrameworkAutomationElement)); }
public bool Exists(Func <ConditionFactory, ConditionBase> conditionFunc, bool checkOnScreen = true, ISHAutomationElement parent = null, bool xpathOnly = false) { return(Exists(conditionFunc, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), checkOnScreen, parent, xpathOnly)); }
/// <summary> /// Converts the element to a <see cref="HorizontalScrollBar"/>. /// </summary> public static HorizontalScrollBar AsHorizontalScrollBar(this ISHAutomationElement self) { return(self == null ? null : new HorizontalScrollBar(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="GridHeaderItem"/>. /// </summary> public static GridHeaderItem AsGridHeaderItem(this ISHAutomationElement self) { return(self == null ? null : new GridHeaderItem(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="GridCell"/>. /// </summary> public static GridCell AsGridCell(this ISHAutomationElement self) { return(self == null ? null : new GridCell(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="DataGridView"/>. /// </summary> public static DataGridView AsDataGridView(this ISHAutomationElement self) { return(self == null ? null : new DataGridView(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="ComboBox"/>. /// </summary> public static ComboBox AsComboBox(this ISHAutomationElement self) { return(self == null ? null : new ComboBox(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="Window"/>. /// </summary> public static Window AsWindow(this ISHAutomationElement self, string pathToConfigFile = null) { return(AsWindow(self, null, pathToConfigFile)); }
public ISHAutomationElement Find(Func <ConditionFactory, ConditionBase> conditionFunc, ISHAutomationElement parent = null) { return(Find(conditionFunc, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(10), parent)); }
/// <summary> /// Converts the element to a <see cref="ListBox"/>. /// </summary> public static ListBox AsListBox(this ISHAutomationElement self) { return(self == null ? null : new ListBox(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="VerticalScrollBar"/>. /// </summary> public static VerticalScrollBar AsVerticalScrollBar(this ISHAutomationElement self) { return(self == null ? null : new VerticalScrollBar(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="Label"/>. /// </summary> public static Label AsLabel(this ISHAutomationElement self) { return(self == null ? null : new Label(self.FrameworkAutomationElement)); }
/// <summary> /// Converts the element to a <see cref="Window"/>. /// </summary> public static Window AsWindow(this ISHAutomationElement self, ILoggingService loggingService, string pathToConfigFile = null) { return(self == null ? null : new Window(self.FrameworkAutomationElement, loggingService, pathToConfigFile)); }