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

[Testing] Add swipe gestures methods to UITest #19132

Merged
merged 5 commits into from Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -71,6 +71,7 @@ public override string ToString()
new GalleryPageFactory(() => new SliderCoreGalleryPage(), "Slider Gallery"),
new GalleryPageFactory(() => new StepperCoreGalleryPage(), "Stepper Gallery"),
new GalleryPageFactory(() => new SwitchCoreGalleryPage(), "Switch Gallery"),
new GalleryPageFactory(() => new SwipeViewCoreGalleryPage(), "SwipeView Gallery"),
new GalleryPageFactory(() => new TimePickerCoreGalleryPage(), "Time Picker Gallery"),
new GalleryPageFactory(() => new WebViewCoreGalleryPage(), "WebView Gallery"),
};
Expand Down
@@ -0,0 +1,128 @@
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample
{
public class SwipeViewCoreGalleryPage : ContentPage
{
const string SwipeViewToRightId = "SwipeViewToRightId";
const string ResultToRightId = "ResultToRightId";
const string SwipeViewToLeftId = "SwipeViewToLeftId";
const string ResultToLeftId = "ResultToLeftId";

public SwipeViewCoreGalleryPage()
{
Title = "Issue 12079";

var layout = new StackLayout
{
Margin = new Thickness(12)
};

var swipeItemToRight = new SwipeItem
{
BackgroundColor = Colors.Red,
Text = "SwipeItem",
};

swipeItemToRight.Invoked += (sender, e) =>
{
DisplayAlert("SwipeView", "Invoked", "Ok");
};

var swipeToRightItems = new SwipeItems { swipeItemToRight };

swipeToRightItems.Mode = SwipeMode.Reveal;

var swipeToRightContent = new Grid
{
BackgroundColor = Colors.Gray
};

var swipeToRightLabel = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = "Swipe to Right"
};

swipeToRightContent.Children.Add(swipeToRightLabel);

var swipeToRightView = new SwipeView
{
AutomationId = SwipeViewToRightId,
HeightRequest = 60,
WidthRequest = 300,
LeftItems = swipeToRightItems,
Content = swipeToRightContent
};

var resultToRight = new Label
{
AutomationId = ResultToRightId
};

swipeToRightView.SwipeEnded += (sender, args) =>
{
resultToRight.Text = "Success";
};

var swipeItemToLeft = new SwipeItem
{
BackgroundColor = Colors.Green,
Text = "SwipeItem",
};

swipeItemToLeft.Invoked += (sender, e) =>
{
DisplayAlert("SwipeView", "Invoked", "Ok");
};

var swipeToLeftItems = new SwipeItems { swipeItemToLeft };

swipeToLeftItems.Mode = SwipeMode.Reveal;

var swipeToLeftContent = new Grid
{
BackgroundColor = Colors.Gray
};

var swipeToLeftLabel = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = "Swipe to Left"
};

swipeToLeftContent.Children.Add(swipeToLeftLabel);

var swipeToLeftView = new SwipeView
{
AutomationId = SwipeViewToLeftId,
HeightRequest = 60,
WidthRequest = 300,
RightItems = swipeToLeftItems,
Content = swipeToLeftContent
};

var resultToLeft = new Label
{
AutomationId = ResultToLeftId
};

swipeToLeftView.SwipeEnded += (sender, args) =>
{
resultToLeft.Text = "Success";
};

layout.Children.Add(swipeToRightView);
layout.Children.Add(resultToRight);

layout.Children.Add(swipeToLeftView);
layout.Children.Add(resultToLeft);

Content = layout;
}
}
}
67 changes: 67 additions & 0 deletions src/Controls/tests/UITests/Tests/SwipeViewUITests.cs
@@ -0,0 +1,67 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests
{
public class SwipeViewUITests : UITest
{
const string ScrollViewGallery = "SwipeView Gallery";

const string SwipeViewToRightId = "SwipeViewToRightId";
const string ResultToRightId = "ResultToRightId";
const string SwipeViewToLeftId = "SwipeViewToLeftId";
const string ResultToLeftId = "ResultToLeftId";

public SwipeViewUITests(TestDevice device)
: base(device)
{
}

protected override void FixtureSetup()
{
base.FixtureSetup();
App.NavigateToGallery(ScrollViewGallery);
}

protected override void FixtureTeardown()
{
base.FixtureTeardown();
this.Back();
}

[Test]
[Description("Swipe to right the SwipeView")]
public void SwipeToRight()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Mac, TestDevice.Windows });

// 1. Open the SwipeView using a gesture.
App.WaitForElement(SwipeViewToRightId);
App.SwipeLeftToRight(SwipeViewToRightId);

// 2. Check if the SwipeView has been opened correctly.
var result = App.FindElement(ResultToRightId).GetText();
Assert.AreEqual("Success", result);

App.Screenshot("The SwipeView is Open");
}

[Test]
[Description("Swipe to left the SwipeView")]
public void SwipeToLeft()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Mac, TestDevice.Windows });

// 1. Open the SwipeView using a gesture.
App.WaitForElement(SwipeViewToLeftId);
App.SwipeRightToLeft(SwipeViewToLeftId);

// 2. Check if the SwipeView has been opened correctly.
var result = App.FindElement(ResultToLeftId).GetText();
Assert.AreEqual("Success", result);

App.Screenshot("The SwipeView is Open");
}
}
}
120 changes: 120 additions & 0 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumSwipeActions.cs
@@ -0,0 +1,120 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using UITest.Core;

namespace UITest.Appium
{
public class AppiumSwipeActions : ICommandExecutionGroup
{
const string SwipeLeftToRightCommand = "swipeLeftToRight";
const string SwipeRightToLeftCommand = "swipeRightToLeft";

protected readonly AppiumApp _app;

readonly List<string> _commands = new()
{
SwipeLeftToRightCommand,
SwipeRightToLeftCommand
};

public AppiumSwipeActions(AppiumApp app)
{
_app = app;
}

public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}

public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
SwipeLeftToRightCommand => SwipeLeftToRight(parameters),
SwipeRightToLeftCommand => SwipeRightToLeft(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}

CommandResponse SwipeLeftToRight(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);

double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];

SwipeToRight(_app.Driver, element, swipePercentage, swipeSpeed, withInertia);

return CommandResponse.SuccessEmptyResponse;
}

CommandResponse SwipeRightToLeft(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var value);
var element = GetAppiumElement(value);

double swipePercentage = (double)parameters["swipePercentage"];
int swipeSpeed = (int)parameters["swipeSpeed"];
bool withInertia = (bool)parameters["withInertia"];

SwipeToLeft(_app.Driver, element, swipePercentage, swipeSpeed, withInertia);

return CommandResponse.SuccessEmptyResponse;
}

static AppiumElement? GetAppiumElement(object? element)
{
if (element is AppiumElement appiumElement)
{
return appiumElement;
}
else if (element is AppiumDriverElement driverElement)
{
return driverElement.AppiumElement;
}

return null;
}

static void SwipeToRight(AppiumDriver driver, AppiumElement? element, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
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(swipeSpeed)
.MoveTo(endX, endY)
.Release()
.Perform();
}

static void SwipeToLeft(AppiumDriver driver, AppiumElement? element, double swipePercentage, int swipeSpeed, bool withInertia = true)
{
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(swipeSpeed)
.MoveTo(endX, endY)
.Release()
.Perform();
}
}
}
1 change: 1 addition & 0 deletions src/TestUtils/src/UITest.Appium/AppiumApp.cs
Expand Up @@ -21,6 +21,7 @@ public AppiumApp(AppiumDriver driver, IConfig config)
_commandExecutor.AddCommandGroup(new AppiumTextActions());
_commandExecutor.AddCommandGroup(new AppiumGeneralActions());
_commandExecutor.AddCommandGroup(new AppiumVirtualKeyboardActions(this));
_commandExecutor.AddCommandGroup(new AppiumSwipeActions(this));
_commandExecutor.AddCommandGroup(new AppiumOrientationActions(this));
}

Expand Down