/// <summary> /// Finds the requested element from its Automation ID and sets text in it, /// throwing an assert if the element returned is null. /// </summary> /// <param name="parent">The parent under which the element we're searching for lives.</param> /// <param name="automationID">The automation ID to search for under the parent</param> public static void FindAndSetText(AutomationElement parent, string automationID, string text) { AutomationElement element = AutomationHelper.FindDescendantById(parent, automationID); TestServices.Assert(element != null, "Could not find the element with Automation ID '" + automationID + "'"); AutomationHelper.SetText(element, text); }
/// <summary> /// Will select all text of the textBox and replace it with the requested string. /// </summary> /// <param name="textBox"></param> /// <param name="newValue"></param> public static void ClearAndSetTextBoxValue( AutomationElement textBox, string newValue ) { textBox.SetFocus(); AutomationHelper.InputKeyGesture(new KeyGesture(Key.A, ModifierKeys.Control)); AutomationHelper.InputTextString(newValue); }
/// <summary> /// Will set the requested string as the text of the textBox, and then simulate /// the requested keypress. /// </summary> /// <param name="textBox"></param> /// <param name="newValue"></param> /// <param name="inputKey"></param> public static void SetNewTextBoxValue( AutomationElement textBox, string newValue, Key inputKey ) { ClearAndSetTextBoxValue(textBox, newValue); AutomationHelper.InputKey(inputKey); }
/// <summary> /// Finds the requested element from its Automation ID and invokes it, /// throwing an assert if the element returned is null. /// </summary> /// <param name="parent">The parent under which the element we're searching for lives.</param> /// <param name="automationID">The automation ID to search for under the parent</param> public static void FindAndInvoke(AutomationElement parent, string automationID) { //Find the element AutomationElement element = AutomationHelper.FindDescendantById(parent, automationID); TestServices.Assert(element != null, "Could not find the element with Automation ID '" + automationID + "'"); //Invoke the element AutomationHelper.Invoke(element); }
/// <summary> /// Will press the key, and verify that the requested element is focused. /// </summary> /// <param name="key"></param> /// <param name="element"></param> public static void PressKeyCheckFocus( Key key, AutomationElement newElement) { AutomationHelper.InputKey(key); // Wait for new element to take focus. WaitForFocus(newElement); // Check if the new element was correctly assigned focus. TestServices.Assert(newElement.Current.HasKeyboardFocus, "Key {0} failed; {1} should have focus, but {2} does.", key.ToString(), newElement.Current.AutomationId, AutomationElement.FocusedElement.Current.AutomationId); }
/// <summary> /// Similar to FindAndInvoke, with the difference that this method /// performs a "manual" (non-input pattern) invocation of the element to work around /// automation issues with modal dialogs. /// </summary> /// <param name="parent">The parent under which the element we're searching for lives.</param> /// <param name="automationID">The automation ID to search for under the parent</param> /// <param name="failSilently">If true, we will simply return if the element can't be /// found or can't be focused. If false, we throw an exception.</param> public static void FindAndManualInvoke( AutomationElement parent, string automationID, bool failSilently) { //We do the invoke differently here due to an unfortunate issue where //Invoke()ing a Winforms Button that brings up a Modal Dialog //will not return until the Modal Dialog is closed. //This obviously would prevent this test from continuing on past that point. //What we're doing to work around the issue is to put focus on the button and invoke //an Enter keypress to bring up the dialog. AutomationElement element = AutomationHelper.FindDescendantById( parent, automationID, !failSilently); if (failSilently && (element == null)) { TestServices.Log("warning - FindAndManualInvoke could not locate " + automationID); return; } TestServices.Assert(element != null, "Could not find the element with Automation ID '" + automationID + "'"); //Focus the element try { element.SetFocus(); } catch (InvalidOperationException) { if (failSilently) { TestServices.Log("warning - FindAndManualInvoke could not focus " + automationID); return; } else { throw; } } //Ensure the element has focus before continuing. WaitForFocus(element); //Invoke the "Enter" key to raise the dialog Input.SendKeyboardInput(Key.Enter, true); WaitForUIRefresh(); Input.SendKeyboardInput(Key.Enter, false); }
/// <summary> /// Will press the given key gesture, and verify that the requested element is focused. /// </summary> /// <param name="gesture"></param> /// <param name="element"></param> public static void PressKeyCheckFocus( KeyGesture gesture, AutomationElement newElement) { AutomationHelper.InputKeyGesture(gesture); // Wait for new element to take focus. WaitForFocus(newElement); // Check if the new element was correctly assigned focus. TestServices.Assert(newElement.Current.HasKeyboardFocus, "Gesture [{0}+{1}] failed; {2} should have focus, but {3} does.", gesture.Modifiers.ToString(), gesture.Key.ToString(), newElement.Current.AutomationId, AutomationElement.FocusedElement.Current.AutomationId); }
/// <summary> /// Waits for the address bar in a given IE window to display the /// specified address. /// </summary> /// <remarks> /// Will TestServices.Assert on timeout. /// </remarks> /// <param name="ieWindow">The IE window</param> /// <param name="address">The address to wait for</param> public static void WaitForAddressBarText(AutomationElement ieWindow, string address) { // wait for the address bar to show the new document AutomationElement addressBar = AutomationHelper.FindDescendantById(ieWindow, "41477"); Condition editClassCondition = new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"); addressBar = addressBar.FindFirst(TreeScope.Descendants, editClassCondition); TestServices.Assert( addressBar != null, "Address bar edit control was not found."); TraceAutomationElement("AddressBar", addressBar); DateTime end = DateTime.Now.AddMilliseconds(Timeout); bool foundAddress = false; while ((!foundAddress) && (DateTime.Now < end)) { TextPattern textPattern = (TextPattern)(addressBar.GetCurrentPattern(TextPattern.Pattern)); string currentAddress = textPattern.DocumentRange.GetText(-1); if (currentAddress.Equals(address, StringComparison.CurrentCultureIgnoreCase)) { foundAddress = true; break; } else { System.Threading.Thread.Sleep(Interval); } } TestServices.Assert( foundAddress, "The address was not found: {0}", address); }
/// <summary> /// Focus a control, send it a keygesture, and check the proper result via GetText. /// </summary> /// <param name="target">The AutomationElement to focus on, or null to keep current focus.</param> /// <param name="keyGesture">The key gesture to send the target.</param> /// <param name="textElement">The AutomationElement to check the text of (via TextPattern).</param> /// <param name="desiredValue">The expected string.</param> public static void CheckKeyResult( AutomationElement target, KeyGesture keyGesture, AutomationElement textElement, string desiredValue ) { if (target != null) { target.SetFocus(); AutomationHelper.WaitForFocus(target); } InputKeyGesture(keyGesture); string actualValue = AutomationHelper.GetText(textElement); TestServices.Assert(actualValue.Equals(desiredValue), "Key gesture ({0}) failed. DesiredResult: {1} ActualResult: {2}", keyGesture.DisplayString, desiredValue, actualValue); }
/// <summary> /// Invokes an element that should open a window. This method will search for the /// element, invoke it, and then return the opened window. To work around public static AutomationElement FindAndInvokeUntilWindowOpened( AutomationElement parent, string automationID, string windowName) { while (true) { //Invoke the element. Here we should fail silently since it is possible that the previous //iteration has opened a modal dialog or closed the original window by this point. AutomationHelper.FindAndManualInvoke(parent, automationID, true); //Attempt to locate the window AutomationElement window = FindWindow(windowName, /*assertOnFailure*/ false, /*timeout*/ 15000); if (window == null) { TestServices.Log("Could not find the " + windowName + " window -- invoking the " + automationID + " button failed. Retrying..."); } else { return(window); } } }