Skip to content

Commit

Permalink
update code to match .net maui
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Mar 25, 2024
1 parent 85dfc3c commit 6a25dcb
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,23 @@ CommandResponse LaunchApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

_app.Driver.LaunchApp();
if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ExecuteScript("macos: activateApp", new Dictionary<string, object>
{
{ "bundleId", _app.GetAppId() },
});
}
else if (_app.GetTestDevice() == TestDevice.Windows)
{
#pragma warning disable CS0618 // Type or member is obsolete
_app.Driver.LaunchApp();
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
_app.Driver.ActivateApp(_app.GetAppId());
}

return CommandResponse.SuccessEmptyResponse;
}
Expand Down Expand Up @@ -85,25 +101,8 @@ CommandResponse ResetApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

// Terminate App not supported on Mac
if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ResetApp();
}
else if (_app.GetTestDevice() == TestDevice.Windows)
{
CloseApp(parameters);
_app.Driver.LaunchApp();
}
else
{
_app.Driver.TerminateApp(_app.GetAppId());

if (_app.GetTestDevice() == TestDevice.iOS)
_app.Driver.ActivateApp(_app.GetAppId());
else
_app.Driver.LaunchApp();
}
CloseApp(parameters);
LaunchApp(parameters);

return CommandResponse.SuccessEmptyResponse;
}
Expand All @@ -113,7 +112,24 @@ CommandResponse CloseApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

_app.Driver.CloseApp();
if (_app.AppState == ApplicationState.NotRunning)
return CommandResponse.SuccessEmptyResponse;

if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ExecuteScript("macos: terminateApp", new Dictionary<string, object>
{
{ "bundleId", _app.GetAppId() },
});
}
else if (_app.GetTestDevice() == TestDevice.Windows)
{
#pragma warning disable CS0618 // Type or member is obsolete
_app.Driver.CloseApp();
#pragma warning restore CS0618 // Type or member is obsolete
}
else
_app.Driver.TerminateApp(_app.GetAppId());

return CommandResponse.SuccessEmptyResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ CommandResponse ScrollTo(IDictionary<string, object> parameters)
throw new TimeoutException($"Timed out scrolling to {toElementId}");
}

var scrollAction = new TouchAction(_appiumApp.Driver).Press(x, startY).MoveTo(x, endY).Release();
scrollAction.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var scrollSequence = new ActionSequence(touchDevice, 0);
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, x, (int)startY, TimeSpan.Zero));
scrollSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, x, (int)endY, TimeSpan.FromMilliseconds(500)));
scrollSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
_appiumApp.Driver.PerformActions([scrollSequence]);
}
}

Expand Down Expand Up @@ -265,11 +270,13 @@ static PointF ElementToClickablePoint(AppiumElement element)

static void DragCoordinates(AppiumDriver driver, double fromX, double fromY, double toX, double toY)
{
new TouchAction(driver)
.Press(fromX, fromY)
.MoveTo(toX, toY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var dragSequence = new ActionSequence(touchDevice, 0);
dragSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, (int)fromX, (int)fromY, TimeSpan.Zero));
dragSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
dragSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, (int)toX, (int)toY, TimeSpan.FromMilliseconds(250)));
dragSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([dragSequence]);
}
}
}
76 changes: 44 additions & 32 deletions src/Plugin.Maui.UITestHelpers.Appium/Actions/AppiumScrollActions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Interactions;
using Plugin.Maui.UITestHelpers.Core;

namespace Plugin.Maui.UITestHelpers.Appium
Expand All @@ -13,6 +15,8 @@ public enum ScrollStrategy

public class AppiumScrollActions : ICommandExecutionGroup
{
const int ScrollTouchDownTime = 100;
const int ProgrammaticallyScrollTime = 0;
const string ScrollLeftCommand = "scrollLeft";
const string ScrollDownCommand = "scrollDown";
const string ScrollRightCommand = "scrollRight";
Expand Down Expand Up @@ -138,78 +142,86 @@ CommandResponse ScrollUp(IDictionary<string, object> parameters)

static void ScrollToLeft(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
var position = element is not null ? element.Location : System.Drawing.Point.Empty;
var size = element is not null ? element.Size : driver.Manage().Window.Size;

int startX = (int)(position.X + (size.Width * 0.05));
int startY = position.Y + size.Height / 2;

int endX = (int)(position.X + (size.Width * swipePercentage));
int endY = startY;

new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var scrollSequence = new ActionSequence(touchDevice, 0);
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
scrollSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
scrollSequence.AddAction(touchDevice.CreatePause(TimeSpan.FromMilliseconds(ScrollTouchDownTime)));
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(strategy != ScrollStrategy.Programmatically ? swipeSpeed : ProgrammaticallyScrollTime)));
scrollSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([scrollSequence]);
}

static void ScrollToDown(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
var position = element is not null ? element.Location : System.Drawing.Point.Empty;
var size = element is not null ? element.Size : driver.Manage().Window.Size;

int startX = position.X + size.Width / 2;
int startY = (int)(position.Y + (size.Height * swipePercentage));

int endX = startX;
int endY = (int)(position.Y + (size.Height * 0.05));

new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var scrollSequence = new ActionSequence(touchDevice, 0);
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
scrollSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
scrollSequence.AddAction(touchDevice.CreatePause(TimeSpan.FromMilliseconds(ScrollTouchDownTime)));
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(strategy != ScrollStrategy.Programmatically ? swipeSpeed : ProgrammaticallyScrollTime)));
scrollSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([scrollSequence]);
}

static void ScrollToRight(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
var position = element is not null ? element.Location : System.Drawing.Point.Empty;
var size = element is not null ? element.Size : driver.Manage().Window.Size;

int startX = (int)(position.X + (size.Width * swipePercentage));
int startY = position.Y + size.Height / 2;

int endX = (int)(position.X + (size.Width * 0.05));
int endY = startY;

new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var scrollSequence = new ActionSequence(touchDevice, 0);
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
scrollSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
scrollSequence.AddAction(touchDevice.CreatePause(TimeSpan.FromMilliseconds(ScrollTouchDownTime)));
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(strategy != ScrollStrategy.Programmatically ? swipeSpeed : ProgrammaticallyScrollTime)));
scrollSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([scrollSequence]);
}

static void ScrollToUp(AppiumDriver driver, AppiumElement element, ScrollStrategy strategy, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
var position = element.Location;
var size = element.Size;
var position = element is not null ? element.Location : System.Drawing.Point.Empty;
var size = element is not null ? element.Size : driver.Manage().Window.Size;

int startX = position.X + size.Width / 2;
int startY = (int)(position.Y + (size.Height * 0.05));

int endX = startX;
int endY = (int)(position.Y + (size.Height * swipePercentage));

new TouchAction(driver)
.Press(startX, startY)
.Wait(strategy != ScrollStrategy.Programmatically ? swipeSpeed : 0)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var scrollSequence = new ActionSequence(touchDevice, 0);
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
scrollSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
scrollSequence.AddAction(touchDevice.CreatePause(TimeSpan.FromMilliseconds(ScrollTouchDownTime)));
scrollSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(strategy != ScrollStrategy.Programmatically ? swipeSpeed : ProgrammaticallyScrollTime)));
scrollSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([scrollSequence]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Interactions;
using Plugin.Maui.UITestHelpers.Core;

namespace Plugin.Maui.UITestHelpers.Appium
Expand Down Expand Up @@ -75,14 +77,15 @@ static void SetSliderValue(AppiumDriver driver, AppiumElement? element, double v
int x = position.X;
int y = position.Y;

double moveToX = (x + size.Width) * value / maximum;
int moveToX = (int)((x + size.Width) * value / maximum);

TouchAction touchAction = new TouchAction(driver);
touchAction
.Press(x, y)
.MoveTo(moveToX, y)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var touchSequence = new ActionSequence(touchDevice, 0);
touchSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, x, y, TimeSpan.Zero));
touchSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
touchSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, moveToX, y, TimeSpan.FromMicroseconds(250)));
touchSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([touchSequence]);
}
}
}
28 changes: 16 additions & 12 deletions src/Plugin.Maui.UITestHelpers.Appium/Actions/AppiumSwipeActions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Interactions;
using Plugin.Maui.UITestHelpers.Core;

namespace Plugin.Maui.UITestHelpers.Appium
Expand Down Expand Up @@ -90,12 +92,13 @@ static void SwipeToRight(AppiumDriver driver, AppiumElement? element, double swi
int endX = (int)(position.X + (size.Width * swipePercentage));
int endY = startY;

new TouchAction(driver)
.Press(startX, startY)
.Wait(swipeSpeed)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var swipeSequence = new ActionSequence(touchDevice, 0);
swipeSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
swipeSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
swipeSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(swipeSpeed)));
swipeSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([swipeSequence]);
}

static void SwipeToLeft(AppiumDriver driver, AppiumElement? element, double swipePercentage, int swipeSpeed, bool withInertia = true)
Expand All @@ -109,12 +112,13 @@ static void SwipeToLeft(AppiumDriver driver, AppiumElement? element, double swip
int endX = (int)(position.X + (size.Width * 0.05));
int endY = startY;

new TouchAction(driver)
.Press(startX, startY)
.Wait(swipeSpeed)
.MoveTo(endX, endY)
.Release()
.Perform();
var touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
var swipeSequence = new ActionSequence(touchDevice, 0);
swipeSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, startX, startY, TimeSpan.Zero));
swipeSequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
swipeSequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, endX, endY, TimeSpan.FromMilliseconds(swipeSpeed)));
swipeSequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions([swipeSequence]);
}
}
}
2 changes: 1 addition & 1 deletion src/Plugin.Maui.UITestHelpers.Appium/AppiumApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AppiumApp(AppiumDriver driver, IConfig config)
public FileInfo Screenshot(string fileName)
{
Screenshot screenshot = _driver.GetScreenshot();
screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Png);
screenshot.SaveAsFile(fileName);
var file = new FileInfo(fileName);
return file;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-beta02" />
<PackageReference Include="System.Drawing.Common" Version="4.7.3" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.6" />
<PackageReference Include="System.Drawing.Common" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 6a25dcb

Please sign in to comment.