[Testing] Add SetSliderValue method to UITest (#19182)

* Added Slider UITest actions

* Updated sample

* More changes

* Updated test
This commit is contained in:
Javier Suárez 2023-12-05 11:02:57 +01:00 коммит произвёл GitHub
Родитель e1c1e525dc
Коммит b69956e45d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 227 добавлений и 11 удалений

Просмотреть файл

@ -1,19 +1,53 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
namespace Maui.Controls.Sample;
internal class SliderCoreGalleryPage : CoreGalleryPage<Slider>
internal class SliderCoreGalleryPage : ContentPage
{
protected override bool SupportsFocus => false;
protected override bool SupportsTapGestureRecognizer => false;
protected override void InitializeElement(Slider element)
public SliderCoreGalleryPage()
{
}
var layout = new StackLayout
{
Padding = new Microsoft.Maui.Thickness(12)
};
protected override void Build()
{
base.Build();
// Default
var defaultLabel = new Label { AutomationId = "DefaultSlider", Text = "Default" };
var defaultSlider = new Slider();
layout.Add(defaultLabel);
layout.Add(defaultSlider);
// BackgroundColor
var backgroundColorLabel = new Label { Text = "BackgroundColor" };
var backgroundColorSlider = new Slider { AutomationId = "BackgroundColorSlider", BackgroundColor = Colors.Red };
layout.Add(backgroundColorLabel);
layout.Add(backgroundColorSlider);
// MaximumTrackColor
var maximumTrackColorLabel = new Label { Text = "MaximumTrackColor" };
var maximumTrackColorSlider = new Slider { AutomationId = "MaximumTrackColorSlider", MinimumTrackColor = Colors.Orange };
layout.Add(maximumTrackColorLabel);
layout.Add(maximumTrackColorSlider);
// MinimumTrackColor
var minimumTrackColorLabel = new Label { Text = "MinimumTrackColor" };
var minimumTrackColorSlider = new Slider { AutomationId = "MinimumTrackColorSlider", MaximumTrackColor = Colors.Blue };
layout.Add(minimumTrackColorLabel);
layout.Add(minimumTrackColorSlider);
// ThumbColor
var thumbColorLabel = new Label { Text = "ThumbColor" };
var thumbColorSlider = new Slider { AutomationId = "ThumbColorSlider", ThumbColor = Colors.Green };
layout.Add(thumbColorLabel);
layout.Add(thumbColorSlider);
// Custom Slider
var customLabel = new Label { Text = "Custom" };
var customSlider = new Slider { AutomationId = "CustomSlider", Minimum = 0, Maximum = 100, Value = 30 };
layout.Add(customLabel);
layout.Add(customSlider);
Content = layout;
}
}
}

Просмотреть файл

@ -0,0 +1,50 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
namespace Microsoft.Maui.AppiumTests
{
public class SliderUITests : UITest
{
public const string SliderGallery = "Slider Gallery";
public SliderUITests(TestDevice device)
: base(device)
{
}
protected override void FixtureSetup()
{
base.FixtureSetup();
App.NavigateToGallery(SliderGallery);
}
protected override void FixtureTeardown()
{
base.FixtureTeardown();
this.Back();
}
[Test]
[Description("Set different slider values")]
public void SetSliderValue()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Mac, TestDevice.Windows });
const string customSlider = "CustomSlider";
App.WaitForElement(customSlider);
// 1. Move the thumb to the left.
App.SetSliderValue(customSlider, 0, maximum: 100);
App.Screenshot("Move the thumb to the left");
// 2. Move the thumb to the right.
App.SetSliderValue(customSlider, 100, maximum: 100);
App.Screenshot("Move the thumb to the right");
// 3. Move the thumb to the center.
App.SetSliderValue(customSlider, 50, maximum: 100);
App.Screenshot("Move the thumb to the center");
}
}
}

Просмотреть файл

@ -0,0 +1,88 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using UITest.Core;
namespace UITest.Appium
{
public class AppiumSliderActions : ICommandExecutionGroup
{
const string SetSliderValueCommand = "setSliderValue";
protected readonly AppiumApp _app;
public AppiumSliderActions(AppiumApp app)
{
_app = app;
}
readonly List<string> _commands = new()
{
SetSliderValueCommand,
};
public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
SetSliderValueCommand => SetSliderValue(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
CommandResponse SetSliderValue(IDictionary<string, object> parameters)
{
parameters.TryGetValue("element", out var element);
var slider = GetAppiumElement(element);
if (slider is not null)
{
var minimum = (double)parameters["minimum"];
var maximum = (double)parameters["maximum"];
var value = (double)parameters["value"];
SetSliderValue(_app.Driver, slider, value, minimum, maximum);
return CommandResponse.SuccessEmptyResponse;
}
return CommandResponse.FailedEmptyResponse;
}
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 SetSliderValue(AppiumDriver driver, AppiumElement? element, double value, double minimum = 0d, double maximum = 1d)
{
var position = element is not null ? element.Location : System.Drawing.Point.Empty;
var size = element is not null ? element.Size : System.Drawing.Size.Empty;
int x = position.X;
int y = position.Y;
double moveToX = (x + size.Width) * value / maximum;
TouchAction touchAction = new TouchAction(driver);
touchAction
.Press(x, y)
.MoveTo(moveToX, y)
.Release()
.Perform();
}
}
}

Просмотреть файл

@ -21,6 +21,7 @@ namespace UITest.Appium
_commandExecutor.AddCommandGroup(new AppiumTextActions());
_commandExecutor.AddCommandGroup(new AppiumGeneralActions());
_commandExecutor.AddCommandGroup(new AppiumVirtualKeyboardActions(this));
_commandExecutor.AddCommandGroup(new AppiumSliderActions(this));
_commandExecutor.AddCommandGroup(new AppiumSwipeActions(this));
_commandExecutor.AddCommandGroup(new AppiumOrientationActions(this));
}

Просмотреть файл

@ -302,6 +302,49 @@ namespace UITest.Appium
});
}
/// <summary>
/// Sets the value of a Slider element that matches marked.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector of the Slider element to update.</param>
/// <param name="value">The value to set the Slider to.</param>
public static void SetSliderValue(this IApp app, string marked, double value)
{
var element = app.FindElement(marked);
double defaultMinimum = 0d;
double defaultMaximum = 1d;
app.CommandExecutor.Execute("setSliderValue", new Dictionary<string, object>
{
{ "element", element },
{ "value", value },
{ "minimum", defaultMinimum },
{ "maximum", defaultMaximum },
});
}
/// <summary>
/// Sets the value of a Slider element that matches marked.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector of the Slider element to update.</param>
/// <param name="value">The value to set the Slider to.</param>
/// <param name="minimum">Te minimum selectable value for the Slider.</param>
/// <param name="maximum">Te maximum selectable value for the Slider.</param>
public static void SetSliderValue(this IApp app, string marked, double value, double minimum = 0d,double maximum = 1d)
{
var element = app.FindElement(marked);
app.CommandExecutor.Execute("setSliderValue", new Dictionary<string, object>
{
{ "element", element },
{ "value", value },
{ "minimum", minimum },
{ "maximum", maximum },
});
}
static IUIElement Wait(Func<IUIElement> query,
Func<IUIElement, bool> satisfactory,
string? timeoutMessage = null,