// 20141001 List <IUiElement> GetWindowCollectionByProcessName( // IUiElement rootElement, WindowSearcherData data) { data.InputObject = data.ProcessNames.SelectMany(processName => Process.GetProcessesByName(processName)).ToList().ToArray(); return(GetWindowCollectionFromProcess(data)); }
List <IUiElement> GetWindowCollectionViaWin32( WindowSearcherData data) { return(UiaHelper.EnumChildWindowsFromHandle( data.Name, data.AutomationId, data.Class, IntPtr.Zero)); }
private void TestParametersAgainstCollection( IEnumerable<int> processIds, IEnumerable<string> names, string automationId, string className, IEnumerable<IUiElement> collection, int expectedNumberOfElements) { // Arrange IUiElement rootElement = FakeFactory.GetElement_ForFindAll( collection, null); var windowSearcherData = new WindowSearcherData { ProcessIds = processIds.ToArray(), Name = new[] { string.Empty }, AutomationId = automationId, Class = className, First = false, Recurse = false }; // Act List<IUiElement> resultList = RealCodeCaller.GetResultList_GetWindowCollectionByPid( rootElement, windowSearcherData); // Assert MbUnit.Framework.Assert.Count(expectedNumberOfElements, resultList); Assert.Equal(expectedNumberOfElements, resultList.Count); // string[] controlTypeNames; // switch (selector) { // case UIAutomationUnitTests.Helpers.Inheritance.UsualWildcardRegex.Wildcard: // const WildcardOptions options = WildcardOptions.IgnoreCase; // WildcardPattern namePattern = new WildcardPattern(name, options); // WildcardPattern automationIdPattern = new WildcardPattern(automationId, options); // WildcardPattern classNamePattern = new WildcardPattern(className, options); // WildcardPattern txtValuePattern = new WildcardPattern(txtValue, options); // // if (!string.IsNullOrEmpty(name)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => namePattern.IsMatch(x.Current.Name)); // resultList.All(x => namePattern.IsMatch(x.Current.Name)); // } // if (!string.IsNullOrEmpty(automationId)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => automationIdPattern.IsMatch(x.Current.AutomationId)); // resultList.All(x => automationIdPattern.IsMatch(x.Current.AutomationId)); // } // if (!string.IsNullOrEmpty(className)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => classNamePattern.IsMatch(x.Current.ClassName)); // resultList.All(x => classNamePattern.IsMatch(x.Current.ClassName)); // } // controlTypeNames = // controlTypes.Select(ct => null != ct ? ct.ProgrammaticName.Substring(12) : string.Empty).ToArray(); // if (null != controlType) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => controlTypeNames.Contains(x.Current.ControlType.ProgrammaticName.Substring(12))); // resultList.All(x => controlTypeNames.Contains(x.Current.ControlType.ProgrammaticName.Substring(12))); // } // // if (!string.IsNullOrEmpty(txtValue)) { // MbUnit.Framework.Assert.ForAll( // resultList // .Cast<IUiElement>() // .ToList<IUiElement>(), x => // { // IValuePattern valuePattern = x.GetCurrentPattern<IValuePattern>(ValuePattern.Pattern) as IValuePattern; // return valuePattern != null && txtValuePattern.IsMatch(valuePattern.Current.Value); // }); // // resultList.All( // x => { // IValuePattern valuePattern = x.GetCurrentPattern<IValuePattern>(ValuePattern.Pattern) as IValuePattern; // return valuePattern != null && txtValuePattern.IsMatch(valuePattern.Current.Value); // }); // } // break; // case UIAutomationUnitTests.Helpers.Inheritance.UsualWildcardRegex.Regex: // if (!string.IsNullOrEmpty(name)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => Regex.IsMatch(x.Current.Name, name)); // resultList.All(x => Regex.IsMatch(x.Current.Name, name)); // } // if (!string.IsNullOrEmpty(automationId)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => Regex.IsMatch(x.Current.AutomationId, automationId)); // resultList.All(x => Regex.IsMatch(x.Current.AutomationId, automationId)); // } // if (!string.IsNullOrEmpty(className)) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => Regex.IsMatch(x.Current.ClassName, className)); // resultList.All(x => Regex.IsMatch(x.Current.ClassName, className)); // } // controlTypeNames = // controlTypes.Select(ct => null != ct ? ct.ProgrammaticName.Substring(12) : string.Empty).ToArray(); // if (null != controlType) { // MbUnit.Framework.Assert.ForAll(resultList.Cast<IUiElement>().ToList<IUiElement>(), x => controlTypeNames.Contains(x.Current.ControlType.ProgrammaticName.Substring(12))); // resultList.All(x => controlTypeNames.Contains(x.Current.ControlType.ProgrammaticName.Substring(12))); // } // if (!string.IsNullOrEmpty(txtValue)) { // MbUnit.Framework.Assert.ForAll( // resultList // .Cast<IUiElement>() // .ToList<IUiElement>(), x => // { // IValuePattern valuePattern = x.GetCurrentPattern<IValuePattern>(ValuePattern.Pattern) as IValuePattern; // return valuePattern != null && Regex.IsMatch(valuePattern.Current.Value, txtValue); // }); // Xunit.Assert.True( // resultList.All( // x => { // IValuePattern valuePattern = x.GetCurrentPattern<IValuePattern>(ValuePattern.Pattern) as IValuePattern; // return valuePattern != null && Regex.IsMatch(valuePattern.Current.Value, txtValue); // }) // ); // } // break; // } }
public static List<IUiElement> GetResultList_GetWindowCollectionByPid( IUiElement rootElement, WindowSearcherData data) { var windowSearcher = new WindowSearcher(); List<IUiElement> resultList = windowSearcher.GetWindowCollectionByPid( rootElement, data); return resultList; }
internal List <IUiElement> GetWindowCollectionByPid( IUiElement rootElement, WindowSearcherData data) { classic.AndCondition conditionsForRecurseSearch = null; var elementsByProcessId = new List <IUiElement>(); // 20141001 // if ((null != data.Name && 0 < data.Name.Count()) || if ((null != data.Name && data.Name.Any()) || !string.IsNullOrEmpty(data.AutomationId) || !string.IsNullOrEmpty(data.Class)) { data.Recurse = true; } var conditionWindowPaneMenu = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Pane), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Menu)); foreach (int processId in data.ProcessIds) { conditionsForRecurseSearch = new classic.AndCondition( new classic.PropertyCondition( classic.AutomationElement.ProcessIdProperty, processId), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window)); var conditionsProcessId = new classic.AndCondition( new classic.PropertyCondition( classic.AutomationElement.ProcessIdProperty, processId), conditionWindowPaneMenu); try { if (data.Recurse) { if (data.First) { IUiElement rootWindowElement = rootElement.FindFirst( classic.TreeScope.Children, conditionsProcessId); if (null != rootWindowElement) { elementsByProcessId.Add(rootWindowElement); } } else { IUiEltCollection rootCollection = rootElement.FindAll( classic.TreeScope.Children, conditionsProcessId); if (null != rootCollection && 0 < rootCollection.Count) { elementsByProcessId.AddRange(rootCollection.Cast <IUiElement>()); } } } else { if (data.First) { IUiElement tempElement = rootElement.FindFirst( classic.TreeScope.Children, conditionsProcessId); if (null != tempElement) { elementsByProcessId.Add(tempElement); } } else { IUiEltCollection tempCollection = rootElement.FindAll( classic.TreeScope.Children, conditionsProcessId); if (null != tempCollection && 0 < tempCollection.Count) { elementsByProcessId.AddRange(tempCollection.Cast <IUiElement>()); } } } } catch (Exception) { // WriteVerbose( // this, // "exception: " + // eGetFirstChildOfRootByProcessId.Message); } } if (!data.Recurse || // 20141001 // ((null == data.Name || 0 == data.Name.Count()) && string.IsNullOrEmpty(data.AutomationId) && ((null == data.Name || !data.Name.Any()) && string.IsNullOrEmpty(data.AutomationId) && string.IsNullOrEmpty(data.Class))) { return(elementsByProcessId); } var resultList = new List <IUiElement>(); /* * List<IUiElement> resultList = * new List<IUiElement>(); */ // 20141001 // if (null != data.Name && 0 < data.Name.Count()) { if (null != data.Name && data.Name.Any()) { foreach (string name in data.Name) { var controlSearcherData = new ControlSearcherData { Name = name, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, ControlType = new string[] { "Window" } }; resultList.AddRange( ReturnOnlyRightElements( elementsByProcessId, controlSearcherData, false, true)); } } else { var controlSearcherData = new ControlSearcherData { Name = string.Empty, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, ControlType = new string[] { "Window" } }; resultList.AddRange( ReturnOnlyRightElements( elementsByProcessId, controlSearcherData, false, true)); } elementsByProcessId = resultList; // 20140121 // never ! // if (null != resultList) { // resultList.Clear(); // resultList = null; // } return(elementsByProcessId); }
List <IUiElement> GetWindowCollectionByName( IUiElement rootElement, WindowSearcherData data) { var windowCollectionByProperties = new List <IUiElement>(); var resultCollection = new List <IUiElement>(); if (null == data.Name) { // 20141001 // data.Name = new string[]{ string.Empty }; data.Name = new [] { string.Empty }; } classic.OrCondition conditionsSet = null; if (data.Recurse) { conditionsSet = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), classic.Condition.FalseCondition); } else { conditionsSet = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Pane), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Menu)); } foreach (string windowTitle in data.Name) { IUiEltCollection windowCollection = rootElement.FindAll(data.Recurse ? classic.TreeScope.Descendants : classic.TreeScope.Children, conditionsSet); var controlSearcherData = new ControlSearcherData { Name = windowTitle, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, // 20141001 // ControlType = (new string[]{ "Window", "Pane", "Menu" }) ControlType = (new [] { "Window", "Pane", "Menu" }) }; windowCollectionByProperties = ReturnOnlyRightElements( windowCollection, controlSearcherData, false, true); try { if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count) { foreach (IUiElement aeWndByTitle in windowCollectionByProperties .Where(aeWndByTitle => aeWndByTitle != null && (int)aeWndByTitle.GetCurrent().ProcessId > 0)) { resultCollection.Add(aeWndByTitle); } windowCollectionByProperties.Clear(); } else { IUiElement tempElement = null; // one more attempt using the FindWindow function IntPtr wndHandle = NativeMethods.FindWindowByCaption(IntPtr.Zero, windowTitle); // 20141001 // if (wndHandle != null && wndHandle != IntPtr.Zero) { if (wndHandle != IntPtr.Zero) { tempElement = UiElement.FromHandle(wndHandle); } if (null == tempElement || (int)tempElement.GetCurrent().ProcessId <= 0) { continue; } resultCollection.Add(tempElement); } } catch {} // 20140122 // AutomationFactory.DisposeChildKernel(); // 20140131 // if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count) { // foreach (IUiElement element in windowCollectionByProperties) { // element.Dispose(); // } // } // if (null != windowCollection && 0 < windowCollection.Count) { // foreach (IUiElement element in windowCollection) { // element.Dispose(); // } // } } return(resultCollection); }
List <IUiElement> GetWindowCollectionFromProcess( WindowSearcherData data) { data.ProcessIds = (from process in data.InputObject where null != process && 0 != process.Id select process.Id).ToList().ToArray(); return(GetWindowCollectionByPid(UiElement.RootElement, data)); }
internal List<IUiElement> GetWindowCollectionByPid( IUiElement rootElement, WindowSearcherData data) { classic.AndCondition conditionsForRecurseSearch = null; var elementsByProcessId = new List<IUiElement>(); // 20141001 // if ((null != data.Name && 0 < data.Name.Count()) || if ((null != data.Name && data.Name.Any()) || !string.IsNullOrEmpty(data.AutomationId) || !string.IsNullOrEmpty(data.Class)) { data.Recurse = true; } var conditionWindowPaneMenu = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Pane), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Menu)); foreach (int processId in data.ProcessIds) { conditionsForRecurseSearch = new classic.AndCondition( new classic.PropertyCondition( classic.AutomationElement.ProcessIdProperty, processId), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window)); var conditionsProcessId = new classic.AndCondition( new classic.PropertyCondition( classic.AutomationElement.ProcessIdProperty, processId), conditionWindowPaneMenu); try { if (data.Recurse) { if (data.First) { IUiElement rootWindowElement = rootElement.FindFirst( classic.TreeScope.Children, conditionsProcessId); if (null != rootWindowElement) { elementsByProcessId.Add(rootWindowElement); } } else { IUiEltCollection rootCollection = rootElement.FindAll( classic.TreeScope.Children, conditionsProcessId); if (null != rootCollection && 0 < rootCollection.Count) { elementsByProcessId.AddRange(rootCollection.Cast<IUiElement>()); } } } else { if (data.First) { IUiElement tempElement = rootElement.FindFirst( classic.TreeScope.Children, conditionsProcessId); if (null != tempElement) { elementsByProcessId.Add(tempElement); } } else { IUiEltCollection tempCollection = rootElement.FindAll( classic.TreeScope.Children, conditionsProcessId); if (null != tempCollection && 0 < tempCollection.Count) { elementsByProcessId.AddRange(tempCollection.Cast<IUiElement>()); } } } } catch (Exception) { // WriteVerbose( // this, // "exception: " + // eGetFirstChildOfRootByProcessId.Message); } } if (!data.Recurse || // 20141001 // ((null == data.Name || 0 == data.Name.Count()) && string.IsNullOrEmpty(data.AutomationId) && ((null == data.Name || !data.Name.Any()) && string.IsNullOrEmpty(data.AutomationId) && string.IsNullOrEmpty(data.Class))) return elementsByProcessId; var resultList = new List<IUiElement>(); /* List<IUiElement> resultList = new List<IUiElement>(); */ // 20141001 // if (null != data.Name && 0 < data.Name.Count()) { if (null != data.Name && data.Name.Any()) { foreach (string name in data.Name) { var controlSearcherData = new ControlSearcherData { Name = name, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, ControlType = new string[]{ "Window" } }; resultList.AddRange( ReturnOnlyRightElements( elementsByProcessId, controlSearcherData, false, true)); } } else { var controlSearcherData = new ControlSearcherData { Name = string.Empty, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, ControlType = new string[]{ "Window" } }; resultList.AddRange( ReturnOnlyRightElements( elementsByProcessId, controlSearcherData, false, true)); } elementsByProcessId = resultList; // 20140121 // never ! // if (null != resultList) { // resultList.Clear(); // resultList = null; // } return elementsByProcessId; }
// 20141001 List<IUiElement> GetWindowCollectionByProcessName( // IUiElement rootElement, WindowSearcherData data) { data.InputObject = data.ProcessNames.SelectMany(processName => Process.GetProcessesByName(processName)).ToList().ToArray(); return GetWindowCollectionFromProcess(data); }
List<IUiElement> GetWindowCollectionByName( IUiElement rootElement, WindowSearcherData data) { var windowCollectionByProperties = new List<IUiElement>(); var resultCollection = new List<IUiElement>(); if (null == data.Name) { // 20141001 // data.Name = new string[]{ string.Empty }; data.Name = new []{ string.Empty }; } classic.OrCondition conditionsSet = null; if (data.Recurse) { conditionsSet = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), classic.Condition.FalseCondition); } else { conditionsSet = new classic.OrCondition( new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Window), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Pane), new classic.PropertyCondition( classic.AutomationElement.ControlTypeProperty, classic.ControlType.Menu)); } foreach (string windowTitle in data.Name) { IUiEltCollection windowCollection = rootElement.FindAll(data.Recurse ? classic.TreeScope.Descendants : classic.TreeScope.Children, conditionsSet); var controlSearcherData = new ControlSearcherData { Name = windowTitle, AutomationId = data.AutomationId, Class = data.Class, Value = string.Empty, // 20141001 // ControlType = (new string[]{ "Window", "Pane", "Menu" }) ControlType = (new []{ "Window", "Pane", "Menu" }) }; windowCollectionByProperties = ReturnOnlyRightElements( windowCollection, controlSearcherData, false, true); try { if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count) { foreach (IUiElement aeWndByTitle in windowCollectionByProperties .Where(aeWndByTitle => aeWndByTitle != null && (int)aeWndByTitle.GetCurrent().ProcessId > 0)) { resultCollection.Add(aeWndByTitle); } windowCollectionByProperties.Clear(); } else { IUiElement tempElement = null; // one more attempt using the FindWindow function IntPtr wndHandle = NativeMethods.FindWindowByCaption(IntPtr.Zero, windowTitle); // 20141001 // if (wndHandle != null && wndHandle != IntPtr.Zero) { if (wndHandle != IntPtr.Zero) { tempElement = UiElement.FromHandle(wndHandle); } if (null == tempElement || (int) tempElement.GetCurrent().ProcessId <= 0) continue; resultCollection.Add(tempElement); } } catch {} // 20140122 // AutomationFactory.DisposeChildKernel(); // 20140131 // if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count) { // foreach (IUiElement element in windowCollectionByProperties) { // element.Dispose(); // } // } // if (null != windowCollection && 0 < windowCollection.Count) { // foreach (IUiElement element in windowCollection) { // element.Dispose(); // } // } } return resultCollection; }
List<IUiElement> GetWindowCollectionFromProcess( WindowSearcherData data) { data.ProcessIds = (from process in data.InputObject where null != process && 0 != process.Id select process.Id).ToList().ToArray(); return GetWindowCollectionByPid(UiElement.RootElement, data); }
List<IUiElement> GetWindowCollectionViaWin32( WindowSearcherData data) { return UiaHelper.EnumChildWindowsFromHandle( data.Name, data.AutomationId, data.Class, IntPtr.Zero); }