/// <summary> /// given a criteria, find a control within a window /// </summary> /// <param name="window">the containing window</param> /// <param name="criteria">the criteria to find the control</param> /// <returns>the found control. null - if not found</returns> public IUIItem FindControl(Window window, Dictionary <string, string> criteria) { // the "all" condition SearchCriteria crit = SearchCriteria.All; // for each criteria, AND with a new condition foreach (string key in criteria.Keys) { switch (key.ToLower()) { case Constants.PropertyNames.ControlType: crit = crit.AndControlType(GetTypeByName(criteria[key])); break; case Constants.PropertyNames.AutomationId: crit = crit.AndAutomationId(criteria[key]); break; case Constants.PropertyNames.Text: crit = crit.AndByText(criteria[key]); break; default: return(null); } ; } // search for control with 'crit' IUIItem item = window.Get(crit, WaitTime); // return the found control return(item); }
static void test1() { ProcessStartInfo process = new ProcessStartInfo(@"WindowsFormsApplication1.exe"); Application app = Application.AttachOrLaunch(process); Window window = app.GetWindow("Form1", InitializeOption.NoCache); SearchCriteria textSearch = SearchCriteria.ByClassName("WindowsForms10.EDIT.app.0.2bf8098_r16_ad1"); textSearch.AndControlType(typeof(TextBox), WindowsFramework.WinForms); textSearch.AndAutomationId("textbox1"); TextBox textBox1 = window.Get <TextBox>(); textBox1.Text = "hello qblong"; SearchCriteria btnSearch = SearchCriteria.ByClassName("WindowsForms10.BUTTON.app.0.2bf8098_r16_ad1"); btnSearch.AndControlType(typeof(Button), WindowsFramework.WinForms); btnSearch.AndAutomationId("button1"); Button button = window.Get <Button>(btnSearch); button.Click(); }
/* Class internal helper methods */ private SearchCriteria GetBy(String id) { System.Threading.Thread.Sleep(waitAfterAction); SearchCriteria sCrit = null; String by = ""; int x = id.IndexOf(SEPARATOR); if (x > 0 && locators.Contains(id.Substring(0, x))) { by = id.Substring(0, x); id = id.Substring(x + 1); } if (by.Equals("automationId")) { sCrit = SearchCriteria.ByAutomationId(id); } else if (by.Equals("className")) { sCrit = SearchCriteria.ByClassName(id); } else if (by.Equals("controlType")) { sCrit = SearchCriteria.ByControlType(GetControlTypes(id)); } else if (by.Equals("text")) { sCrit = SearchCriteria.ByText(id); } else if (by.Equals("xpath")) { int startIndex = 2; int indexx = 0; String element = id.Substring(startIndex, (id.IndexOf('[') - startIndex)).Trim(); sCrit = SearchCriteria.ByControlType(GetControlTypes(element)); while ((startIndex = id.IndexOf("[@")) >= 0) { id = id.Substring(startIndex + 2); startIndex = id.IndexOf("='"); String attribute = id.Substring(0, startIndex).Trim(); String param = id.Substring(startIndex + 2, (id.IndexOf("']") - (startIndex + 2))); if (attribute.Equals("automationId")) { sCrit = sCrit.AndAutomationId(param); } else if (attribute.Equals("className")) { sCrit = sCrit.AndByClassName(param); } else if (attribute.Equals("text")) { sCrit = sCrit.AndByText(param); } } if ((startIndex = id.IndexOf('[')) >= 0) { indexx = Convert.ToInt16(id.Substring(startIndex + 1, id.LastIndexOf(']') - (startIndex + 1)).Trim()); } if (indexx > 0) { sCrit = sCrit.AndIndex(indexx - 1); } } else { sCrit = SearchCriteria.ByText(id); } return(sCrit); }
public override void Apply(SearchCriteria searchCriteria) { searchCriteria.AndAutomationId(name); }
/// <summary> /// given a criteria, find a control within a window /// </summary> /// <param name="window">the containing window</param> /// <param name="criteria">the criteria to find the control</param> /// <returns>the found control. null - if not found</returns> public IUIItem FindControl(Window window, Dictionary <string, string> criteria) { // the "all" condition SearchCriteria crit = SearchCriteria.All; // for each criteria, AND with a new condition foreach (string key in criteria.Keys) { switch (key.ToLower()) { //case Constants.PropertyNames.ControlType: // crit = crit.AndControlType(GetTypeByName(criteria[key])); // break; case Constants.PropertyNames.AutomationId: crit = crit.AndAutomationId(criteria[key]); break; case Constants.PropertyNames.Text: crit = crit.AndByText(criteria[key]); break; case Constants.PropertyNames.ClassName: crit = crit.AndByClassName(criteria[key]); break; case Constants.PropertyNames.Index: crit = crit.AndIndex(int.Parse(criteria[key])); break; default: { bool bNativeFound = false; AutomationProperty[] props = window.AutomationElement.GetSupportedProperties(); foreach (AutomationProperty prop in props) { string propName = prop.ProgrammaticName.Substring(prop.ProgrammaticName.IndexOf('.') + 1); propName = propName.Substring(0, propName.IndexOf("Property")); if (propName.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { crit.AndNativeProperty(prop, criteria[key]); bNativeFound = true; break; } } if (bNativeFound) { break; } ; } return(null); } ; } try { // search for control with 'crit' IUIItem item = window.Get(crit, WaitTime); // return the found control return(item); } catch (Exception) { return(null); } }