public ViewTree(IUIAutomationElement element, IUIAutomation automation) { this.root = new TreeNode(element); this.root.parent = null; IUIAutomationElementArray array = element.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); if (0 == array.Length) { this.root.children = null; this.root.isLeaf = true; } else { for (int i = 0; i < array.Length; i++) { IUIAutomationElement e = array.GetElement(i); TreeNode n = new TreeNode(e); this.root.children.Add(n); } } }
// Uses Windows UI Automation API to determine if we should click and Drag in context Mode private void uiAutomationCheck(Point p) { tagPOINT reference = new tagPOINT(); reference.x = p.X; reference.y = p.Y; if (this.status.getCurrentMode() != ProgramMode.clickAndDrag) { try { IUIAutomationElement focus = this.automator.ElementFromPoint(reference); // Useful for debugging API support System.Diagnostics.Debug.Print(focus.CurrentControlType.ToString()); System.Diagnostics.Debug.Print("localized:" + focus.CurrentLocalizedControlType); if ((focus.CurrentControlType == 50037 && this.parameters.contextValues.supportTitleBars) || (focus.CurrentControlType == 50027 && this.parameters.contextValues.supportScrollBars) || (focus.CurrentControlType == 50018 && this.parameters.contextValues.supportTabs)) { this.status.setCurrentMode(ProgramMode.clickAndDrag); } } catch (COMException e) { // No element given, give up } } }
public IUIAutomationElement GetScrollingContainerElement(IUIAutomationElement rootElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "wsFrame"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_DocumentControlTypeId); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "document"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement frameElement = rootElement.FindFirst(TreeScope.TreeScope_Children, conditions); conditionArray = new IUIAutomationCondition[3]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "mainContent"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListControlTypeId); conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement contentElement = frameElement.FindFirst(TreeScope.TreeScope_Children, conditions); conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Scrolling Container"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_GroupControlTypeId); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "group"); conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement scrollingContainerElement = contentElement.FindFirst(TreeScope.TreeScope_Children, conditions); return(scrollingContainerElement); }
///////////////////////////////////////////////////////////////////////////////////////////////// // // RegisterStructureChangeListener() // // Add a UIA event handler to react to structure change events sent from the browser window. // // Runs on the background thread. // ///////////////////////////////////////////////////////////////////////////////////////////////// void RegisterStructureChangeListener() { // This sample assumes we know which window we want to gets events from. // *** Note: We must get a pointer to the element we're interested in, in this thread. Do // not have the UI thread get the element and supply it through to the background thread. IntPtr hwnd = Win32.FindWindow(strBrowserWindowClass, null); if (hwnd != IntPtr.Zero) { _elementBrowser = _automation.ElementFromHandle(hwnd); if (_elementBrowser != null) { // In general it is not the IEFrame element itself which throws the structure change // events which we will receive in the handler, but rather various decendants elements. // It is these descendants which are the "senders" in the event handler later. // AddStructureChangedEventHandler() can take a cache request as other UIA methods do. // So if an event handler wanted to retrieve properties from the event sender without // having to incur the time cost for a cross-proc call, it would use a cache request // here. (For this sample, the event handler doesn't need that.) // Other types of UIA event handler can be set up with the following calls: // AddFocusChangeEventHandler() // AddAutomationEventHandler() // AddPropertyChangeEventHandler() - note that some providers might not raise all // the expected propety change events. _automation.AddStructureChangedEventHandler(_elementBrowser, TreeScope.TreeScope_Descendants, null, this); _fAddedEventHandler = true; } } }
/// <summary> /// Get Property value safely /// </summary> /// <param name="element"></param> /// <param name="id"></param> /// <returns></returns> private static dynamic GetPropertyValue(IUIAutomationElement element, int id) { if (element == null) { throw new ArgumentNullException(nameof(element)); } dynamic value = null; try { value = element.GetCurrentPropertyValue(id); if (id == PropertyType.UIA_LabeledByPropertyId && value != null) { value = GetHeaderOfLabelBy(value); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); value = null; } #pragma warning restore CA1031 // Do not catch general exception types return(value); }
internal static IUIAutomationElement GetCurrentParent(this IUIAutomationElement element) { var automation = new CUIAutomationClass(); var walker = automation.CreateTreeWalker(automation.RawViewCondition); return(walker.GetParentElement(element)); }
public void MyTestInitialize() { // Create the factory and register schemas _nFactory = new CUIAutomationClass(); // Start the app var curDir = Environment.CurrentDirectory; _app = new TargetApp(curDir + "\\WpfAppWithAdvTextControl.exe"); _app.Start(); // Find the main control var appElement = _nFactory.ElementFromHandle(_app.MainWindow); var advTestBoxCondition = _nFactory.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "advTextBox1"); _nAdvancedTextBoxElement = appElement.FindFirst(NTreeScope.TreeScope_Children, advTestBoxCondition); Assert.IsNotNull(_nAdvancedTextBoxElement); var testControlCondition = _nFactory.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "testControl"); _nTestControlElement = appElement.FindFirst(NTreeScope.TreeScope_Children, testControlCondition); Assert.IsNotNull(_nTestControlElement); var window = AutomationElement.RootElement.FindFirst(WTreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "MainWindow")); _wAdvancedTextBoxElement = window.FindFirst(WTreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "advTextBox1")); _wTestControlElement = window.FindFirst(WTreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "testControl")); }
#pragma warning disable CA1725 // Parameter names should match base declaration public void HandleTextEditTextChangedEvent(IUIAutomationElement sender, TextEditChangeType textEditChangeType, string[] eventStrings) #pragma warning restore CA1725 // Parameter names should match base declaration { #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(this.EventId, sender); if (m != null) { m.Properties = new List <KeyValuePair <string, dynamic> > { new KeyValuePair <string, dynamic>("TextEditChangeType", textEditChangeType.ToString()), }; if (eventStrings != null) { for (int i = 0; i < eventStrings.Length; i++) { m.Properties.Add(new KeyValuePair <string, dynamic>(Invariant($"[{i}]"), eventStrings.GetValue(i))); } } this.ListenEventMessage(m); } #pragma warning restore CA2000 }
/// <summary> /// Find all descendents of a given element, optionally searching with the given strategy /// </summary> /// <param name="element">The element whose descendents we need to find</param> /// <param name="searchStrategy">The strategy we should use to evaluate children</param> /// <returns>All elements that pass the searchStrategy</returns> public static List <IUIAutomationElement> GetAllDescendents(this IUIAutomationElement element, Func <IUIAutomationElement, bool> searchStrategy = null) { var toReturn = new List <IUIAutomationElement>(); var walker = new CUIAutomation8().RawViewWalker; var toSearch = new Queue <IUIAutomationElement>();//BFS toSearch.Enqueue(element); while (toSearch.Any())//beware infinite recursion. If this becomes a problem add a limit { var current = toSearch.Dequeue(); for (var child = walker.GetFirstChildElement(current); child != null; child = walker.GetNextSiblingElement(child)) { toSearch.Enqueue(child); } if (searchStrategy == null || searchStrategy(current)) { toReturn.Add(current); } } return(toReturn); }
public static bool OpenBarMenu() { try { //197170 var winBarUia = UiaHelper.GetUIAutomation().ElementFromHandle((IntPtr)197170); var elements = winBarUia.FindAll(UIAutomationClient.TreeScope.TreeScope_Descendants, UiaHelper.GetUIAutomation(). CreateTrueCondition()); IUIAutomationElement mation = null; for (var i = 0; i < elements.Length; i++) { if (elements.GetElement(i).CurrentName == "增值税普通发票填开") { mation = elements.GetElement(i); } Console.WriteLine($"name:{elements.GetElement(i).CurrentName},handler:{elements.GetElement(i).CurrentNativeWindowHandle}"); } var pt = (IUIAutomationInvokePattern)mation.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); Task.Factory.StartNew(() => { pt.Invoke(); }); } catch (Exception e) { Console.WriteLine(e); } return(false); }
public static IUIAutomationElement FindDescendantByName( this IUIAutomationElement parent, string name ) { if (parent == null) { throw new ArgumentNullException(nameof(parent)); } var condition = Helper.Automation.CreatePropertyCondition( AutomationElementIdentifiers.NameProperty.Id, name ); var child = Helper.Retry( () => parent.FindFirst(TreeScope.TreeScope_Descendants, condition), AutomationRetryDelay, retryCount: AutomationRetryCount ); if (child == null) { throw new InvalidOperationException( $"Could not find item with name '{name}' under '{parent.GetNameForExceptionMessage()}'." ); } return(child); }
private static void ThrowUnableToFindChildException(string path, IUIAutomationElement item) { // if not found, build a list of available children for debugging purposes var validChildren = new List <string>(); try { var children = item.GetCachedChildren(); for (var i = 0; i < children.Length; i++) { validChildren.Add(SimpleControlTypeName(children.GetElement(i))); } } catch (InvalidOperationException) { // if the cached children can't be enumerated, don't blow up trying to display debug info } throw new InvalidOperationException( string.Format( "Unable to find a child named {0}. Possible values: ({1}).", path, string.Join(", ", validChildren) ) ); }
/// <summary> /// Populate siblings /// </summary> /// <param name="parentNode"></param> /// <param name="poiNode"></param> /// <param name="startId"></param> private int PopulateSiblingTreeNodes(A11yElement parentNode, A11yElement poiNode) { int childId = 1; IUIAutomationTreeWalker walker = this.TreeWalker; IUIAutomationElement child = null; if ((IUIAutomationElement)parentNode.PlatformObject != null) { try { child = walker.GetFirstChildElement((IUIAutomationElement)parentNode.PlatformObject); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types while (child != null) { #pragma warning disable CA2000 // Use recommended dispose patterns var childNode = new DesktopElement(child, true, false); #pragma warning restore CA2000 // Use recommended dispose patterns childNode.PopulateMinimumPropertiesForSelection(); if (childNode.IsSameUIElement(poiNode) == false) { childNode.UniqueId = childId++; childNode.Parent = parentNode; childNode.TreeWalkerMode = this.TreeWalkerMode; this.Items.Add(childNode); } else { childNode = poiNode as DesktopElement; } parentNode.Children.Add(childNode); try { child = walker.GetNextSiblingElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types } } return(childId); }
/// <summary> /// Private ctor to set common fields /// </summary> private EventListenerBase(IUIAutomationElement element, TreeScope scope, int eventId, HandleUIAutomationEventMessage peDelegate) { this.EventId = eventId; this.Element = element; this.ListenEventMessage = peDelegate; this.Scope = scope; }
public AutomationElement FindFirst(TreeScope scope, Condition condition) { Validate.ArgumentNotNull(parameter: condition, parameterName: nameof(condition)); var firstBuildCache = IUIAutomationElement.FindFirstBuildCache(scope: UiaConvert.Convert(treeScope: scope), condition: condition.IUIAutomationCondition, cacheRequest: DefaultCacheRequest.IUIAutomationCacheRequest); return(firstBuildCache != null ? new AutomationElement(autoElement: firstBuildCache) : null); }
public object GetCachedPropertyValue(AutomationProperty property, bool ignoreDefaultValue) { Validate.ArgumentNotNull(parameter: property, parameterName: nameof(property)); var cachedPropertyValueEx = IUIAutomationElement.GetCachedPropertyValueEx(propertyId: property.Id, ignoreDefaultValue: Convert.ToInt32(value: ignoreDefaultValue)); return(UiaConvert.ConvertPropertyValue(property: property, propertyValueVariant: cachedPropertyValueEx)); }
private AutomationElement(IUIAutomationElement elem) { pElement = elem; lock (typeof(AutomationElement)) { ++instances; } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // InvokeLinkInternal() // // Invoke a hyperlink in the browser window. // // Runs on the background thread. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void InvokeLinkInternal(IUIAutomationElement elementLink, bool fUseCache) { if (elementLink != null) { IUIAutomationInvokePattern pattern = null; int iPatternId = _patternIdInvoke; // Will we be calling the Invoke() method through the Invoke pattern. // So first get the pattern for the hyperlink element. if (fUseCache) { // This does not result in a cross-proc call here, and should not fail. pattern = (IUIAutomationInvokePattern)elementLink.GetCachedPattern(iPatternId); } else { // This will fail if the element no longer exists. try { pattern = (IUIAutomationInvokePattern)elementLink.GetCurrentPattern(iPatternId); } catch { // If an exception is throw trying to access the element, do nothing. This will // occur if the element no longer exists, (eg the browser window has been closed.) } } if (pattern != null) { pattern.Invoke(); } } }
public static IUIAutomationElement FindDescendantByPath( this IUIAutomationElement element, string path ) { var pathParts = path.Split(".".ToCharArray()); // traverse the path var item = element; foreach (var pathPart in pathParts) { var next = item.FindFirst( TreeScope.TreeScope_Descendants, Helper.Automation.CreatePropertyCondition( AutomationElementIdentifiers.LocalizedControlTypeProperty.Id, pathPart ) ); if (next == null) { ThrowUnableToFindChildException(path, item); } item = next; } return(item); }
/// <summary> /// Get all the properties supported by an element /// </summary> /// <param name="element">The element being extended</param> /// <returns>A list of all the properties supported</returns> public static List <string> GetProperties(this IUIAutomationElement element) { int[] ids; string[] names; new CUIAutomation8().PollForPotentialSupportedProperties(element, out ids, out names); return(names.ToList()); }
private void EnumarateChildren(IUIAutomationElement element) { Console.WriteLine("{0}", element.CurrentName.Trim()); IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest(); cacheRequest.AddProperty(System.Windows.Automation.AutomationElement.NameProperty.Id); cacheRequest.AddProperty(System.Windows.Automation.AutomationElement.ControlTypeProperty.Id); cacheRequest.TreeScope = TreeScope.TreeScope_Element | TreeScope.TreeScope_Children | TreeScope.TreeScope_Subtree; IUIAutomationCondition cond; cond = _automation.CreatePropertyConditionEx( System.Windows.Automation.AutomationElement.ControlTypeProperty.Id, System.Windows.Automation.ControlType.Window.Id, PropertyConditionFlags.PropertyConditionFlags_IgnoreCase); IUIAutomationElementArray elementList = element.FindAllBuildCache(TreeScope.TreeScope_Children, cond, cacheRequest); if (elementList == null) { return; } for (int i = 0; i < elementList.Length; i++) { EnumarateChildren(elementList.GetElement(i)); } }
public virtual extern void IUIAutomation3_PollForPotentialSupportedProperties( [MarshalAs(unmanagedType: UnmanagedType.Interface), In] IUIAutomationElement pElement, [MarshalAs(unmanagedType: UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_INT)] out int[] propertyIds, [MarshalAs(unmanagedType: UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] out string[] propertyNames);
private void RegisterSelChangedEventAT(object state) { IUIAutomationElement elem = null; try { pAutomation.ElementFromHandle(handler.GetHwnd(), out elem); if (elem != null) { pAutomation.AddAutomationEventHandler(UIA_Selection_InvalidatedEventId, elem, TreeScope_Element, IntPtr.Zero, handler); pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementAddedToSelectionEventId, elem, TreeScope_Descendants, IntPtr.Zero, handler); pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementRemovedFromSelectionEventId, elem, TreeScope_Descendants, IntPtr.Zero, handler); pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementSelectedEventId, elem, TreeScope_Descendants, IntPtr.Zero, handler); } } catch (Exception ex) { } finally { if (elem != null) { Marshal.ReleaseComObject(elem); } ((AutoResetEvent)state).Set(); } }
public static async Task <IUIAutomationElement> FindAutomationElementAsync( string elementName, bool recursive = false ) { IUIAutomationElement element = null; var scope = recursive ? TreeScope.TreeScope_Descendants : TreeScope.TreeScope_Children; var condition = Helper.Automation.CreatePropertyCondition( AutomationElementIdentifiers.NameProperty.Id, elementName ); // TODO(Dustin): This is code is a bit terrifying. If anything goes wrong and the automation // element can't be found, it'll continue to spin until the heat death of the universe. await IntegrationHelper .WaitForResultAsync( () => (element = Helper.Automation.GetRootElement().FindFirst(scope, condition)) != null, expectedResult : true ) .ConfigureAwait(false); return(element); }
public static bool ExpandAll(this IUIAutomationElement element, IUIAutomation automation, int level) { if (element.CurrentIsOffscreen != 0) { return(false); } var returnValue = false; if (element.GetCurrentPattern(UIA_PatternIds.UIA_ExpandCollapsePatternId) is IUIAutomationExpandCollapsePattern expandCollapsePattern && expandCollapsePattern.CurrentExpandCollapseState != ExpandCollapseState.ExpandCollapseState_LeafNode) { returnValue = true; // TODO: find timing issue and resolve structurally. This issue shows with CalcVolume Thread.Sleep(100); expandCollapsePattern.Expand(); } var condition = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsEnabledPropertyId, true); var item = element.FindAll(TreeScope.TreeScope_Children, condition); for (var i = 0; i < item.Length; i++) { if (ExpandAll(item.GetElement(i), automation, level + 1)) { returnValue = true; } } return(returnValue); }
/// <summary> /// Creates a debug hint /// </summary> /// <param name="owningWindow">The window that owns the hint</param> /// <param name="hintBounds">The hint bounds</param> /// <param name="automationElement">The automation element</param> /// <returns>A debug hint</returns> private DebugHint CreateDebugHint(IntPtr owningWindow, Rect hintBounds, IUIAutomationElement automationElement) { // Enumerate all possible patterns. Note that the performance of this is *very* bad -- hence debug only. var programmaticNames = new List <string>(); foreach (var pn in UiAutomationPatternIds.PatternNames) { try { var pattern = automationElement.GetCurrentPattern(pn.Key); if (pattern != null) { programmaticNames.Add(pn.Value); } } catch (Exception) { } } if (programmaticNames.Any()) { return(new DebugHint(owningWindow, hintBounds, programmaticNames.ToList())); } return(null); }
public static bool CollapseAll(this IUIAutomationElement element, IUIAutomation automation) { if (element.CurrentIsOffscreen != 0) { return(false); } var returnValue = false; var condition = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsEnabledPropertyId, true); var item = element.FindAll(TreeScope.TreeScope_Children, condition); for (var i = 0; i < item.Length; i++) { if (CollapseAll(item.GetElement(i), automation)) { returnValue = true; } } if (!(element.GetCurrentPattern(UIA_PatternIds.UIA_ExpandCollapsePatternId) is IUIAutomationExpandCollapsePattern expandCollapsePattern) || expandCollapsePattern.CurrentExpandCollapseState == ExpandCollapseState.ExpandCollapseState_LeafNode) { return(returnValue); } expandCollapsePattern.Collapse(); return(true); }
/// <summary> /// This method will attempt to set the LegacyIAccessible Value of an element and then press TAB. /// </summary> /// <param name="element"></param> /// <param name="value"></param> public static void xtSetValue(this IUIAutomationElement element, string value) { _LegacyIAccessiblePattern = (IUIAutomationLegacyIAccessiblePattern)element.GetCurrentPattern(UIA_PatternIds.UIA_LegacyIAccessiblePatternId); _LegacyIAccessiblePattern.SetValue(value); Thread.Sleep(100); Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.TAB); }
/// <summary> /// Adds the value of the ClickablePoint property to the A11yElement's Properties dictionary /// </summary> /// <remarks> /// Requesting the clickable point property in Edge can cause a crash, /// so the clickable point property is not initially populated by <see cref="DesktopElementExtensionMethods.PopulatePropertiesAndPatternsFromCache(A11yElement)"/>. /// </remarks> private static void InitClickablePointProperty(this A11yElement a11yElement, IUIAutomationElement uiaElement) { if (a11yElement.IsEdgeElement()) { return; } int id = PropertyType.UIA_ClickablePointPropertyId; double[] clickablePoint = uiaElement.GetCurrentPropertyValue(id); if (clickablePoint == null) { return; } string name = A11yAutomation.UIAutomationObject.GetPropertyProgrammaticName(id); var prop = new A11yProperty { Id = id, Name = name, Value = new Point((int)clickablePoint[0], (int)clickablePoint[1]) }; a11yElement.Properties.Add(id, prop); }
///////////////////////////////////////////////////////////////////////////////////////////////// // // HandleStructureChangedEvent() // // Sample application's handler for UIA StructureChanged events. // // Runs on a background thread create by UIA. // ///////////////////////////////////////////////////////////////////////////////////////////////// public void HandleStructureChangedEvent(IUIAutomationElement sender, StructureChangeType changeType, int[] array) { // A structure changed event has been sent by the browser window element or some descendant of it. // Check that this event hasn't arrived around the time we're removing the event handler on shutdown. if (!_fAddedEventHandler) { return; } // For this sample, all the event handler needs to do is notify the main UI thread that the // list of hyperlinks should be refreshed to make sure it's showing the most current list. // A shipping event handler might want to interact with the sender of the events. // For example if this was a Focus Changed event handler, it might get the cached // bounding rect from the sender to highlight where focus is now. Preferably the // work done is kept to a minimum in the UIA event handler itself, so the handler // could signal to another thread that action is required. // Note that the browser window can send many Structure Changed events in rapid succession. // There's nothing to be gained by trying to act on every event received, so only refresh // the list of hyperlinks 2 seconds after an event is received. (This delay would want to // be tuned for the best user experience in a shipping app.) if (_timerRefresh == null) { _timerRefresh = new System.Windows.Forms.Timer(); _timerRefresh.Tick += new EventHandler(timerRefresh_Tick); _timerRefresh.Interval = 2000; _timerRefresh.Start(); } }
StructureChangedEventHandlerImpl( IUIAutomationElement uiAutomationElement, StructureChangedEventHandler handlingDelegate) { this._uiAutomationElement = uiAutomationElement; this._handlingDelegate = handlingDelegate; }
public ViewManager(IntPtr hwnd) { _rootElement = _automation.ElementFromHandle(hwnd); if (null == _rootElement) { throw this.exception; } this.vt = new ViewTree(_rootElement, _automation); this.vt.BuildTree(); }
public void ClickElement(IUIAutomationElement element) { int left = element.CurrentBoundingRectangle.left; int right = element.CurrentBoundingRectangle.right; int top = element.CurrentBoundingRectangle.top; int bottom = element.CurrentBoundingRectangle.bottom; int x = (left + right) / 2; int y = (top + bottom) / 2; NativeMethods.SetCursorPos(x, y); Mouse.LeftClick(); Thread.Sleep(3000); }
public void MyTestInitialize() { // Create the factory and register schemas _factory = new CUIAutomationClass(); ReadyStateSchema.GetInstance().Register(); TestSchema.GetInstance().Register(); ColorSchema.GetInstance().Register(); // Start the app var curDir = Environment.CurrentDirectory; _app = new TargetApp(curDir + "\\UiaControls.exe"); _app.Start(); // Find the main control var appElement = _factory.ElementFromHandle(_app.MainWindow); var condition = _factory.CreatePropertyCondition( UIA_PropertyIds.UIA_AutomationIdPropertyId, "triColorControl1"); _customElement = appElement.FindFirst(TreeScope.TreeScope_Children, condition); Assert.IsNotNull(_customElement); }
public void ScrollToRight(IUIAutomationElement element) { Scroller.HorScroll(element.CurrentNativeWindowHandle, 200); Thread.Sleep(1000); }
public void Dispose() { if(pElement != null) { Marshal.ReleaseComObject(pElement); pElement = null; } GC.SuppressFinalize(this); }
public IUIAutomationElement GetReportElement(IUIAutomationElement rootElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Report this review"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_GroupControlTypeId); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "pdpReportReviewMenu"); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement reportElement = rootElement.FindFirst(TreeScope.TreeScope_Children, conditions); return reportElement; }
public IUIAutomationElement GetTopFreeHome(IUIAutomationElement element) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[2]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Top free"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement topFreeElement = element.FindFirst(TreeScope.TreeScope_Subtree, conditions); Console.WriteLine(topFreeElement.CurrentName); return topFreeElement; }
public IUIAutomationElement GetUpdateDocumentElement(IUIAutomationElement rootElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_DocumentControlTypeId); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "frameHeader"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "document"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement updateElement = rootElement.FindFirst(TreeScope.TreeScope_Children, conditions); return updateElement; }
public void GoBack(IUIAutomationElement updateElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Back"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "backButton"); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement backElement = updateElement.FindFirst(TreeScope.TreeScope_Children, conditions); this.ClickElement(backElement); Thread.Sleep(2000); }
internal Button(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal CheckBox(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal Hyperlink(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
private AutomationElement(IUIAutomationElement elem) { pElement = elem; lock(typeof(AutomationElement)) { ++instances; } }
/// <summary> /// Creates an instance of a desktop element. /// </summary> /// <param name="element"> The automation element for this element. </param> /// <param name="application"> The application parent for this element. </param> /// <param name="parent"> The parent element for this element. </param> protected DesktopElement(IUIAutomationElement element, Application application, ElementHost parent) : base(application, parent) { NativeElement = element; }
/// <summary> /// Creates an element from the automation element. /// </summary> /// <param name="element"> The element to create. </param> /// <param name="application"> The application parent for this element. </param> /// <param name="parent"> The parent of the element to create. </param> internal static DesktopElement Create(IUIAutomationElement element, Application application, DesktopElement parent) { var itemType = element.CurrentControlType; switch (itemType) { case UIA_ControlTypeIds.UIA_ButtonControlTypeId: return new Button(element, application, parent); case UIA_ControlTypeIds.UIA_CalendarControlTypeId: return new Calendar(element, application, parent); case UIA_ControlTypeIds.UIA_CheckBoxControlTypeId: return new CheckBox(element, application, parent); case UIA_ControlTypeIds.UIA_ComboBoxControlTypeId: return new ComboBox(element, application, parent); case UIA_ControlTypeIds.UIA_CustomControlTypeId: return new Custom(element, application, parent); case UIA_ControlTypeIds.UIA_DataGridControlTypeId: return new DataGrid(element, application, parent); case UIA_ControlTypeIds.UIA_DataItemControlTypeId: return new DataItem(element, application, parent); case UIA_ControlTypeIds.UIA_DocumentControlTypeId: return new Document(element, application, parent); case UIA_ControlTypeIds.UIA_EditControlTypeId: return new Edit(element, application, parent); case UIA_ControlTypeIds.UIA_GroupControlTypeId: return new Group(element, application, parent); case UIA_ControlTypeIds.UIA_HeaderControlTypeId: return new Header(element, application, parent); case UIA_ControlTypeIds.UIA_HeaderItemControlTypeId: return new HeaderItem(element, application, parent); case UIA_ControlTypeIds.UIA_HyperlinkControlTypeId: return new Hyperlink(element, application, parent); case UIA_ControlTypeIds.UIA_ImageControlTypeId: return new Image(element, application, parent); case UIA_ControlTypeIds.UIA_ListControlTypeId: return new List(element, application, parent); case UIA_ControlTypeIds.UIA_ListItemControlTypeId: return new ListItem(element, application, parent); case UIA_ControlTypeIds.UIA_MenuControlTypeId: return new Menu(element, application, parent); case UIA_ControlTypeIds.UIA_MenuBarControlTypeId: return new MenuBar(element, application, parent); case UIA_ControlTypeIds.UIA_MenuItemControlTypeId: return new MenuItem(element, application, parent); case UIA_ControlTypeIds.UIA_PaneControlTypeId: return new Pane(element, application, parent); case UIA_ControlTypeIds.UIA_ProgressBarControlTypeId: return new ProgressBar(element, application, parent); case UIA_ControlTypeIds.UIA_RadioButtonControlTypeId: return new RadioButton(element, application, parent); case UIA_ControlTypeIds.UIA_SeparatorControlTypeId: return new Separator(element, application, parent); case UIA_ControlTypeIds.UIA_ScrollBarControlTypeId: return new ScrollBar(element, application, parent); case UIA_ControlTypeIds.UIA_SemanticZoomControlTypeId: return new SemanticZoom(element, application, parent); case UIA_ControlTypeIds.UIA_SliderControlTypeId: return new Slider(element, application, parent); case UIA_ControlTypeIds.UIA_SpinnerControlTypeId: return new Spinner(element, application, parent); case UIA_ControlTypeIds.UIA_SplitButtonControlTypeId: return new SplitButton(element, application, parent); case UIA_ControlTypeIds.UIA_StatusBarControlTypeId: return new StatusBar(element, application, parent); case UIA_ControlTypeIds.UIA_TabControlTypeId: return new TabControl(element, application, parent); case UIA_ControlTypeIds.UIA_TabItemControlTypeId: return new TabItem(element, application, parent); case UIA_ControlTypeIds.UIA_TableControlTypeId: return new Table(element, application, parent); case UIA_ControlTypeIds.UIA_TextControlTypeId: return new Text(element, application, parent); case UIA_ControlTypeIds.UIA_ThumbControlTypeId: return new Thumb(element, application, parent); case UIA_ControlTypeIds.UIA_TitleBarControlTypeId: return new TitleBar(element, application, parent); case UIA_ControlTypeIds.UIA_ToolBarControlTypeId: return new ToolBar(element, application, parent); case UIA_ControlTypeIds.UIA_ToolTipControlTypeId: return new ToolTip(element, application, parent); case UIA_ControlTypeIds.UIA_TreeControlTypeId: return new Tree(element, application, parent); case UIA_ControlTypeIds.UIA_TreeItemControlTypeId: return new TreeItem(element, application, parent); case UIA_ControlTypeIds.UIA_WindowControlTypeId: return new Window(element, application, parent); default: Debug.WriteLine("Need to add support for [" + itemType + "] element."); return new DesktopElement(element, application, parent); } }
internal Document(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal Header(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal ToolTip(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
public IUIAutomationElement GetUpdateLinkElement(IUIAutomationElement updateDocumentElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[3]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_HyperlinkControlTypeId); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "headerLinkUpdates"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement updateLinkElement = updateDocumentElement.FindFirst(TreeScope.TreeScope_Children, conditions); return updateLinkElement; }
internal Window(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal List(IUIAutomationElement element, Application application, DesktopElement parent) : base(element, application, parent) { }
internal SplitButton(IUIAutomationElement element, Application application, DesktopElement parent) : base(element, application, parent) { }
public IUIAutomationElement[] GetTopFreeHomeArray(IUIAutomationElement element) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[2]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Top free"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElementArray topFreeList = element.FindAll(TreeScope.TreeScope_Children, conditions); IUIAutomationElement[] topFreeArray = new IUIAutomationElement[topFreeList.Length]; for (int i = 0; i < topFreeList.Length; i++) { topFreeArray[i] = topFreeList.GetElement(i); } return topFreeArray; }
internal ProgressBar(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
public IUIAutomationElement GetScrollingContainerElement(IUIAutomationElement rootElement) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "wsFrame"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_DocumentControlTypeId); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "document"); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement frameElement = rootElement.FindFirst(TreeScope.TreeScope_Children, conditions); conditionArray = new IUIAutomationCondition[3]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "mainContent"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListControlTypeId); conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement contentElement = frameElement.FindFirst(TreeScope.TreeScope_Children, conditions); conditionArray = new IUIAutomationCondition[4]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Scrolling Container"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_GroupControlTypeId); conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_FrameworkIdPropertyId, "InternetExplorer"); conditionArray[3] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "group"); conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElement scrollingContainerElement = contentElement.FindFirst(TreeScope.TreeScope_Children, conditions); return scrollingContainerElement; }
internal SemanticZoom(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal ListItem(IUIAutomationElement element, Application application, Element parent) : base(element, application, parent) { }
internal AutomationElement(IUIAutomationElement pElement, AutomationElementFactory factory) { this.factory = factory; this.pElement = pElement; factory.AddToDisposeList(this); }