UIAutomation.HasControlInputCmdletBase.OnUIRecordingAutomationEvent C# (CSharp) Method

OnUIRecordingAutomationEvent() protected method

protected OnUIRecordingAutomationEvent ( object src, UIANET::System.Windows.Automation e ) : void
src object
e UIANET::System.Windows.Automation
return void
        protected internal void OnUIRecordingAutomationEvent(
            object src,
            classic.AutomationEventArgs e)
        {
            try { // experimental
                
                IUiElement sourceElement;
                string elementTitle = String.Empty;
                string elementType = String.Empty;
                classic.AutomationEvent eventId = null;
                
                try {
                    
                    sourceElement = src as IUiElement;
                    // 20140312
                    // try { elementTitle = sourceElement.Cached.Name; } catch { }
                    // try { elementTitle = (sourceElement as ISupportsCached).Cached.Name; } catch { }
                    try { elementTitle = sourceElement.GetCached().Name; } catch { }
                    try {
                        // 20140312
                        elementType =
                            // sourceElement.Cached.ControlType.ProgrammaticName;
                            // (sourceElement as ISupportsCached).Cached.ControlType.ProgrammaticName;
                            sourceElement.GetCached().ControlType.ProgrammaticName;
                    } catch { }
    
                    try {
                        elementType = 
                            elementType.Substring(
                            elementType.IndexOf('.') + 1);
                        if (elementType.Length == 0) {
                            return;
                        }
                    } catch { }
                    
                    try {
                        eventId = e.EventId;
                        if (sourceElement == null ||
                            elementType.Length == 0 ||
                            eventId == null) {
                            return;
                        }
                    } catch { }
                } catch (classic.ElementNotAvailableException) {
                    return;
                }
                // try {
                string whatToWrite = String.Empty;
                string specificToEvent = String.Empty;
                // 
                try {
                    if (eventId == classic.AutomationElement.AsyncContentLoadedEvent) {
                        specificToEvent = "#AsyncContentLoadedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.SelectionItemPattern.ElementAddedToSelectionEvent) {
                        specificToEvent =
                            "SelectItem -AddToSelection:$true -ItemName " + 
                            elementTitle;
                    }
                    if (eventId == classic.SelectionItemPattern.ElementRemovedFromSelectionEvent) {
                        specificToEvent =
                            "SelectItem -RemoveFromSelection:$true -ItemName " + 
                            elementTitle;
                    }
                    if (eventId == classic.SelectionItemPattern.ElementSelectedEvent) {
                        specificToEvent =
                            "SelectItem -ItemName " + 
                            elementTitle;
                    }
                    if (eventId == classic.SelectionPattern.InvalidatedEvent) {
                        specificToEvent = "#InvalidatedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.InvokePattern.InvokedEvent) {
                        specificToEvent = "Click";
                    }
                    if (eventId == classic.AutomationElement.LayoutInvalidatedEvent) {
                        specificToEvent = "#LayoutInvalidatedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.MenuClosedEvent) {
                        specificToEvent = "#MenuClosedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.MenuOpenedEvent) {
                        specificToEvent = "#MenuOpenedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.TextPattern.TextChangedEvent) {
                        specificToEvent = "#TextChangedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.TextPattern.TextSelectionChangedEvent) {
                        specificToEvent = "#TextSelectionChangedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.ToolTipClosedEvent) {
                        specificToEvent = "#ToolTipClosedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.ToolTipOpenedEvent) {
                        specificToEvent = "#ToolTipOpenedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.WindowPattern.WindowOpenedEvent) {
                        specificToEvent = "#WindowOpenedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.AutomationFocusChangedEvent) {
                        specificToEvent = "#AutomationFocusChangedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.AutomationElement.AutomationPropertyChangedEvent) {
                        specificToEvent = "#AutomationPropertyChangedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                        // specificToEvent += "old value: ";
                        // specificToEvent += eventId.
                    }
                    if (eventId == classic.AutomationElement.StructureChangedEvent) {
                        specificToEvent = "#StructureChangedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                    if (eventId == classic.WindowPattern.WindowClosedEvent) {
                        specificToEvent = "#WindowClosedEvent triggered\r\n#source title: " + 
                            elementTitle + " of the type " + elementType;
                    }
                } catch (Exception e1) {
                    WriteVerbose(this,
                                 "Event handling for element: " +
                        // 20140312
                                 // sourceElement.Current.Name +
                        sourceElement.GetCurrent().Name + 
                                 " eventId: " +
                                 eventId + 
                                 " failed");
                    WriteVerbose(this,
                                 e1.Message);
                }
// } else {
//  // handle any other events
// }
                if (specificToEvent.Length > 0) {
                    if (specificToEvent.Substring(0, 1) != "#") {
                        // 20131227
                        // whatToWrite += "Invoke-UIA";
                        whatToWrite += "Invoke-Uia";
                        whatToWrite += elementType;
                        whatToWrite += specificToEvent;
                    } else {
                        whatToWrite = specificToEvent;
                    }
                    if (whatToWrite != 
                        // ((ArrayList)Recording[Recording.Count - 1])[0].ToString()) {
                        // ((ArrayList)Recording[Recording.Count - 1]).Insert(0, whatToWrite);
                        (Recording[Recording.Count - 1])[0].ToString()) {
                        (Recording[Recording.Count - 1]).Insert(0, whatToWrite);
                    }
                }
            //} catch { return; }
            
            
            } catch (Exception eUnknown) {
                // WriteVerbose("!!!OnUIRecording " + eUnknown.Message);
                WriteDebug(this, eUnknown.Message);
            } // experimental
            try {
                // 20131109
                WriteVerbose(this, e.EventId + "on " + (src as classic.AutomationElement) + " fired");
                WriteVerbose(this, e.EventId + "on " + (src as IUiElement) + " fired");
            } catch { }
        }
        #endregion Event handling for recording