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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
54 changes: 44 additions & 10 deletions
54
src/Controls/samples/Controls.Sample.UITests/Elements/SliderCoreGalleryPage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/TestUtils/src/UITest.Appium/Actions/AppiumSliderActions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.