protected ArrayList getControl(GetControlCmdletBase cmdlet) { try { aeCtrl = new ArrayList(); System.Windows.Automation.AndCondition conditions = null; // 20130221 System.Windows.Automation.AndCondition conditionsForWildCards = null; // 20130221 System.Windows.Automation.AndCondition conditionsForTextSearch = null; // 20130221 //conditions = this.getControlConditions(cmdlet, cmdlet.ControlType, ((GetControlCmdletBase)cmdlet).CaseSensitive, true) as AndCondition; // 20130221 // // display conditions for a regular search // this.WriteVerbose(cmdlet, "these conditions are used for an exact search:"); // displayConditions(cmdlet, conditions, "for exact search"); // additional search conditions if the search works with wildcards // only ControlType is here since that // Name, AutomationID, ClassName can be wildcarded // 20120918 // System.Windows.Automation.ControlType ctrlTypeForWildCards = // UIAHelper.GetControlTypeByTypeName(cmdlet.ControlType); GetControlCmdletBase tempCmdlet = new GetControlCmdletBase(); tempCmdlet.ControlType = cmdlet.ControlType; // 20130128 bool notTextSearch = true; if (null != cmdlet.ContainsText && string.Empty != cmdlet.ContainsText) { tempCmdlet.ContainsText = cmdlet.ContainsText; notTextSearch = false; // 20130221 conditionsForTextSearch = this.getControlConditions( tempCmdlet, tempCmdlet.ControlType, cmdlet.CaseSensitive, false) as AndCondition; // display conditions for text search this.WriteVerbose(cmdlet, "these conditions are used for text search:"); displayConditions(cmdlet, conditionsForTextSearch, "for text search"); } else { // 20130221 conditions = this.getControlConditions(cmdlet, cmdlet.ControlType, ((GetControlCmdletBase)cmdlet).CaseSensitive, true) as AndCondition; // display conditions for a regular search this.WriteVerbose(cmdlet, "these conditions are used for an exact search:"); displayConditions(cmdlet, conditions, "for exact search"); conditionsForWildCards = this.getControlConditions(tempCmdlet, tempCmdlet.ControlType, ((GetControlCmdletBase)cmdlet).CaseSensitive, true) as AndCondition; // display conditions for wildcard search this.WriteVerbose(cmdlet, "these conditions are used for wildcard search:"); displayConditions(cmdlet, conditionsForWildCards, "for wildcard search"); } // 20130221 //System.Windows.Automation.AndCondition conditionsForWildCards = // this.getControlConditions(tempCmdlet, tempCmdlet.ControlType, ((GetControlCmdletBase)cmdlet).CaseSensitive, true) as AndCondition; // 20130221 //System.Windows.Automation.AndCondition conditionsForTextSearch = // this.getControlConditions( // tempCmdlet, // tempCmdlet.ControlType, // cmdlet.CaseSensitive, // false) as AndCondition; // 20130221 // // display conditions for wildcard search // this.WriteVerbose(cmdlet, "these conditions are used for wildcard search:"); // displayConditions(cmdlet, conditionsForWildCards, "for wildcard search"); // 20130221 // // display conditions for text search // this.WriteVerbose(cmdlet, "these conditions are used for text search:"); // displayConditions(cmdlet, conditionsForTextSearch, "for text search"); // 20120823 foreach (AutomationElement inputObject in cmdlet.InputObject) { int processId = 0; do { if (inputObject != null && (int)inputObject.Current.ProcessId > 0) { this.WriteVerbose(cmdlet, "CommonCmdletBase: getControl(cmdlet)"); this.WriteVerbose(cmdlet, "cmdlet.InputObject != null"); processId = inputObject.Current.ProcessId; } // 20130204 // don't change the order! (text->exact->wildcard->win32 to win32->text->exact->wildcard) #region text search if (0 == aeCtrl.Count) { if (!notTextSearch && !cmdlet.Win32) { SearchByTextViaUIA(cmdlet, inputObject, conditionsForTextSearch); } } #endregion text search #region text search Win32 if (0 == aeCtrl.Count) { if (!notTextSearch && cmdlet.Win32) { SearchByTextViaWin32(cmdlet, inputObject, cmdlet.ControlType); } } #endregion text search Win32 #region exact search if (0 == aeCtrl.Count && notTextSearch) { if (!Preferences.DisableExactSearch && !cmdlet.Win32 ) { SearchByExactConditionsViaUIA(cmdlet, inputObject, conditions); } } #endregion exact search #region wildcard search if (0 == aeCtrl.Count && notTextSearch) { if (!Preferences.DisableWildCardSearch && !cmdlet.Win32) { //SearchByWildcardViaUIA(cmdlet, inputObject, conditionsForWildCards); SearchByWildcardViaUIA(cmdlet, ref aeCtrl, inputObject, cmdlet.Name, cmdlet.AutomationId, cmdlet.Class, cmdlet.Value, conditionsForWildCards); } } #endregion wildcard search #region Win32 search if (0 == aeCtrl.Count && notTextSearch) { // && //(null == cmdlet.AutomationId || string.Empty == cmdlet.AutomationId) && //(null == cmdlet.Class || string.Empty == cmdlet.Class)) { if (!Preferences.DisableWin32Search || cmdlet.Win32) { SearchByWildcardViaWin32(cmdlet, inputObject); } // if (! Preferences.DisableWin32Search) } // FindWindowEx #endregion Win32 search if (null != aeCtrl && aeCtrl.Count > 0) { break; } cmdlet.WriteVerbose(cmdlet, "going to sleep 99999999999"); SleepAndRunScriptBlocks(cmdlet); // System.Threading.Thread.Sleep(Preferences.SleepInterval); ////impossible due to inheritance and the absense of scriptblock here // SleepAndRunScriptBlocks(cmdlet); System.DateTime nowDate = System.DateTime.Now; try { WriteVerbose(cmdlet, "control type: '" + cmdlet.ControlType + "' , name: '" + cmdlet.Name + "', automationId: '" + cmdlet.AutomationId + "', class: '" + cmdlet.Class + "' , value: '" + cmdlet.Value + "' , seconds: " + ((nowDate - startDate).TotalSeconds).ToString()); } catch { //(Exception eWriteVerbose) { //WriteVerbose(this, eWriteVerbose.Message); } if ((nowDate - startDate).TotalSeconds > cmdlet.Timeout / 1000) { if (null == aeCtrl || 0 == aeCtrl.Count) { return null; } break; } else{ rootElement = System.Windows.Automation.AutomationElement.RootElement; if (processId > 0) { try { System.Windows.Automation.PropertyCondition pIDcondition = new System.Windows.Automation.PropertyCondition( System.Windows.Automation.AutomationElement.ProcessIdProperty, processId); AutomationElement tempElement = rootElement.FindFirst(System.Windows.Automation.TreeScope.Children, pIDcondition); if (tempElement != null && (int)tempElement.Current.ProcessId > 0) { } else { // 20120830 (the new style of writing errors) this.WriteError( cmdlet, "The input control or window has been lost", "ObjectOrWindowLost", ErrorCategory.ObjectNotFound, true); return null; } } catch {//"process is gone" // get new window } } else { this.WriteVerbose(cmdlet, "failed to get the process Id"); // 20120830 //return null; // 20120830 (the new style of writing errors) this.WriteError( cmdlet, "The input control or window has been lost", "ObjectOrWindowLost", ErrorCategory.ObjectNotFound, true); return null; } //#describe the output } //} // 20120823 } while (cmdlet.Wait); } // 20120823 #region commented // 20120824 //if (aeCtrl != null && (int)aeCtrl.Current.ProcessId > 0) // 20120830 //if (null != aeCtrl && (int)((AutomationElement)aeCtrl[0]).Current.ProcessId > 0) { //{ // if (null != aeCtrl && 0 < aeCtrl.Count) { // WriteVerbose(cmdlet, aeCtrl); // } // // // // 20120917 // if (null == aeCtrl) { // cmdlet.WriteVerbose(cmdlet, "aeCtrl == null"); // } // if (0 == aeCtrl.Count) { // cmdlet.WriteVerbose(cmdlet, "aeCtrl.Count == 0"); // } #endregion commented return aeCtrl; #region commented } catch (Exception eGetControlException) { this.WriteError( cmdlet, "Failed to get the control." + eGetControlException.Message, "UnableToGetControl", ErrorCategory.InvalidResult, true); return null; } // catch { // this.WriteError( // cmdlet, // "Failed to get the control.", // "UnableToGetControl", // ErrorCategory.InvalidResult, // true); // // return null; // } #endregion commented }
protected void displayConditions( GetControlCmdletBase cmdlet, AndCondition conditions, string description) { try { Condition[] conds = conditions.GetConditions(); for (int i = 0; i < conds.Length; i++) { cmdlet.WriteVerbose(cmdlet, "<<<< displaying conditions '" + description + "' >>>>"); cmdlet.WriteVerbose(cmdlet, (conds[i] as PropertyCondition).Property.ProgrammaticName); cmdlet.WriteVerbose(cmdlet, (conds[i] as PropertyCondition).Value.ToString()); cmdlet.WriteVerbose(cmdlet, (conds[i] as PropertyCondition).Flags.ToString()); } } catch {} }
internal void SearchByExactConditionsViaUIA( GetControlCmdletBase cmdlet, AutomationElement inputObject, System.Windows.Automation.AndCondition conditions) { #region the -First story // 20120824 //aeCtrl = // 20120921 #region -First // if (cmdlet.First) { // AutomationElement tempFirstElement = // inputObject.FindFirst( // System.Windows.Automation.TreeScope.Descendants, // conditions); // if (null != tempFirstElement) { // if (null == cmdlet.SearchCriteria || 0 == cmdlet.SearchCriteria.Length) { // aeCtrl.Add(tempFirstElement); // } else { // if (testControlWithAllSearchCriteria( // cmdlet, // cmdlet.SearchCriteria, // tempFirstElement)) { // aeCtrl.Add(tempFirstElement); // } // } // } // } else { #endregion -First // 20120823 //cmdlet.InputObject.FindFirst(System.Windows.Automation.TreeScope.Descendants, // 20120824 // 20120917 #region -First // } #endregion -First //else if (UIAutomation.CurrentData.LastResult #endregion the -First story //internal void SearchByExactConditionsViaUIA(System.Windows.Automation.AndCondition conditions, ref bool notTextSearch, ref System.Windows.Automation.AndCondition conditionsForWildCards, ref AutomationElement inputObject, ref int processId, GetControlCmdletBase cmdlet) //{ if (conditions != null) { if (inputObject != null && (int)inputObject.Current.ProcessId > 0) { AutomationElementCollection tempCollection = inputObject.FindAll(System.Windows.Automation.TreeScope.Descendants, conditions); foreach (AutomationElement tempElement in tempCollection) { if (null == cmdlet.SearchCriteria || 0 == cmdlet.SearchCriteria.Length) { aeCtrl.Add(tempElement); cmdlet.WriteVerbose(cmdlet, "ExactSearch: element added to the result collection"); } else { cmdlet.WriteVerbose(cmdlet, "ExactSearch: checking search criteria"); if (testControlWithAllSearchCriteria(cmdlet, cmdlet.SearchCriteria, tempElement)) { cmdlet.WriteVerbose(cmdlet, "ExactSearch: the control matches the search criteria"); aeCtrl.Add(tempElement); cmdlet.WriteVerbose(cmdlet, "ExactSearch: element added to the result collection"); } } } } } }
internal void SearchByWildcardViaWin32(GetControlCmdletBase cmdlet, AutomationElement inputObject) { this.WriteVerbose(cmdlet, "[getting the control] using FindWindowEx"); ArrayList tempListWin32 = new ArrayList(); if (null != cmdlet.Name && string.Empty != cmdlet.Name) { this.WriteVerbose(cmdlet, "collecting controls by name (Win32)"); tempListWin32.AddRange(UIAHelper.GetControlByName(cmdlet, inputObject, cmdlet.Name)); } if (null != cmdlet.Value && string.Empty != cmdlet.Value) { this.WriteVerbose(cmdlet, "collecting controls by value (Win32)"); tempListWin32.AddRange(UIAHelper.GetControlByName(cmdlet, inputObject, cmdlet.Value)); } foreach (AutomationElement tempElement3 in tempListWin32) { if (null != cmdlet.ControlType && 0 < cmdlet.ControlType.Length) { if (!tempElement3.Current.ControlType.ProgrammaticName.ToUpper().Contains(cmdlet.ControlType.ToUpper()) || !(tempElement3.Current.ControlType.ProgrammaticName.ToUpper().Substring(12).Length == cmdlet.ControlType.ToUpper().Length)) { continue; } } if (null == cmdlet.SearchCriteria || 0 == cmdlet.SearchCriteria.Length) { aeCtrl.Add(tempElement3); cmdlet.WriteVerbose(cmdlet, "Win32Search: element added to the result collection"); } else { cmdlet.WriteVerbose(cmdlet, "Win32Search: checking search criteria"); if (testControlWithAllSearchCriteria(cmdlet, cmdlet.SearchCriteria, tempElement3)) { cmdlet.WriteVerbose(cmdlet, "Win32Search: the control matches the search criteria"); aeCtrl.Add(tempElement3); cmdlet.WriteVerbose(cmdlet, "Win32Search: element added to the result collection"); } } } }
internal void SearchByWildcardViaUIA( //GetCmdletBase cmdlet, GetControlCmdletBase cmdlet, // 20130318 // ?? ref ArrayList resultCollection, AutomationElement inputObject, string name, string automationId, string className, string strValue, System.Windows.Automation.AndCondition conditionsForWildCards) { this.WriteVerbose((cmdlet as PSTestLib.PSCmdletBase), "[getting the control] using WildCard search"); try { // 20130220 GetControlCollectionCmdletBase cmdlet1 = new GetControlCollectionCmdletBase( //cmdlet.InputObject, null != cmdlet.InputObject ? cmdlet.InputObject : (new AutomationElement[]{ AutomationElement.RootElement }), //null, name, //cmdlet.Name, automationId, //cmdlet.AutomationId, className, //cmdlet.Class, //cmdlet.Value, //string.Empty != cmdlet.Value ? cmdlet.Value : null, strValue, // 20130318 //(new string[] {}), //null != cmdlet.controlt //string.Empty != cmdlet.ControlType ? cmdlet.ControlType : (new string[] {}), //null != cmdlet.ControlType ? cmdlet.ControlType : (new string[] {}), //null != cmdlet.ControlType ? cmdlet.ControlType : string.Empty, //string.Empty != cmdlet.ControlType ? cmdlet.ControlType : string.Empty, null != cmdlet.ControlType ? (new string[] {cmdlet.ControlType}) : (new string[] {}), this.caseSensitive); try { this.WriteVerbose((cmdlet as PSTestLib.PSCmdletBase), "using the GetAutomationElementsViaWildcards_FindAll method"); ArrayList tempList = cmdlet1.GetAutomationElementsViaWildcards_FindAll( cmdlet1, inputObject, conditionsForWildCards, cmdlet1.CaseSensitive, false, false); cmdlet.WriteVerbose( cmdlet, "there are " + tempList.Count.ToString() + " elements that match the conditions"); foreach (AutomationElement tempElement2 in tempList) { if (null == cmdlet.SearchCriteria || 0 == cmdlet.SearchCriteria.Length) { resultCollection.Add(tempElement2); cmdlet.WriteVerbose(cmdlet, "WildCardSearch: element added to the result collection (no SearchCriteria)"); } else { cmdlet.WriteVerbose(cmdlet, "WildCardSearch: checking search criteria"); if (TestControlWithAllSearchCriteria(cmdlet, cmdlet.SearchCriteria, tempElement2)) { cmdlet.WriteVerbose(cmdlet, "WildCardSearch: the control matches the search criteria"); resultCollection.Add(tempElement2); cmdlet.WriteVerbose(cmdlet, "WildCardSearch: element added to the result collection (SearchCriteria)"); } // cmdlet.WriteVerbose(cmdlet, "WildCardSearch: element added to the result collection (SearchCriteria) (2)"); } // cmdlet.WriteVerbose(cmdlet, "WildCardSearch: element added to the result collection (SearchCriteria) (3)"); } cmdlet.WriteVerbose(cmdlet, "WildCardSearch: element(s) added to the result collection: " + resultCollection.Count.ToString()); } catch (Exception eUnexpected) { // this.WriteVerbose(this, eUnexpected.Message); this.WriteError( this, "The input control or window has been possibly lost." + eUnexpected.Message, "UnexpectedError", ErrorCategory.ObjectNotFound, true); } } catch (Exception eWildCardSearch) { this.WriteError( cmdlet, "The input control or window has been possibly lost." + eWildCardSearch.Message, "UnexpectedError", ErrorCategory.ObjectNotFound, true); } }