/// <summary> /// Sets the new position of the window. /// </summary> /// <param name="left">The new position from the left.</param> /// <param name="top">The new position from the top.</param> /// <returns>A WindowSetup to be able to append additional actions.</returns> public WindowSetup Position(int left, int top) { LogPool.Append("Set window position to left {0} and top {1}.", left, top); _window.Unsafe.SetPosition(new Point(left, top)); return(this); }
/// <summary> /// Sets the new state of the window. /// </summary> /// <param name="windowState">The new state of the window.</param> /// <returns>A WindowSetup to be able to append additional actions.</returns> public WindowSetup State(WindowState windowState) { LogPool.Append("Set window state to '{0}'.", windowState); _window.Unsafe.SetState(windowState); return(this); }
/// <summary> /// Sets the new size of the window. /// </summary> /// <param name="width">The new width of the window.</param> /// <param name="height">The new height of the window.</param> /// <returns>A WindowSetup to be able to append additional actions.</returns> public WindowSetup Size(int width, int height) { LogPool.Append("Set window size to width {0} and height {1}.", width, height); _window.Unsafe.SetSize(new Size(width, height)); return(this); }
/// <summary> /// Returns a tree of information objects which shows the whole tree below the given object. /// </summary> /// <param name="element">The parent object from which all child elements have to be read.</param> /// <returns>The given element as an information object wich all its children in a tree.</returns> public static AutomationElementInfo GetFullUITree(AutomationElement element) { var rootElementInfo = new AutomationElementInfo(element); LogPool.Append("Read full UI tree down from '{0}'.", rootElementInfo); Read(rootElementInfo); return(rootElementInfo); }
/// <summary> /// Searches for a window by the given conditions and settings. If not disabled And.Assert().And.Timeout(10000) gets appended. /// </summary> /// <param name="use">Defines the conditions to be used for searching for a window.</param> /// <param name="is">Defines the relation of the window to another object.</param> /// <param name="settings">Defines the settings to be used while searching.</param> /// <exception cref="DW.CodedUI.WindowNotFoundException">The window could not be found. (If not disabled.)</exception> /// <exception cref="DW.CodedUI.WrongSetupException">Dll is missing or in the wrong version.</exception> /// <returns>The found window if any; otherwise an exception as long its not disabled by And.NoAssert(). If its disabled the return is null.</returns> public static BasicWindow Search(Use use, Is @is, And settings) { var condition = use.GetCondition(); var settingsConditions = settings.GetConditions(); var instanceNumber = settings.GetInstanceNumber(); var timeout = settings.GetTimeout(); var useTimeout = settingsConditions.Contains(AndCondition.Timeout); var assertResult = settingsConditions.Contains(AndCondition.Assert); var checkInstance = settingsConditions.Contains(AndCondition.Instance); var useInterval = settingsConditions.Contains(AndCondition.Interval); var interval = settings.GetInterval(); if (CodedUIEnvironment.LoggerSettings.ShortLogging) { LogPool.Append("Search for a window. {0}", use.GetConditionDescription()); } else { LogPool.Append("Search for a window. {0}", MessageBuilder.BuildMessage(use, useTimeout, useInterval, interval, timeout, @is)); } var watch = new Stopwatch(); watch.Start(); while (true) { var foundInstance = 0; var windows = GetAllWindows(); foreach (var window in windows) { var matchingWindow = Matches(window, condition, @is); if (matchingWindow == null) { continue; } if (!checkInstance || ++foundInstance == instanceNumber) { LogPool.Append("Window '{0}' found.", matchingWindow); return(matchingWindow); } } if (!useTimeout || watch.Elapsed.TotalMilliseconds >= timeout) { if (assertResult) { throw new WindowNotFoundException(use, useTimeout, useInterval, interval, watch.Elapsed, @is); } return(null); } if (useInterval) { Thread.Sleep((int)interval); } } }
/// <summary> /// Executes a mouse left click on a relative position inside the given basic element. /// </summary> /// <param name="element">The basic element to be clicked.</param> /// <param name="relativePosition">The relative position in the control where to click.</param> /// <returns>A combinable Do to be able to append additional actions</returns> public static CombinableDo Click(BasicElement element, At relativePosition) { return(WrapIt(() => { LogPool.Append("Click with the left mouse button at the relative position '{0}' on the element '{1}'", relativePosition, element); ClickRelative(element, MouseButtons.Left, relativePosition); }, string.Format("Cannot click with the left mouse button at the relative position '{0}' on the element '{1}'", relativePosition, element))); }
/// <summary> /// Types the given key. /// </summary> /// <param name="key">The key to type.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo TypeKey(Key key) { return(WrapIt(() => { LogPool.Append("Type key '{0}'.", key); Type((byte)key); }, string.Format("Cannot type the key '{0}'", key))); }
/// <summary> /// Types the given modifier keys. /// </summary> /// <param name="modifierKeys">The modifier keys to type.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo TypeKey(ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Type modifier keys '{0}'.", modifierKeys); Type((byte)modifierKeys); }, string.Format("Cannot type the modifier keys '{0}'", modifierKeys))); }
/// <summary> /// Executes a mouse left double click in the center of the given basic element. /// </summary> /// <param name="element">The basic element to be double clicked.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element) { return(WrapIt(() => { LogPool.Append("Doubleclick with the left mouse button in the center of the '{0}'.", element); DoubleClickCentered(element, MouseButtons.Left); }, string.Format("Cannot doubleclick the BasicElement '{0}' centered with the left mouse button.", element))); }
/// <summary> /// Executes a mouse double click with specific mouse buttons on a relative position in the given basic element. /// </summary> /// <param name="element">The basic element to be double clicked.</param> /// <param name="buttons">The mouse buttons which will be double clicked.</param> /// <param name="relativePosition">The relative position in the control where to double click.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element, MouseButtons buttons, At relativePosition) { return(WrapIt(() => { LogPool.Append("Doubleclick with the mouse buttons '{0}' at the relative position '{1}' on the element '{2}'", buttons, relativePosition, element); DoubleClickRelative(element, buttons, relativePosition); }, string.Format("Cannot doubleclick with the mouse buttons '{0}' at the relative position '{1}' on the element '{2}'", buttons, relativePosition, element))); }
/// <summary> /// Executes a mouse double click with specific mouse buttons and hold modifier keys in the center of the given basic element. /// </summary> /// <param name="element">The basic element to be double clicked.</param> /// <param name="buttons">The mouse buttons which will be double clicked.</param> /// <param name="modifierKeys">The modifier keys to be hold while the double click is executed.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element, MouseButtons buttons, ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Click with the mouse buttons '{0}' in the center of the '{1}' with the modifier keys '{2}' pressed.", buttons, element, modifierKeys); DoubleClickCentered(element, buttons, modifierKeys); }, string.Format("Cannot click with the mouse buttons '{0}' in the center of the '{1}' with the modifier keys '{2}' pressed.", buttons, element, modifierKeys))); }
/// <summary> /// Executes a mouse double click with specific mouse buttons in the center of the given basic element. /// </summary> /// <param name="element">The basic element to be double clicked.</param> /// <param name="buttons">The mouse buttons which will be double clicked.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element, MouseButtons buttons) { return(WrapIt(() => { LogPool.Append("Doubleclick the BasicElement '{0}' centered with the mouse buttons '{1}'.", element, buttons); DoubleClickCentered(element, buttons); }, string.Format("Cannot doubleclick the BasicElement '{0}' centered with the mouse buttons '{1}'.", element, buttons))); }
/// <summary> /// Executes a mouse left click. /// </summary> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Click() { return(WrapIt(() => { LogPool.Append("Click with left mouse button."); WinApi.MouseEvent((int)MouseButtons.Left); }, "Cannot click with the left mouse button.")); }
/// <summary> /// Presses and holds the given key down. Use <see cref="ReleaseKey(Key)" /> to release the key back again. /// </summary> /// <param name="key">The key to press and hold.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo PressKey(Key key) { return(WrapIt(() => { LogPool.Append("Press key '{0}'.", key); WinApi.KeyboardEvent((byte)key, WinApi.KeyboardEventFlags.KEYEVENTF_EXTENDEDKEY); }, string.Format("Cannot press down the key '{0}'.", key))); }
/// <summary> /// Executes a mouse left double click at a relative position of the given basic element with holding modifier keys. /// </summary> /// <param name="element">The basic element to be double clicked.</param> /// <param name="modifierKeys">The modifier keys to be hold while the double click is executed.</param> /// <param name="relativePosition">The relative position in the control where to double click.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element, ModifierKeys modifierKeys, At relativePosition) { return(WrapIt(() => { LogPool.Append("Doubleclick with the left mouse button at the relative position '{0}' on the element '{1}' with the modifier keys {2} pressed.", relativePosition, element, modifierKeys); DoubleClickRelative(element, MouseButtons.Left, modifierKeys, relativePosition); }, string.Format("Cannot doubleclick with the left mouse button at the relative position '{0}' on the element '{1}' with the modifier keys {2} pressed.", relativePosition, element, modifierKeys))); }
/// <summary> /// Releases the key which got pressed before. /// </summary> /// <param name="key">The key to release.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo ReleaseKey(Key key) { return(WrapIt(() => { LogPool.Append("Release key '{0}'.", key); WinApi.KeyboardEvent((byte)key, WinApi.KeyboardEventFlags.KEYEVENTF_EXTENDEDKEY | WinApi.KeyboardEventFlags.KEYEVENTF_KEYUP); }, string.Format("Cannot release the key '{0}'", key))); }
/// <summary> /// Executes a mouse click with specific mouse buttons. /// </summary> /// <param name="buttons">The mouse buttons which will be clicked.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Click(MouseButtons buttons) { return(WrapIt(() => { LogPool.Append("Click with the buttons '{0}'.", buttons); WinApi.MouseEvent((int)buttons); }, string.Format("Cannot click the mouse buttons '{0}'", buttons))); }
/// <summary> /// Releases the modifier key which got pressed before. /// </summary> /// <param name="modifierKeys">The modifier keys to release.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo ReleaseKey(ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Release keys '{0}'.", modifierKeys); WinApi.KeyboardEvent((byte)modifierKeys, WinApi.KeyboardEventFlags.KEYEVENTF_EXTENDEDKEY | WinApi.KeyboardEventFlags.KEYEVENTF_KEYUP); }, string.Format("Cannot release the modifier key '{0}'", modifierKeys))); }
/// <summary> /// Presses and holds the given modifier keys down. Use <see cref="ReleaseKey(ModifierKeys)" /> to release the key back again. /// </summary> /// <param name="modifierKeys">The modifier keys to press and hold.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo PressKey(ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Press key '{0}'.", modifierKeys); WinApi.KeyboardEvent((byte)modifierKeys, WinApi.KeyboardEventFlags.KEYEVENTF_EXTENDEDKEY); }, string.Format("Cannot press down the modifier key '{0}'.", modifierKeys))); }
/// <summary> /// Executes a mouse double click with specific mouse buttons and hold modifier keys in the relative position of the given basic element. /// </summary> /// <param name="element">The basic element to be clicked.</param> /// <param name="buttons">The mouse buttons which will be double clicked.</param> /// <param name="modifierKeys">The modifier keys to be hold while the double click is executed.</param> /// <param name="relativePosition">The relative position in the control where to double click.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo DoubleClick(BasicElement element, MouseButtons buttons, ModifierKeys modifierKeys, At relativePosition) { return(WrapIt(() => { LogPool.Append("Doubleclick with the mouse buttons '{0}' in relative position '{1}' of the '{2}' with the modifier keys '{3}' pressed.", buttons, relativePosition, element, modifierKeys); DoubleClickRelative(element, buttons, modifierKeys, relativePosition); }, string.Format("Cannot doubleclick with the mouse buttons '{0}' in relative position '{1}' of the '{2}' with the modifier keys '{3}' pressed.", buttons, relativePosition, element, modifierKeys))); }
/// <summary> /// Types the given text. /// </summary> /// <param name="text">The text to type.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo TypeText(string text) { return(WrapIt(() => { LogPool.Append("Type text '{0}'.", text); SendText(text); }, string.Format("Cannot send the text '{0}'", text))); }
/// <summary> /// Executes a mouse left click at a specific position within the screen. /// </summary> /// <param name="screenCoordinate">The point on the whole screen where to click.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Click(Point screenCoordinate) { return(WrapIt(() => { LogPool.Append("Click with the left mouse button on the screen coordinate '{0}'.", screenCoordinate); Cursor.Position = screenCoordinate.ToDrawingPoint(); WinApi.MouseEvent((int)MouseButtons.Left); }, string.Format("Click with the left mouse button on the screen coordinate '{0}'.", screenCoordinate))); }
/// <summary> /// Places the mouse cursor on the specific position. /// </summary> /// <param name="to">The position where the mouse cursor should be placed.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Move(Position to) { return(WrapIt(() => { var position = to.GetPosition(); LogPool.Append("Place the mouse cursor on position '{0}'.", position); Cursor.Position = position.ToDrawingPoint(); }, string.Format("Cannot place the mouse cursor on position '{0}'.", to.GetPosition()))); }
private static TControl StartSearchChild <TControl>(By by, From from, With with) where TControl : BasicElement { var condition = by.GetCondition(); var rawCondition = by.GetRawCondition(); var sourceElement = from.GetSourceElement(); var settings = with.GetConditions(); var useTimeout = settings.Contains(WithCondition.Timeout); var timeout = with.GetTimeout(); var assertResult = settings.Contains(WithCondition.Assert); var useInterval = settings.Contains(WithCondition.Interval); var interval = with.GetInterval(); var needsToBeReady = settings.Contains(WithCondition.ReadyToUse); if (CodedUIEnvironment.LoggerSettings.ShortLogging) { LogPool.Append("Search for UI element. {0}", by.GetConditionDescription()); } else { LogPool.Append("Search for UI element down from '{0}'. {1}", sourceElement, MessageBuilder.BuildMessage(by, useTimeout, useInterval, timeout, interval)); } var watch = new Stopwatch(); watch.Start(); while (true) { var foundItem = StartSearchChild <TControl>(sourceElement, condition, rawCondition); if (foundItem != null) { if (!needsToBeReady) { LogPool.Append("UI element '{0}' found.", foundItem); return(foundItem); } return(ReturnIfReady(assertResult, by, useTimeout, useInterval, timeout, watch, interval, false, foundItem)); } if (!useTimeout || watch.Elapsed.TotalMilliseconds >= timeout) { if (assertResult) { throw new UIElementNotFoundException(by, useTimeout, useInterval, interval, watch.Elapsed, false); } return(null); } if (useInterval) { Thread.Sleep((int)interval); } } }
/// <summary> /// Executes a mouse left click while holding specific modifier keys. /// </summary> /// <param name="modifierKeys">The modifier keys to be hold while the click is executed.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Click(ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Click with the left mouse button and with the modifier keys '{0}' pressed.", modifierKeys); KeyboardEx.PressKey(modifierKeys); WinApi.MouseEvent((int)MouseButtons.Left); KeyboardEx.ReleaseKey(modifierKeys); }, string.Format("Cannot click the left mouse button with the modifier keys '{0}' pressed.", modifierKeys))); }
/// <summary> /// Gives the BasicElement the focus and types the given text with a small delay time between each character. /// </summary> /// <param name="control">The BasicElement who should get the focus before typing the text.</param> /// <param name="text">The text to type.</param> /// <param name="delay">The time to wait after each character in milliseconds.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo TypeText(BasicElement control, string text, int delay) { return(WrapIt(() => { LogPool.Append("Set control focus for keyboard inputs on '{0}'.", control); control.AutomationElement.SetFocus(); LogPool.Append("Type text '{0}' with a delay of {1} milliseconds.", text, delay); SendText(text, delay); }, string.Format("Cannot send the text '{0}' with the '{1} focused.'", text, control))); }
/// <summary> /// Gives the BasicElement the focus and presses and holds the given key down. Use <see cref="ReleaseKey(Key)" /> to release the key back again. /// </summary> /// <param name="control">The BasicElement who should get the focus before pressing the key.</param> /// <param name="key">The key to press and hold.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo PressKey(BasicElement control, Key key) { return(WrapIt(() => { LogPool.Append("Set control focus for keyboard inputs on '{0}'.", control); control.AutomationElement.SetFocus(); LogPool.Append("Press key '{0}'.", key); WinApi.KeyboardEvent((byte)key, WinApi.KeyboardEventFlags.KEYEVENTF_EXTENDEDKEY); }, string.Format("Cannot press down the key '{0}' with the '{1}' focused.", key, control))); }
/// <summary> /// Waits the given time. /// </summary> /// <param name="milliseconds">The milliseconds to wait.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public CombinableDo Wait(uint milliseconds) { _doAction = DoAction.Action; _currentAction = () => { LogPool.Append("Wait {0} milliseconds.", milliseconds); Thread.Sleep((int)milliseconds); }; Execute(); return(new CombinableDo()); }
/// <summary> /// Gives the BasicElement the focus and types the given modifier keys. /// </summary> /// <param name="control">The BasicElement who should get the focus before typing the key.</param> /// <param name="modifierKeys">The modifier keys to type.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo TypeKey(BasicElement control, ModifierKeys modifierKeys) { return(WrapIt(() => { LogPool.Append("Set control focus for keyboard inputs on '{0}'.", control); control.AutomationElement.SetFocus(); LogPool.Append("Type modifier keys '{0}'.", modifierKeys); Type((byte)modifierKeys); }, string.Format("Cannot press down the modifier keys '{0}' with the '{1}' focused.", modifierKeys, control))); }
/// <summary> /// Executes a mouse click at a specific position within the screen by using specific mouse buttons and holding a modifier key. /// </summary> /// <param name="buttons">The mouse buttons which will be clicked.</param> /// <param name="modifierKeys">The modifier keys to be hold while the click is executed.</param> /// <param name="screenCoordinate">The point on the whole screen where to click.</param> /// <returns>A combinable Do to be able to append additional actions.</returns> public static CombinableDo Click(MouseButtons buttons, ModifierKeys modifierKeys, Point screenCoordinate) { return(WrapIt(() => { LogPool.Append("Click with the modifier keys '{0}' on the screen coordinate '{1}'.", modifierKeys, screenCoordinate); Cursor.Position = screenCoordinate.ToDrawingPoint(); KeyboardEx.PressKey(modifierKeys); WinApi.MouseEvent((int)buttons); KeyboardEx.ReleaseKey(modifierKeys); }, string.Format("Cannot click with the modifier keys '{0}' on the screen coordinate '{1}'.", modifierKeys, screenCoordinate))); }