Skip to content

Commit

Permalink
Added DragCoordinates action to appium UITest (#19333)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Dec 11, 2023
1 parent 089eacc commit 174a3a9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumPointerActions.cs
Expand Up @@ -16,15 +16,18 @@ public class AppiumPointerActions : ICommandExecutionGroup
const string DragAndDropCommand = "dragAndDrop";
const string ScrollToCommand = "scrollTo";
const string TapCoordinatesCommand = "tapCoordinates";
const string DragCoordinatesCommand = "dragCoordinates";

readonly AppiumApp _appiumApp;

readonly List<string> _commands = new()
{
ClickCommand,
DoubleClickCommand,
DragAndDropCommand,
ScrollToCommand,
TapCoordinatesCommand,
DragCoordinatesCommand
};

public AppiumPointerActions(AppiumApp appiumApp)
Expand All @@ -46,6 +49,7 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
DragAndDropCommand => DragAndDrop(parameters),
ScrollToCommand => ScrollTo(parameters),
TapCoordinatesCommand => TapCoordinates(parameters),
DragCoordinatesCommand => DragCoordinates(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
Expand Down Expand Up @@ -212,6 +216,29 @@ CommandResponse TapCoordinates(IDictionary<string, object> parameters)
return CommandResponse.FailedEmptyResponse;
}

CommandResponse DragCoordinates(IDictionary<string, object> parameters)
{
parameters.TryGetValue("fromX", out var fromX);
parameters.TryGetValue("fromY", out var fromY);

parameters.TryGetValue("toX", out var toX);
parameters.TryGetValue("toY", out var toY);

if (fromX is not null && fromY is not null && toX is not null && toY is not null)
{
DragCoordinates(
_appiumApp.Driver,
Convert.ToDouble(fromX),
Convert.ToDouble(fromY),
Convert.ToDouble(toX),
Convert.ToDouble(toY));

return CommandResponse.SuccessEmptyResponse;
}

return CommandResponse.FailedEmptyResponse;
}

static AppiumElement? GetAppiumElement(object element)
{
if (element is AppiumElement appiumElement)
Expand All @@ -235,5 +262,14 @@ static PointF ElementToClickablePoint(AppiumElement element)

return new PointF(x, y);
}

static void DragCoordinates(AppiumDriver driver, double fromX, double fromY, double toX, double toY)
{
new TouchAction(driver)
.Press(fromX, fromY)
.MoveTo(toX, toY)
.Release()
.Perform();
}
}
}
19 changes: 19 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Expand Up @@ -438,6 +438,25 @@ public static void SetSliderValue(this IApp app, string marked, double value, do
});
}

/// <summary>
/// Performs a continuous drag gesture between 2 points.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="fromX">The x coordinate to start dragging from.</param>
/// <param name="fromY">The y coordinate to start dragging from.</param>
/// <param name="toX">The x coordinate to drag to.</param>
/// <param name="toY">The y coordinate to drag to.</param>
public static void DragCoordinates(this IApp app, float fromX, float fromY, float toX, float toY)
{
app.CommandExecutor.Execute("dragCoordinates", new Dictionary<string, object>
{
{ "fromX", fromX },
{ "fromY", fromY },
{ "toX", toX },
{ "toY", toY },
});
}

static IUIElement Wait(Func<IUIElement> query,
Func<IUIElement, bool> satisfactory,
string? timeoutMessage = null,
Expand Down

0 comments on commit 174a3a9

Please sign in to comment.