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 SetSliderValue method to UITest #19182

Merged
merged 4 commits into from Dec 5, 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
@@ -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;
public SliderCoreGalleryPage()
{
var layout = new StackLayout
{
Padding = new Microsoft.Maui.Thickness(12)
};

protected override bool SupportsTapGestureRecognizer => false;
// Default
var defaultLabel = new Label { AutomationId = "DefaultSlider", Text = "Default" };
var defaultSlider = new Slider();
layout.Add(defaultLabel);
layout.Add(defaultSlider);

protected override void InitializeElement(Slider element)
{
}
// BackgroundColor
var backgroundColorLabel = new Label { Text = "BackgroundColor" };
var backgroundColorSlider = new Slider { AutomationId = "BackgroundColorSlider", BackgroundColor = Colors.Red };
layout.Add(backgroundColorLabel);
layout.Add(backgroundColorSlider);

protected override void Build()
{
base.Build();
// 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;
}
}
}
50 changes: 50 additions & 0 deletions src/Controls/tests/UITests/Tests/SliderUITests.cs
@@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uitest-slider

{
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");
}
}
}
88 changes: 88 additions & 0 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumSliderActions.cs
@@ -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();
}
}
}
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 AppiumSliderActions(this));
_commandExecutor.AddCommandGroup(new AppiumSwipeActions(this));
_commandExecutor.AddCommandGroup(new AppiumOrientationActions(this));
}
Expand Down
43 changes: 43 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Expand Up @@ -284,6 +284,49 @@ public static void TapCoordinates(this IApp app, float x, float y)
});
}

/// <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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
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,
Expand Down