internal static IUIAutomationElement GetCurrentParent(this IUIAutomationElement element) { var automation = new CUIAutomationClass(); var walker = automation.CreateTreeWalker(automation.RawViewCondition); return(walker.GetParentElement(element)); }
/// <summary> /// /// </summary> /// <param name="RelativeType"></param> /// <param name="Condition"></param> /// <returns></returns> public Element GetNextRelative(ElementRelative RelativeType) { IUIAutomationElement relative = null; IUIAutomationTreeWalker walker = new CUIAutomationClass().RawViewWalker; switch (RelativeType) { case ElementRelative.Parent: for (int i = 0; i < 10; i++) { try { relative = walker.GetParentElement(this.IUIElement); break; } catch (COMException e) { } } break; case ElementRelative.FirstChild: for (int i = 0; i < 10; i++) { try { relative = walker.GetFirstChildElement(this.IUIElement); break; } catch (COMException e) { } } break; case ElementRelative.NextSibling: for (int i = 0; i < 10; i++) { try { relative = walker.GetNextSiblingElement(this.IUIElement); break; } catch (COMException e) { } } break; default: throw new Exception("Given enum was not present in the switch!!!"); } if (relative == null) { return(null); } else { return(new Element(relative)); } }
public BaseWindow(Application AppUnderTest, int TimeOut = WINDOWTIMEOUT) { Window = null; AUTOCLASS = null; ROOT = null; this.AppUnderTest = null; Thread.Sleep(10); GC.Collect(); Thread.Sleep(10); this.AppUnderTest = AppUnderTest; AUTOCLASS = new CUIAutomationClass(); ROOT = new Element(new CUIAutomationClass().GetRootElement()); }
/// <summary> /// Gets all the direct children of an element. /// </summary> /// <param name="element"> The element to get the children of. </param> /// <returns> The list of children for the element. </returns> private static IEnumerable <IUIAutomationElement> GetChildren(Element element) { var automation = new CUIAutomationClass(); var walker = automation.CreateTreeWalker(automation.RawViewCondition); var child = walker.GetFirstChildElement(element.NativeElement); while (child != null) { yield return(child); child = walker.GetNextSiblingElement(child); } }
/// <summary> /// Gets the element that is currently focused. /// </summary> /// <returns> The element if found or null if not found. </returns> public static Element FromFocusElement() { try { var automation = new CUIAutomationClass(); var element = automation.GetFocusedElement(); return(element == null ? null : new Element(element, null, null)); } catch (Exception) { return(null); } }
/// <summary> /// Get all elements from the current element that match the specified control type. /// </summary> /// <param name="Scope"></param> /// <param name="ControlType"></param> /// <returns></returns> public ControlElement[] GetControlsByControlType(TreeScope Scope, LocalizedControlType ControlType) { IUIAutomationElementArray elementsToFind = null; IUIAutomationCondition cond = new CUIAutomationClass().CreatePropertyCondition(By.ControlType.GetId(), ControlType.GetControlTypeValue()); elementsToFind = this.IUIElement.FindAll(Scope, cond); int size = elementsToFind.Length; ControlElement[] arr = new ControlElement[size]; for (int i = 0; i < size; i++) { arr[i] = new ControlElement(elementsToFind.GetElement(i)); } return(arr); }
/// <summary> /// Gets the element that is currently at the point. /// </summary> /// <param name="point"> The point to try and detect at element at. </param> /// <returns> The element if found or null if not found. </returns> public static Element FromPoint(Point point) { try { var automation = new CUIAutomationClass(); var element = automation.ElementFromPoint(new tagPOINT { x = point.X, y = point.Y }); return(element == null ? null : new Element(element, null, null)); } catch (Exception) { return(null); } }
/// <summary> /// Gets all windows for the process. /// </summary> /// <param name="process"> The process to get windows for. </param> /// <param name="application"> The application the elements are for. </param> /// <returns> The array of windows. </returns> public static IEnumerable <Window> GetWindows(this Process process, Application application) { // There is a issue in Windows 10 and Cortana (or modern apps) where there is a 60 second delay when walking the root element. // When you hit the last sibling it delays. For now we are simply going to return the main window and we'll roll this code // back once the Windows 10 issue has been resolved. process.Refresh(); var response = new List <Window>(); var automation = new CUIAutomationClass(); foreach (var handle in EnumerateProcessWindowHandles(process.Id)) { var automationElement = automation.ElementFromHandle(handle); var element = Element.Create(automationElement, application, null) as Window; if (element != null) { response.Add(element); } } return(response); }
/// <summary> /// Gets all windows for the process. /// </summary> /// <param name="process"> The process to get windows for. </param> /// <param name="application"> The application the elements are for. </param> /// <returns> The array of windows. </returns> public static IEnumerable<Window> GetWindows(this Process process, Application application) { // There is a issue in Windows 10 and Cortana (or modern apps) where there is a 60 second delay when walking the root element. // When you hit the last sibling it delays. For now we are simply going to return the main window and we'll roll this code // back once the Windows 10 issue has been resolved. process.Refresh(); var response = new List<Window>(); var automation = new CUIAutomationClass(); foreach (var handle in EnumerateProcessWindowHandles(process.Id)) { var automationElement = automation.ElementFromHandle(handle); var element = Element.Create(automationElement, application, null) as Window; if (element != null) { response.Add(element); } } return response; }
public static IUIAutomationElement xtGetRelative(this IUIAutomationElement element, RelativeType RelativeType) { IUIAutomationElement relative = null; IUIAutomationTreeWalker walker = new CUIAutomationClass().RawViewWalker; switch (RelativeType) { case RelativeType.Parent: try { relative = walker.GetParentElement(element); } catch (COMException e) { } break; case RelativeType.FirstChild: try { relative = walker.GetFirstChildElement(element); } catch (COMException e) { } break; default: throw new Exception("Given enum was not present in the switch!!!"); } return(relative as IUIAutomationElement); }
static void Main(string[] args) { try { //Launch settings var appActiveManager = new ApplicationActivationManager(); appActiveManager.ActivateApplication( "windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel", "page=SettingsPageAppsDefaults", ActivateOptions.None, //TODO: Find a way to hide the splash screen. Or maybe just hide the window until the splash screen is gone out var pid ); var uiAutomation = new CUIAutomationClass(); var window = FindAndWait(uiAutomation.GetRootElement(), TreeScope.TreeScope_Children, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Settings"), TimeSpan.FromSeconds(3)); try { //Can't hide the window since that prevents UI Automation from seeing it ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).SetWindowVisualState(WindowVisualState.WindowVisualState_Minimized); var defaultWebBrowserDropDown = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "SystemSettings_DefaultApps_Browser_Button"), TimeSpan.FromSeconds(1)); ((IUIAutomationInvokePattern)defaultWebBrowserDropDown.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke(); var chooseAnApplication = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "DefaultAppsFlyoutPresenter"), TimeSpan.FromSeconds(1)); var googleChromeOption = FindAndWait( chooseAnApplication, TreeScope.TreeScope_Descendants, uiAutomation.CreateAndCondition( uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Google Chrome"), uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId) ), TimeSpan.FromSeconds(1) ); ((IUIAutomationInvokePattern)googleChromeOption.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke(); //If switching from Edge for the first time, it shows a confirmation prompt try { var switchAnyway = FindAndWait( window, TreeScope.TreeScope_Descendants, uiAutomation.CreateAndCondition( uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Switch anyway"), uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_HyperlinkControlTypeId) ), TimeSpan.FromSeconds(1) ); ((IUIAutomationInvokePattern)switchAnyway.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke(); } catch (TimeoutException) { } //All good } finally { ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).Close(); } } catch //Ensures all nested finally blocks execute { throw; } }
public void Test1() { ProcessStartInfo x = new ProcessStartInfo(); x.FileName = @"C:\Windows\System32\calc.exe"; var process = Process.Start(x); process.WaitForInputIdle(); var ROOT = new CUIAutomationClass().GetRootElement(); var prc = ROOT.FindAll(TreeScope.TreeScope_Children, new CUIAutomationClass().CreateTrueCondition()); int size = prc.Length; for (int i = 0; i < prc.Length; i++) { string l = prc.GetElement(i).CurrentName; int r = prc.GetElement(i).CurrentProcessId; } ////This is how you attach to an already opened process //foreach (Process procs in Process.GetProcesses()) //{ // if (procs.ProcessName.Equals("Calculator")) // { // int ProcessID = procs.Id; // } //} IUIAutomationCondition window_cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Calculator"); IUIAutomationElement window = null; int attempts = 1; while (window == null && attempts < 10) { window = ROOT.FindFirst(TreeScope.TreeScope_Children, window_cond); attempts++; } IUIAutomationCondition el_cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "num9Button"); IUIAutomationElement el = window.FindFirst(TreeScope.TreeScope_Descendants, el_cond); var pat = (IUIAutomationInvokePattern)el.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); pat.Invoke(); IUIAutomation m = new CUIAutomation(); IUIAutomationCondition hist_Cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "HistoryButton"); IUIAutomationElement hist_El = window.FindFirst(TreeScope.TreeScope_Descendants, hist_Cond); var y = hist_El.CurrentHelpText; int p = 0; }
/// <summary> /// Gets the element that is currently at the point. /// </summary> /// <param name="point"> The point to try and detect at element at. </param> /// <returns> The element if found or null if not found. </returns> public static DesktopElement FromPoint(Point point) { try { var automation = new CUIAutomationClass(); var element = automation.ElementFromPoint(new tagPOINT { x = point.X, y = point.Y }); return element == null ? null : new DesktopElement(element, null, null); } catch (Exception) { return null; } }
/// <summary> /// Gets the element that is currently focused. /// </summary> /// <returns> The element if found or null if not found. </returns> public static DesktopElement FromFocus() { try { var automation = new CUIAutomationClass(); var element = automation.GetFocusedElement(); return element == null ? null : new DesktopElement(element, null, null); } catch (Exception) { return null; } }
internal static IUIAutomationElement GetCurrentParent(this IUIAutomationElement element) { var automation = new CUIAutomationClass(); var walker = automation.CreateTreeWalker(automation.RawViewCondition); return walker.GetParentElement(element); }
/// <summary> /// <para>This method will continually get the same subsequent relatives until a match is found or no more elements exist (null element returned).</para> /// <para>Useful for stepping down a large tree until the expected element is found.</para> /// <para>Example: Recursively finds the first child of the next element until a condition is hit.</para> /// </summary> /// <param name="RelativeType"></param> /// <param name="Condition"></param> /// <returns></returns> public Element GetRelativeElementUntilFound(ElementRelative RelativeType, SearchCondition Condition, int NumberOfRelatives = 1000) { Console.WriteLine($"--- Start of GetRelativeElementUntilFound Method ---"); //Console.WriteLine($"--- Iterating through Relative Type : {RelativeType.ToString()} ---"); Console.WriteLine($"--- Looking for Condition - By: [{Condition.By}] - ByValue: [{Condition.ByValue}] - LocalizedControlType: [{Condition.ControlType}] ---"); IUIAutomationElement relative = this.IUIElement; IUIAutomationTreeWalker walker = new CUIAutomationClass().RawViewWalker; switch (RelativeType) { case ElementRelative.Parent: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetParentElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.Name}] - AutomationId: [{relative.AutomationId}] - LocalizedControlType: [{relative.LocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; case ElementRelative.FirstChild: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetFirstChildElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.CurrentName}] - AutomationId: [{relative.CurrentAutomationId}] - LocalizedControlType: [{relative.CurrentLocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; case ElementRelative.NextSibling: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetNextSiblingElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.Name}] - AutomationId: [{relative.AutomationId}] - LocalizedControlType: [{relative.LocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; default: throw new Exception("Given enum was not present in the switch!!!"); } if (relative == null) { //Console.WriteLine($"--- End of GetRelativeElementUntilFound Method - Returning Null ---"); return(null); } else { //Console.WriteLine($"--- End of GetRelativeElementUntilFound Method - Returning Element ---"); return(new Element(relative)); } }