/// <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 test2() { Application app = Application.Attach("CnEport.SuperPass.UIModule.Eport"); Window window = app.GetWindow(SearchCriteria.ByAutomationId("AppForm"), InitializeOption.NoCache); SearchCriteria searchGName = SearchCriteria.ByClassName("WindowsForms10.EDIT.app.0.202c666"); searchGName.AndControlType(typeof(TextBox), WindowsFramework.WinForms); //searchGName.AndAutomationId("GName"); //searchGName.AndByText("规格型号"); StringBuilder sb = new StringBuilder(); foreach (TextBox a in window.GetMultiple(searchGName)) { sb.AppendFormat("name:{0} id:{1} helpText:{2}\n", a.Name, a.Id, a.HelpText); if (a.HelpText.IndexOf("规格型号") > -1) { a.Text = "testtest"; } } System.IO.File.WriteAllText("textbox.txt", sb.ToString()); }
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(); }
public static T CustomGet <T>(this IUIItem item, SearchCriteria searchCriteria = null, TimeSpan?retryInterval = null, TimeSpan?timeout = null, bool throwsException = true) where T : IUIItem { searchCriteria = searchCriteria ?? SearchCriteria.All; timeout = timeout ?? TimeSpan.FromMilliseconds(DefaultTimeoutMs); retryInterval = retryInterval ?? TimeSpan.FromMilliseconds(DefaultRetryIntervalMs); DateTime startTime = DateTime.Now; searchCriteria = searchCriteria.AndControlType(typeof(T), item.Framework); UIItemContainer container = new UIItemContainer(item.AutomationElement, item.ActionListener); while (true) { try { IUIItem element = container.Get(searchCriteria, TimeSpan.Zero); if (element == null) { throw new Exception("Null element found while getting " + searchCriteria); } return((T)element); } catch (AutomationException) { TimeSpan elapsedTime = DateTime.Now.Subtract(startTime); bool timeoutOccurred = elapsedTime > timeout; if (timeoutOccurred) { break; } Thread.Sleep((TimeSpan)retryInterval); // gives the automation time to refresh UIAutomation tree } } Assert.False(throwsException, $"Failed to get {searchCriteria} in {container}."); return(default(T)); }
/// <summary> /// Check if any UI Item of a certain Type exists /// </summary> /// <typeparam name="T">Type of Item</typeparam> /// <param name="uiItem">The root UI Item from were to start searching</param> /// <param name="searchCriteria">The Search Criteria to use for the Search</param> /// <returns><c>true</c> if there is any; otherwise, <c>false</c>.</returns> public static bool Exists <T>(this IUIItem uiItem, SearchCriteria searchCriteria) where T : IUIItem { return(GetUiItemContainer(uiItem).Exists(searchCriteria.AndControlType(typeof(T), uiItem.Framework))); }
public virtual T Get <T>(SearchCriteria searchCriteria, ActionListener actionListener, UIItemFactory factory) where T : UIItem { return((T)Get(searchCriteria.AndControlType(typeof(T)), actionListener, factory)); }
/// <summary> /// Finds UIItem which matches specified type and searchCriteria. Type supplied need not be supplied again in SearchCondition. /// e.g. in Get<Button>(SearchCriteria.ByAutomationId("OK").ByControlType(typeof(Button)).Indexed(1) the ByControlType(typeof(Button)) part /// is redundant. Look at documentation of SearchCriteria for details on it. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="searchCriteria">Criteria provided to search UIItem</param> /// <returns>First items matching the type and criteria</returns> public virtual T Get <T>(SearchCriteria searchCriteria) where T : UIItem { return((T)Get(searchCriteria.AndControlType(typeof(T), Framework))); }
public virtual bool Exists <T>(SearchCriteria searchCriteria) where T : IUIItem { return(Exists(searchCriteria.AndControlType(typeof(T), Framework))); }
public virtual T Get <T>(SearchCriteria searchCriteria, IActionListener actionListener, IUIItemFactory factory) where T : UIItem { return((T)Get(searchCriteria.AndControlType(typeof(T), WindowsFramework.None), actionListener, factory)); }
/// <summary> /// Implements <see cref="IUIItemContainer.GetMultiple{T}(SearchCriteria)" /> /// </summary> public virtual T[] GetMultiple <T>(SearchCriteria searchCriteria) where T : IUIItem { var items = GetMultiple(searchCriteria.AndControlType(typeof(T), Framework)); return(items.Select(item => (T)item).Where(cast => cast != null).ToArray()); }
public virtual T Get <T>(SearchCriteria searchCriteria, ActionListener actionListener, UIItemFactory factory) where T : UIItem { return((T)Get(searchCriteria.AndControlType(typeof(T), new WindowsFramework(Constants.MissingFrameworkId)), actionListener, factory)); }
public static T Get <T>(this UIItem uiItem, SearchCriteria searchCriteria) where T : UIItem { UIItemContainer uiItemContainer = GetUiItemContainer(uiItem); return((T)uiItemContainer.Get(searchCriteria.AndControlType(typeof(T)))); }