/// <summary> /// Try to call the invoke pattern on the target control. /// </summary> /// <param name="control">The target control.</param> /// <returns>True if the operation succeed, false otherwise.</returns> public static bool TryInvoke(this WpfControl control) { try { var automationElement = control.NativeElement as AutomationElement; if (automationElement == null) { return(false); } object objectPattern; if (!automationElement.TryGetCurrentPattern(InvokePattern.Pattern, out objectPattern)) { return(false); } var pattern = objectPattern as InvokePattern; if (pattern != null) { pattern.Invoke(); } return(true); } catch { } return(false); }
/// <summary> /// RecordedMethod1 - Use 'RecordedMethod1Params' to pass parameters into this method. /// </summary> public void RecordedMethod1() { #region Variable Declarations WinEdit uIBillTextBoxEdit = this.UIForm1Window.UIBillTextBoxWindow.UIBillTextBoxEdit; WpfControl uIEnterTotalBillEdit = this.UIForm1Window.UIEnterTotalBillEdit; WinWindow uITipBoxWindow = this.UIForm1Window.UIForm1Client.UITipBoxWindow; WinEdit uITipBoxEdit = this.UIForm1Window.UITipBoxWindow.UITipBoxEdit; WinClient uIForm1Client = this.UIForm1Window.UIForm1Client; WinButton uIComputeTipButton = this.UIForm1Window.UIComputeTipWindow.UIComputeTipButton; #endregion // Launch '%USERPROFILE%\source\repos\u1190338\Labs\Lab6\Lab6\bin\Debug\Lab6.exe' ApplicationUnderTest uIForm1Window = ApplicationUnderTest.Launch(this.RecordedMethod1Params.UIForm1WindowExePath, this.RecordedMethod1Params.UIForm1WindowAlternateExePath); // Click 'BillTextBox' text box Mouse.Click(uIBillTextBoxEdit, new Point(61, 4)); // Double-Click 'BillTextBox' text box Mouse.DoubleClick(uIBillTextBoxEdit, new Point(61, 4)); // Type '100' in 'Enter Total Bill' text box Keyboard.SendKeys(uIEnterTotalBillEdit, this.RecordedMethod1Params.UIEnterTotalBillEditSendKeys, ModifierKeys.None); // Click 'TipBox' window Mouse.Click(uITipBoxWindow, new Point(32, 1)); // Type '20' in 'TipBox' text box uITipBoxEdit.Text = this.RecordedMethod1Params.UITipBoxEditText; // Click 'Form1' client Mouse.Click(uIForm1Client, new Point(301, 303)); // Click 'Compute Tip' button Mouse.Click(uIComputeTipButton, new Point(132, 22)); }
public static WpfMenuItem 菜单(this WpfControl context, string title = null) { context = context.Find <WpfMenu>("主菜单"); var path = title.SplitBy("."); if (path.Length > 1) { for (int i = 0, c = path.Length - 1; i < c; i++) { var pageTitle = path[i]; var menu = context.菜单(pageTitle); if (!menu.Expanded) { menu.Expanded = true; } context = menu; } } var mi = context.Find <WpfMenuItem>(path.Last()); if (!mi.Exists) { throw new UIAutomationException("没有找到菜单" + title); } return(mi); }
public void ClickOnCreateNewTestClass() { WpfControl installedDatagrid = new WpfControl(newprojectwindow); installedDatagrid.SearchProperties[WpfControl.PropertyNames.ControlType] = "DataItem"; installedDatagrid.SearchProperties[WpfControl.PropertyNames.AutomationId] = "Installed"; WpfExpander expand = new WpfExpander(installedDatagrid); WpfTree installedTreeView = new WpfTree(expand); installedTreeView.SearchProperties.Add(WpfTree.PropertyNames.AutomationId, "Installed"); WpfTreeItem tempTreeItem = new WpfTreeItem(installedTreeView); tempTreeItem.SearchProperties.Add(WpfTreeItem.PropertyNames.AutomationId, "Templates"); WpfTreeItem visualCtreeItem = new WpfTreeItem(tempTreeItem); visualCtreeItem.SearchProperties.Add(WpfTreeItem.PropertyNames.AutomationId, "Visual C#"); expand.Expanded = true; WpfTreeItem testTreeItem = new WpfTreeItem(visualCtreeItem); testTreeItem.SearchProperties.Add(WpfTreeItem.PropertyNames.AutomationId, "Test"); Mouse.Click(testTreeItem); }
/// <summary> /// Try to select the control using the SelectionItemPattern /// </summary> /// <param name="control">The target control.</param> /// <returns>True if the operation succeed, false otherwise.</returns> public static bool TrySelect(this WpfControl control) { try { AutomationElement automationElement = GetAutomationElement(control); if (automationElement == null) { return(false); } object objectPattern; if (!automationElement.TryGetCurrentPattern(SelectionItemPattern.Pattern, out objectPattern)) { return(false); } var pattern = objectPattern as SelectionItemPattern; if (pattern != null) { pattern.Select(); } return(true); } catch { } return(false); }
/// <summary> /// Try to collapse the control using the ExpandCollapsePattern /// </summary> /// <param name="control">The target control.</param> /// <returns>True if the operation succeed, false otherwise.</returns> public static bool TryCollapse(this WpfControl control) { try { var automationElement = control.NativeElement as AutomationElement; if (automationElement == null) { return(false); } object objectPattern; if (!automationElement.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out objectPattern)) { return(false); } var pattern = objectPattern as ExpandCollapsePattern; if (pattern != null && pattern.Current.ExpandCollapseState != ExpandCollapseState.Collapsed && pattern.Current.ExpandCollapseState != ExpandCollapseState.LeafNode) { pattern.Collapse(); } return(true); } catch { } return(false); }
/// <summary> /// Try to set the value to the control using the ValuePattern. /// </summary> /// <param name="valueToSet"></param> /// <param name="control">The target control.</param> /// <returns>True if the operation succeed, false otherwise.</returns> public static bool TrySetValue(this WpfControl control, string valueToSet) { try { var automationElement = control.NativeElement as AutomationElement; if (automationElement == null) { return(false); } object objectPattern; if (!automationElement.TryGetCurrentPattern(ValuePattern.Pattern, out objectPattern)) { return(false); } var pattern = objectPattern as ValuePattern; if (pattern != null) { pattern.SetValue(valueToSet); } return(true); } catch { } return(false); }
private static void DumpPropertyExpressions( WpfControl control, bool traceInsteadOfDebug, IEnumerable <PropertyExpression> propertyExpressionCollection) { var sb = new StringBuilder(); sb.AppendFormat("SearchProperties for {0}{1}", control, Environment.NewLine); foreach (PropertyExpression sp in propertyExpressionCollection) { sb.AppendFormat (" -> {0} {1} {2}{3}", sp.PropertyName, sp.PropertyOperator, sp.PropertyValue, Environment.NewLine); } if (traceInsteadOfDebug) { Trace.WriteLine(sb.ToString()); } else { Debug.WriteLine(sb.ToString()); } }
/// <summary> /// Dump to the output all the supported properties of a Wpf control. /// </summary> /// <param name="control">The target control.</param> /// <param name="traceInsteadOfDebug">Dump to the trace instead of the debug output.</param> /// <exception cref="ArgumentNullException">control</exception> public static void DumpSupportedProperties(this WpfControl control, bool traceInsteadOfDebug = false) { if (control == null) { throw new ArgumentNullException("control"); } var sb = new StringBuilder(); var element = control.NativeElement as AutomationElement; sb.AppendFormat("SupportedProperties for {0}{1}", control, Environment.NewLine); if (element != null) { foreach (AutomationProperty sp in element.GetSupportedProperties()) { sb.AppendFormat (" -> {0}: {1}{2}", sp.ProgrammaticName, element.GetCurrentPropertyValue(sp), Environment.NewLine); } } if (traceInsteadOfDebug) { Trace.WriteLine(sb.ToString()); } else { Debug.WriteLine(sb.ToString()); } }
public static WpfComboListControl 属性编辑器_下拉列表(this WpfControl context, string title = null) { return(new WpfComboListControl { Control = 属性编辑器 <WpfComboBox>(context, title) }); }
private static IEnhancedWpfControl WrapUtil(WpfControl control) { IEnhancedWpfControl _con = null; if (control.GetType() == typeof(WpfButton)) { _con = new EnhancedWpfButton(); } else if (control.GetType() == typeof(WpfCheckBox)) { _con = new EnhancedWpfCheckBox(); } else if (control.GetType() == typeof(WpfComboBox)) { _con = new EnhancedWpfComboBox(); } else if (control.GetType() == typeof(WpfEdit)) { _con = new EnhancedWpfEdit(); } else if (control.GetType() == typeof(WpfHyperlink)) { _con = new EnhancedWpfHyperlink(); } else if (control.GetType() == typeof(WpfList)) { _con = new EnhancedWpfList(); } else if (control.GetType() == typeof(WpfListItem)) { _con = new EnhancedWpfListItem(); } else if (control.GetType() == typeof(WpfRadioButton)) { _con = new EnhancedWpfRadioButton(); } else if (control.GetType() == typeof(WpfTable)) { _con = new EnhancedWpfTable(); } else if (control.GetType() == typeof(WpfCell)) { _con = new EnhancedWpfCell(); } else if (control.GetType() == typeof(WpfScrollBar)) { _con = new EnhancedWpfScrollBar(); } else if (control.GetType() == typeof(WpfCustom)) { _con = new EnhancedWpfCustom(); } else { throw new Exception(string.Format("WrapUtil: '{0}' not supported", control.GetType().Name)); } _con.WrapReady(control); return(_con); }
/// <summary> /// Dump to the output all the filter properties of a Wpf control. /// </summary> /// <param name="control">The target control.</param> /// <param name="traceInsteadOfDebug">Dump to the trace instead of the debug output.</param> /// <exception cref="ArgumentNullException">control</exception> public static void DumpFilterProperties(this WpfControl control, bool traceInsteadOfDebug = false) { if (control == null) { throw new ArgumentNullException("control"); } DumpPropertyExpressions(control, traceInsteadOfDebug, control.FilterProperties); }
public static WpfControl 双击(this WpfControl control) { control.EnsureClickable(); control.WaitForControlEnabled(); Mouse.DoubleClick(control); return(control); }
public static IDisposable 进入(WpfControl item) { 设置当前测试对象(item); return(new CurrentTestWrapper() { _item = item }); }
public static WpfTabPage 页签(this WpfControl context, string title = null) { var path = title.SplitBy("."); context = LocateParentTab(context, path); return(context.Find <WpfTabPage>(path.Last())); }
public void WpfListView_GetRow_CanClickOnRow() { WpfTable listView = mainWindow.Get <WpfTable>("AutomationId=lv1"); Assert.AreEqual(8, listView.RowCount); WpfControl <CUITControls.WpfControl> control = listView.Get <WpfControl <CUITControls.WpfControl> >("AutomationId=Item0"); control.Click(); }
/// <summary> /// Get all the children of a control of a given type. /// It can returns only direct children or all of them. /// </summary> /// <typeparam name="T">The aimed type of child.</typeparam> /// <param name="onlyDirectChildren">Do you want only direct children ?</param> /// <param name="control">The target control.</param> /// <returns>The children of the aimed type. An empty list if there is none.</returns> public static List <T> GetChildren <T>(this WpfControl control, bool onlyDirectChildren = true) where T : WpfControl { UITestControlCollection children = control.GetChildren(); var controls = new List <T>(); GetChildren(children, controls, onlyDirectChildren); return(controls); }
private Type GetControl(WpfControl control) { switch (control) { case WpfControl.Foo: return(typeof(FooControlType)); ... } }
public static WpfButton 点击按钮(this WpfControl context, string title) { Logger.LogLine("点击按钮:" + title); var btn = context.钮(title); btn.单击(); return(btn); }
public void WpfListView_GetRow_CanClickOnCell() { WpfTable listView = mainWindow.Get <WpfTable>("AutomationId=lv1"); Assert.AreEqual(8, listView.RowCount); WpfControl <CUITControls.WpfControl> control = listView.Get <WpfControl <CUITControls.WpfControl> >("AutomationId=Item0"); WpfCell cell = control.Get <WpfCell>("ColumnHeader=Content"); cell.Click(); }
private static UITestControlCollection GetNodes(WpfControl treeItem) { var item = treeItem as WpfTreeItem; if (item != null) { return(item.Nodes); } return((treeItem as WpfTree).Nodes); }
private static WpfControl LocateParentTab(WpfControl context, string[] path) { if (path.Length > 1) { for (int i = 0, c = path.Length - 1; i < c; i++) { var pageTitle = path[i]; context = context.页签(pageTitle); } } return(context); }
public static T Get <T>(this WpfControl container, Func <PropertyExpressionCollection> searchProperties) where T : WpfControl, new() { container.WaitForControlReady(); var foo = new T { Container = container, TechnologyName = "UIA" }; foo.SearchProperties.AddRange(searchProperties()); foo.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); foo.Find(); return(foo); }
public static IWpfControl CreateWpfControl(WpfControl control, string controlName) { IWpfControl wpfControl = default(IWpfControl); Type type = GetControl(control); if (type != null && type.IsAssignableFrom(typeof(IWpfControl))) { wpfControl = (IWpfControl)Activator.CreateInstance(type); } if (wpfControl != null) { wpfControl.Name = controlName ?? Consts.DefaultEaControlName; } return(wpfControl); }
public static IEnumerable <T> GetMultiple <T>(this WpfControl container, string automationId) where T : WpfControl, new() { container.WaitForControlReady(); var foo = new T { Container = container, SearchProperties = { { WpfControl.PropertyNames.AutomationId, automationId } }, TechnologyName = "UIA" }; foo.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); return(foo.FindMatchingControls().Cast <T>()); }
public static TControl CreateById <TControl>(string id, WpfControl context = null) where TControl : WpfControl, new() { var control = new TControl(); if (context != null) { control.Container = context; } if (!string.IsNullOrEmpty(id)) { control.SearchProperties[WpfControl.PropertyNames.AutomationId] = id; } return(control); }
/// <summary> /// Returns the control's direct children automation ids. /// </summary> /// <param name="control"></param> /// <returns>A list of all automation id. It may be empty.</returns> /// <exception cref="InvalidOperationException">Cannot retrieve the automation element.</exception> public static List <string> GetChildrenAutomationIds(this WpfControl control) { UITestControlCollection collection = control.GetChildren(); List <string> ids = collection.Select (c => { var automationElement = c.NativeElement as AutomationElement; if (automationElement == null) { throw new InvalidOperationException("Cannot retrieve the automation element."); } return(automationElement.Current.AutomationId); }).ToList(); return(ids); }
public static IEnumerable <T> GetMultipleByName <T>(this WpfControl container, string name, string technology = "MSAA") where T : WpfControl, new() { container.WaitForControlReady(); var foo = new T { Container = container, SearchProperties = { { UITestControl.PropertyNames.Name, name } }, TechnologyName = technology }; foo.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); return(foo.FindMatchingControls().Cast <T>()); }
public static T GetByName <T>(this WpfControl container, string name, string technology = "MSAA") where T : WpfControl, new() { container.WaitForControlReady(); var foo = new T { Container = container, SearchProperties = { { UITestControl.PropertyNames.Name, name } }, TechnologyName = technology }; foo.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); foo.Find(); return(foo); }
/// <summary> /// Try to set the text on a control. /// It uses the value pattern and if not available, simulate key press. /// More info at this acdress : http://msdn.microsoft.com/en-us/library/ms750582.aspx /// </summary> public static bool TrySetText(this WpfControl control, string valueToSet) { //TextPattern and ValuePattern cannnot be used on the same control // NOTE: Elements that support TextPattern // do not support ValuePattern and TextPattern // does not support setting the text of // multi-line edit or document controls. // For this reason, text input must be simulated // using one of the following methods. // if (!control.TrySetValue(valueToSet)) { control.SetFocus(); Keyboard.SendKeys("^{HOME}"); // Move to start of control Keyboard.SendKeys("^+{END}"); // Select everything Keyboard.SendKeys("{DEL}"); // Delete selection Keyboard.SendKeys(valueToSet); return(true); } return(false); }
/// <summary> /// Create a _Wpf* control based on the type of provided WpfControl. /// </summary> /// <param name="control"></param> /// <returns></returns> public static IEnhancedWpfControl Create(WpfControl control) { string Prefix = ".Enhanced"; string controlTypeName = control.GetType().Name; string Namespace = typeof(EnhancedWpfControlFactory).Namespace; // Get type based on WpfControl type and namespace Type Type = Type.GetType(Namespace + Prefix + controlTypeName); // The type will be null if it does not exist if (Type == null) { throw new Exception(string.Format("Control Type '{0}' not supported", control.GetType().Name)); } // Create control IEnhancedWpfControl enhancedControl = (IEnhancedWpfControl)Activator.CreateInstance(Type); // Wrap WinControl enhancedControl.WrapReady(control); return enhancedControl; }
/// <summary> /// /// </summary> /// <param name="item">当前测试对象</param> /// <param name="action">以 item 为环境运行的代码</param> /// <returns></returns> public static WpfControl 进入(WpfControl item, Action<WpfControl> action) { using (进入(item)) { action(item); } return item; }
public static IDisposable 进入(WpfControl item) { 设置当前测试对象(item); return new CurrentTestWrapper() { _item = item }; }
public static void 设置当前测试对象(WpfControl item) { TestContext.Current.ControlContexts.Push(item); LogLine("→进入→: " + item.FriendlyName); }
public static void SetUIAItemControlValue(string name, string type, string value, WinWindow parent) { WinWindow itemWindow = new WinWindow(parent); itemWindow.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "HwndWrapper", PropertyExpressionOperator.Contains)); itemWindow.WaitForControlReady(); WpfPane pane = new WpfPane(itemWindow); WpfCustom customControl = new WpfCustom(pane); customControl.SearchProperties[WpfControl.PropertyNames.ClassName] = "Uia.SegmentedEntry"; customControl.WaitForControlReady(); WpfControl uIControl = new WpfControl(customControl); uIControl.TechnologyName = "UIA"; uIControl.SearchProperties.Add("ControlType", type); uIControl.SearchProperties.Add("AutomationId", name); uIControl.WaitForControlReady(); if (type == "CheckBox") { WpfCheckBox mUICheckBox = new WpfCheckBox(uIControl); mUICheckBox.Checked = Convert.ToBoolean(value); } else if (type == "Edit") { WpfEdit mUIEdit = new WpfEdit(uIControl); mUIEdit.Text = value; } }
public static void SetUIAControlValue(string name, string type, string value, WinWindow parent) { WpfControl uIControl = new WpfControl(parent); uIControl.TechnologyName = "UIA"; uIControl.SearchProperties.Add("ControlType", type); uIControl.SearchProperties.Add("AutomationId", name); uIControl.WaitForControlReady(); if (type == "CheckBox") { WpfCheckBox mUICheckBox = new WpfCheckBox(uIControl); mUICheckBox.Checked = Convert.ToBoolean(value); } else if (type == "Edit") { WpfEdit mUIEdit = new WpfEdit(uIControl); mUIEdit.Text = value; } }
public static WpfControl GetUIAControl(string name, string type, WinWindow parent) { WpfControl uIControl = new WpfControl(parent); uIControl.TechnologyName = "UIA"; uIControl.SearchProperties.Add("ControlType", type); uIControl.SearchProperties.Add("AutomationId", name); uIControl.WaitForControlReady(); return uIControl; }