Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ways to customise click actions to be able to perform more robu… #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/TestStack.White/UIItems/IUIItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ public interface IUIItem : IActionListener, IEquatable<IUIItem>
/// </summary>
void Click();

/// <summary>
/// Performs mouse click at relative location 1,1 is bottom right
/// </summary>
void RelativeClick(float x, float y);

/// <summary>
/// Performs mouse click at an absolute location from top left
/// </summary>
void AbsoluteClick(Point offset);

/// <summary>
/// Performs mouse double click at the center of the UI Item
/// </summary>
Expand Down
40 changes: 37 additions & 3 deletions src/TestStack.White/UIItems/UIItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,30 @@ public virtual void Invoke()
public virtual void Click()
{
actionListener.ActionPerforming(this);
PerformIfValid(PerformClick);
PerformIfValid(ClickCenter);
}

private void ClickCenter()
{
PerformRelativeClick(0.5f, 0.5f);
}

/// <summary>
/// Performs mouse click at relative location 1,1 is bottom right
/// </summary>
public virtual void RelativeClick(float x, float y)
{
actionListener.ActionPerforming(this);
PerformIfValid(() => PerformRelativeClick(x, y));
}

/// <summary>
/// Performs mouse click at an absolut location from top left
/// </summary>
public virtual void AbsoluteClick(Point offset)
{
actionListener.ActionPerforming(this);
PerformIfValid(() => PerformOffsetClick(offset));
}

/// <summary>
Expand Down Expand Up @@ -622,15 +645,26 @@ private void PerformIfValid(System.Action action)
throw new AutomationException(string.Format("Cannot perform action on {0}, {1}", this, message), Debug.Details(AutomationElement));
}

private void PerformClick()
private void PerformRelativeClick(float width, float height)
{
if (!Enabled) Logger.WarnFormat("Clicked on disabled item: {0}", ToString());
var bounds = Bounds;
if (bounds.IsEmpty)
{
throw new WhiteException(string.Format("Failed to click on {0}, bounds empty", ToString()));
}
mouse.Click(new Point(bounds.Left + (bounds.Width * width), bounds.Top + (bounds.Height * height)), actionListener);
}

private void PerformOffsetClick(Point offset)
{
if (!Enabled) Logger.WarnFormat("Clicked on disabled item: {0}", ToString());
var bounds = Bounds;
if (bounds.IsEmpty)
{
throw new WhiteException(string.Format("Failed to click on {0}, bounds empty", ToString()));
}
mouse.Click(bounds.Center(), actionListener);
mouse.Click(new Point(bounds.Left + offset.X, bounds.Top + offset.Y), actionListener);
}

#endregion
Expand Down