/// <summary> /// Adds a property changed automation event handler /// </summary> /// <param name="hWnd">window handle</param> /// <param name="property">the property to track</param> /// <param name="element">the element in the window to track</param> /// <param name="eventHandler">the event handler</param> static public void AddAutomationPropertyChangedEventHandler(IntPtr hWnd, AutomationProperty property, AutomationElement element, AutomationPropertyChangedEventHandler eventHandler) { lock (WindowTable) { var windowElement = (WindowElement)WindowTable[hWnd]; if (windowElement == null) { windowElement = new WindowElement(hWnd); WindowTable.Add(hWnd, windowElement); windowElement.EvtOnWindowClosed += windowElement_EvtOnWindowClosed; } var item = new AddAutomationPropertyChangedItem { Property = property, AutoElement = element, EventHandler = eventHandler, WinElement = windowElement }; AddAutomationPropertyChanged(item); } }
public override AutomationElement FromHandle(IntPtr hwnd) { CheckConnected(); var xWinElem = new WindowElement(Client.GetWindows().Single(i => i.Handle == hwnd.ToInt64()), this); return(new AutomationElement(xWinElem)); }
/// <summary> /// Shows the given window instance /// </summary> public void Show(IWindow window) { this.InvokeAsynchronouslyInBackground ( () => { WindowElement element = window as WindowElement; this.desktop.AddElement(element); element.Show(); this.InvokeAsynchronouslyInBackground ( () => { if (element.DataContext is INavigationViewModel) { INavigationViewModel windowViewModel = element.DataContext as INavigationViewModel; ((IClosableViewModel)windowViewModel).Title = element.Title; this.Windows.Add(windowViewModel); } } ); } ); }
private bool ClosePopUp(WindowElement window, PopUpConfig config) { if (!string.IsNullOrWhiteSpace(config.LabelTextRegEx) && !IsTitleAMatch(config.TitleRegEx, window)) { return(false); } if (!string.IsNullOrWhiteSpace(config.LabelxPath)) { var label = window.Query(config.LabelxPath).FirstOrDefault(); if (label == null) { return(false); } if (!string.IsNullOrWhiteSpace(config.LabelTextRegEx) && !Regex.IsMatch(label.Item.Current.Name, config.LabelTextRegEx)) { return(false); } } if (string.IsNullOrWhiteSpace(config.ButtonxPath)) { return(false); } var btn = window.Query(config.ButtonxPath).FirstOrDefault(); if (btn == null) { return(false); } Thread.Sleep(100); btn.Click(); Thread.Sleep(400); return(true); }
/// <summary> /// Process the given <see cref="NavigationResponse"/> instance /// </summary> /// <param name="response"></param> void INavigationHandler.ProcessResponse(NavigationResponse response) { // basic check if (response == null) { throw new ArgumentNullException("response"); } // we check if the navigation was successfull or not if (response.Status != ResponseStatus.Success) { this.PublishNavigationFailedInfo(response.Request); IShowMessageViewService showMessageService = ViewServiceLocator.GetViewService <IShowMessageViewService>(); showMessageService.ButtonSetup = DialogButton.Ok; showMessageService.Caption = "Chronos - Error en la navegación"; showMessageService.Text = ((response.Error != null) ? response.Error.Message : String.Format("No ha sido posible resolver la navegación solicitada ({0})", response.Request.RequestUrl)); showMessageService.ShowMessage(); } else { Application.Current.Dispatcher.BeginInvoke ( DispatcherPriority.Background, new ThreadStart ( () => { WindowElement window = response.Content as WindowElement; if (response.ResponseParameters != null && response.ResponseParameters.Count > 0) { ISupportNavigationLifecycle supporter = nRoute.Navigation.NavigationService.GetSupporter <ISupportNavigationLifecycle>(response.Content); if (supporter != null) { supporter.Initialize(response.ResponseParameters); } } this.PublishNavigatedInfo(window.Title, response); if (response.Request.NavigationMode == NavigateMode.Modal) { ServiceLocator.GetService <IVirtualDesktopManager>().ShowDialog(window); } else { ServiceLocator.GetService <IVirtualDesktopManager>().Show(window); } } ) ); } }
public void SetWindowSimple(Element startingElement, SearchCondition Condition, TreeScope treeScope, int TimeOutInSeconds = WINDOWTIMEOUT) { bool found = false; IUIAutomationElement windowToFind = null; DateTime dtStart = DateTime.Now; DateTime dtCurrent; Element[] childrenOfRoot = null; for (int i = 0; i < 200; i++) { windowToFind = null; childrenOfRoot = null; new BaseWindow(this.AppUnderTest); windowToFind = startingElement.GetControl(treeScope, Condition).IUIElement; if (windowToFind != null) { break; } dtCurrent = DateTime.Now; if (windowToFind == null) { Console.WriteLine($"Window does not exist yet."); Console.WriteLine($"CheckForWindow Method - Iteration: [{i}] - Seconds Spent: [{(dtCurrent - dtStart).TotalSeconds}]"); Thread.Sleep(1000); } else { break; } if ((dtCurrent - dtStart).TotalSeconds > TimeOutInSeconds) { break; } } if (windowToFind == null) { throw new Exception($"Window was not found!!!"); } else { Window = new WindowElement(windowToFind); } Window.Pattern_Window().WaitForWindowReadyState(TimeOutInSeconds); Console.WriteLine($"Window identified - Window Name: [{Window.Name}] - Window Id: [{Window.AutomationId}]"); DateTime dtEnd = DateTime.Now; Console.WriteLine($"Total Time for SetWindow Method: {(dtEnd - dtStart).ToString()}"); }
private void btnExportXML_Click(object sender, EventArgs e) { var selected = GetSelectedWindowHandle(); var windowEl = new WindowElement(AutomationElement.FromHandle(selected.Handle)); var saveDlg = new SaveFileDialog() { Title = "Save XML", RestoreDirectory = true, Filter = "xml (*.xml) | *.xml" }; saveDlg.ShowDialog(); File.WriteAllText(saveDlg.FileName, windowEl.ToXml()); }
public IEnumerable <WindowElement> FindFloatingMenuElements(WindowElement src) { var currentNotepad = rootElement.FindFirst(TreeScope.Children, notePadCondition); if (currentNotepad != null) { var menuElements = currentNotepad.FindAll(TreeScope.Descendants, menuCondition); if (menuElements.Count > 0) { AutomationElement menuHiddenElement = menuElements[menuElements.Count - 1]; return(ToWindowElements(menuHiddenElement.FindAll(TreeScope.Children, menuItemCondition))); } } return(new List <WindowElement>()); }
/// <summary> /// Closes the current dialog /// </summary> public void CloseDialog() { Logger.Debug("Cierre de la ventana modal activa"); this.VerifyActiveDesktop(); this.Invoke( delegate { WindowElement window = WindowElement.ModalContainerPanel.Children.OfType <WindowElement>().FirstOrDefault(); if (window != null) { window.Hide(); } }); }
/// <summary> /// Adds an automation event handler for the specified element in the /// specified window /// </summary> /// <param name="hWnd">window handle</param> /// <param name="autoEvent">the event to add</param> /// <param name="element">the automation element for the control</param> /// <param name="eventHandler">the event handler for the event</param> static public void AddAutomationEventHandler(IntPtr hWnd, AutomationEvent autoEvent, AutomationElement element, AutomationEventHandler eventHandler) { Log.Debug(); var windowElement = (WindowElement)WindowTable[hWnd]; if (windowElement == null) { windowElement = new WindowElement(hWnd); WindowTable.Add(hWnd, windowElement); var control = Form.FromHandle(hWnd); if (control is Form) { var form = control as Form; form.VisibleChanged += form_VisibleChanged; form.FormClosing += form_FormClosing; } else { windowElement.EvtOnWindowClosed += windowElement_EvtOnWindowClosed; } } else { Log.Debug("Found window element"); } // create the item and add it var item = new AddEventHandlerItem { AutoEvent = autoEvent, AutoElement = element, EventHandler = eventHandler, WinElement = windowElement }; AddAutomationEvent(item); }
/// <summary> /// <para>Sets/Resets the Pattern and its properties.</para> /// <para>Useful in this class for resetting the Pattern's properties after an action is performed on the Element.</para> /// </summary> private void Initialize(WindowElement element) { this._element = element; int counter = 0; do { try { _WindowPattern = (IUIAutomationWindowPattern)element.IUIElement.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId); } catch (COMException e) { } catch (InvalidOperationException ex) { } Thread.Sleep(10); } while (_WindowPattern == null && counter++ < 50); if (_WindowPattern == null) { throw new Exception($"Pattern is not available for Element where Name=[{_element.Name}] - AutomationId=[{_element.AutomationId}] - ClassName=[{_element.ClassName}]."); } this.SetProperties(); }
public IEnumerable <WindowElement> FindAllElements(WindowElement src) { var floatingMenuBarElements = FindFloatingMenuElements(src); return(floatingMenuBarElements.Concat(menuBarElements)); }
WindowElement InitializeButton( IWindowElement window, IWindowElement other, BindingPosition position ) { Debug.Assert( Dispatcher.CurrentDispatcher == NoFocusManager.Default.ExternalDispatcher, "This method should only be called by the ExternalThread." ); WindowElement button = null; button = new WindowElement( new UnbindButtonView() { DataContext = CreateVM( window, other, position ) }, "UnbindButton" ); DoPlaceButtons( button, window, position ); TopMostService.Service.RegisterTopMostElement( "30", button.Window ); return button; }
/// <summary> /// Attempts to find the window starting from the Root Element (Desktop). /// </summary> /// <param name="Condition"></param> /// <param name="Scope"></param> /// <param name="SetMaximize"></param> /// <param name="TimeOutInSeconds"></param> public void SetWindow(SearchCondition Condition, bool SetMaximize, int TimeOutInSeconds = WINDOWTIMEOUT) { bool found = false; IUIAutomationElement windowToFind = null; DateTime dtStart = DateTime.Now; DateTime dtCurrent; Element[] childrenOfRoot = null; for (int i = 0; i < 200; i++) { windowToFind = null; childrenOfRoot = null; new BaseWindow(this.AppUnderTest); childrenOfRoot = ROOT.GetAllElements(TreeScope.TreeScope_Children); var el = FindElementInArray(childrenOfRoot, Condition, this.AppUnderTest.ProcessId); if (el != null) { windowToFind = el.IUIElement; } if (windowToFind == null) { for (int j = 0; j < childrenOfRoot.Length; j++) { if (childrenOfRoot[j].ProcessId == this.AppUnderTest.ProcessId) { var child = childrenOfRoot[j].GetRelativeElementUntilFound(ElementRelative.FirstChild, Condition, 30); if (child != null && !child.IsNull) { windowToFind = child.IUIElement; } } if (windowToFind != null) // && !windowToFind.IsNull) { break; } } } else { break; } dtCurrent = DateTime.Now; if (windowToFind == null) // || windowToFind.IsNull) { Console.WriteLine($"Window does not exist yet."); Console.WriteLine($"CheckForWindow Method - Iteration: [{i}] - Seconds Spent: [{(dtCurrent - dtStart).TotalSeconds}]"); Thread.Sleep(1000); } else { break; } if ((dtCurrent - dtStart).TotalSeconds > TimeOutInSeconds) { break; } } if (windowToFind == null) // || windowToFind.IsNull) { throw new Exception($"Window was not found!!!"); } else { Window = new WindowElement(windowToFind); //.IUIElement); } Window.Pattern_Window().WaitForWindowReadyState(TimeOutInSeconds); Console.WriteLine($"Window identified - Window Name: [{Window.Name}] - Window Id: [{Window.AutomationId}]"); if (SetMaximize) { Window.Pattern_Window().Maximize(); } else { Window.SetFocus(); } Thread.Sleep(200); Window.Pattern_Window().WaitForWindowReadyState(TimeOutInSeconds); DateTime dtEnd = DateTime.Now; Console.WriteLine($"Total Time for SetWindow Method: {(dtEnd - dtStart).ToString()}"); }
public void WindowManagerApiTest() { IWindowManager windowManager = new CK.WindowManager.WindowManager(); WindowElement A = new WindowElement( windowManager, new Window() { Top = 10, Left = 10, Width = 10, Height = 10 },"A" ); WindowElement B = new WindowElement( windowManager, new Window() { Top = 10, Left = 20, Width = 10, Height = 10 },"B" ); WindowElement C = new WindowElement( windowManager, new Window() { Top = 10, Left = 30, Width = 10, Height = 10 },"C" ); WindowElement D = new WindowElement( windowManager, new Window() { Top = 20, Left = 20, Width = 10, Height = 10 },"D" ); WindowElement E = new WindowElement( windowManager, new Window() { Top = 20, Left = 40, Width = 10, Height = 10 },"E" ); WindowElement F = new WindowElement( windowManager, new Window(), "F"); // B is linked to A C D // A has 1 binding: AB. // B has 3 bindings: BA BC BD. // C has 1 binding: CB. // D has 1 binding: DB. // E has no binding. // // A-B-C // | E // D WindowElementBinder binder = new WindowElementBinder(); binder.BeforeBinding += ( sender, e ) => { }; WindowBindedEventArgs bindedEvent = null; binder.AfterBinding += ( sender, e ) => { bindedEvent = e; }; // The listener plugin listen the window location change event directly from the WindowManager. // When a Window from A B C or D change the location, all the window must move together. WindowManagerExecutor executor = new WindowManagerExecutor(); executor.WindowManager = windowManager; executor.WindowBinder = binder; executor.Start(); binder.Attach( A, B ); Assert.That( bindedEvent, Is.Not.Null ); Assert.That( bindedEvent.BindingType == BindingEventType.Attach ); Assert.That( bindedEvent.Binding, Is.Not.Null ); Assert.That( bindedEvent.Binding.First == A ); Assert.That( bindedEvent.Binding.Second == B ); binder.Attach( B, C ); Assert.That( bindedEvent.BindingType == BindingEventType.Attach ); Assert.That( bindedEvent.Binding, Is.Not.Null ); Assert.That( bindedEvent.Binding.First == B ); Assert.That( bindedEvent.Binding.Second == C ); binder.Attach( B, D ); Assert.That( bindedEvent.BindingType == BindingEventType.Attach ); Assert.That( bindedEvent.Binding, Is.Not.Null ); Assert.That( bindedEvent.Binding.First == B ); Assert.That( bindedEvent.Binding.Second == D ); A.Window.Left = 15; A.OnWindowLocationChanged( A, EventArgs.Empty ); executor.Stop(); }
//public bool IsModal { get; private set; } public WindowPattern(WindowElement element) { Initialize(element); }
private bool IsTitleAMatch(string regEx, WindowElement window) { return(Regex.IsMatch(window.Item.Current.Name, regEx, RegexOptions.IgnoreCase)); }
private void CloseWindow(WindowElement win) { var closeBtn = win.Query(@"//Element[@Type='title bar']/Children/Element[@Type='button' and @Name='Close']").FirstOrDefault(); closeBtn.Click(); }
public static IElement ParseElement(string localizedType, string designedId, string designedName, IElement parent) { IElement element = null; switch (localizedType.ToLower()) { case ElementBase.WINDOW: element = new WindowElement(); element.Attributes.ElementType = ElementBase.WINDOW_TYPE; break; case ElementBase.DIALOG: element = new WindowElement(); element.Attributes.ElementType = ElementBase.DIALOG_TYPE; break; case ElementBase.BUTTON: element = new ButtonElement(); element.Attributes.ElementType = ElementBase.BUTTON_TYPE; break; case ElementBase.COMBO_BOX: element = new ComboBoxElement(); element.Attributes.ElementType = ElementBase.COMBOBOX_TYPE; break; case ElementBase.TEXT: element = new TextElement(); element.Attributes.ElementType = ElementBase.TEXT_TYPE; break; case ElementBase.PANE: element = new ContainerElement(); element.Attributes.ElementType = ElementBase.CONTAINER_TYPE; break; case ElementBase.TITLE_BAR: element = new TitleBarElement(); element.Attributes.ElementType = ElementBase.TITLEBAR_TYPE; break; case ElementBase.MENU_BAR: element = new MenuBarElement(); element.Attributes.ElementType = ElementBase.MENUBAR_TYPE; break; case ElementBase.DOCUMENT: element = new EditableTextElement(); element.Attributes.ElementType = ElementBase.EDITABLETEXT_TYPE; break; case ElementBase.TAB: element = new TabPageElement(); element.Attributes.ElementType = ElementBase.TABPAGE_TYPE; break; case ElementBase.TAB_ITEM: element = new TabItemElement(); element.Attributes.ElementType = ElementBase.TABITEM_TYPE; break; case ElementBase.SCROLL_BAR: element = new ScrollBarElement(); element.Attributes.ElementType = ElementBase.SCROLLBAR_TYPE; break; //case THUMB: case ElementBase.TREE: element = new TreeViewElement(); element.Attributes.ElementType = ElementBase.TREEVIEW_TYPE; break; case ElementBase.TREE_VIEW: element = new TreeViewElement(); element.Attributes.ElementType = ElementBase.TREEVIEW_TYPE; break; case ElementBase.TREE_ITEM: element = new TreeItemElement(); element.Attributes.ElementType = ElementBase.TREEITEM_TYPE; break; case ElementBase.TABLE: element = new TableElement(); element.Attributes.ElementType = ElementBase.TABLE_TYPE; break; //? case ElementBase.HEADER: element = new HeaderElement(); element.Attributes.ElementType = ElementBase.HEADER_TYPE; break; case ElementBase.ITEM: if (parent != null && (parent is TableElement || parent is DatagridElement)) { element = new RowElement(); element.Attributes.ElementType = ElementBase.ROW_TYPE; } break; case ElementBase.LIST: //(listview or checkedlistbox) element = new ListElement(); element.Attributes.ElementType = ElementBase.LIST_TYPE; break; case ElementBase.LIST_VIEW: //(listview or checkedlistbox) element = new ListElement(); element.Attributes.ElementType = ElementBase.LIST_TYPE; break; case ElementBase.LIST_ITEM: //(table) element = new ListItemElement(); element.Attributes.ElementType = ElementBase.LISTITEM_TYPE; break; case ElementBase.EDIT: //textbox element = new EditableTextElement(); element.Attributes.ElementType = ElementBase.EDITABLETEXT_TYPE; break; case ElementBase.CHECK_BOX: element = new CheckBoxElement(); element.Attributes.ElementType = ElementBase.CHECKBOX_TYPE; break; case ElementBase.RADIO_BUTTON: element = new RadioButtonElement(); element.Attributes.ElementType = ElementBase.RADIO_BUTTON_TYPE; break; case ElementBase.CALENDAR: element = new CalendarElement(); element.Attributes.ElementType = "Calendar"; break; case ElementBase.CUSTOM: element = new CustomElement(); element.Attributes.ElementType = "Custom"; break; case ElementBase.DATAGRID: element = new DatagridElement(); element.Attributes.ElementType = "DataGrid"; break; case ElementBase.DATAGRID2: element = new DatagridElement(); element.Attributes.ElementType = "DataGrid"; break; case ElementBase.DATAITEM: element = new DataitemElement(); element.Attributes.ElementType = "dataitem"; break; case ElementBase.GROUP: element = new GroupELement(); element.Attributes.ElementType = ElementBase.GROUP_TYPE; break; case ElementBase.HEADER_ITEM: element = new HeaderItemElement(); element.Attributes.ElementType = "HeaderItem"; break; case ElementBase.HYPERLINK: element = new LinkElement(); element.Attributes.ElementType = "Hyperlink"; break; case ElementBase.IMAGE: element = new ImageElement(); element.Attributes.ElementType = "Image"; break; case ElementBase.MENU: element = new MenuElement(); element.Attributes.ElementType = "Menu"; break; case ElementBase.PROGRESS_BAR: element = new ProgressBarElement(); element.Attributes.ElementType = "ProgressBar"; break; case ElementBase.SEPARATOR: element = new SeparatorElement(); element.Attributes.ElementType = "Separator"; break; case ElementBase.SLIDER: element = new SliderElement(); element.Attributes.ElementType = "Slider"; break; case ElementBase.SPINNER: element = new SpinnerElement(); element.Attributes.ElementType = "Spinner"; break; case ElementBase.SPLIT_BUTTON: element = new SplitButtonElement(); element.Attributes.ElementType = "SplitButton"; break; case ElementBase.STATUS_BAR: element = new StatusBarElement(); element.Attributes.ElementType = "StatusBar"; break; case ElementBase.TOOL_BAR: element = new ToolBarElement(); element.Attributes.ElementType = "ToolBar"; break; case ElementBase.TOOL_TIP: element = new ToolTipElement(); element.Attributes.ElementType = "ToolTip"; break; //? case ElementBase.MENU_ITEM: element = new MenuItemElement(); element.Attributes.ElementType = "MenuItem"; break; case ElementBase.LINK: element = new LinkElement(); element.Attributes.ElementType = ElementBase.LINK_TYPE; break; } if (element == null) { if (parent is TableElement || parent is DatagridElement) { element = new RowElement(); element.Attributes.ElementType = ElementBase.ROW_TYPE; } else if (parent is RowElement) { element = new CellElement(); element.Attributes.ElementType = ElementBase.CELL_TYPE; } else { element = new UnknownELement(); element.Attributes.ElementType = ElementBase.UNKNOWN_TYPE; } } element.Attributes.DesignedId = designedId; return(element); }