-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
507 additions
and
5 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/Controls/samples/Controls.Sample.UITests/Concepts/AlertsGalleryPage.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,96 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.Controls; | ||
|
||
namespace Maui.Controls.Sample | ||
{ | ||
internal class AlertsGalleryPage : CoreGalleryBasePage | ||
{ | ||
protected override void Build() | ||
{ | ||
// ALERTS | ||
|
||
// Test with a single button alert that can be dismissed by tapping the button | ||
Add(Test.Alerts.AlertCancel, async t => | ||
{ | ||
await DisplayAlert( | ||
"Alert Title Here", | ||
"Alert Message Here", | ||
"CANCEL"); | ||
t.ReportSuccessEvent(); | ||
}); | ||
|
||
// Test alert with options to Accept or Cancel, Accept is the correct option | ||
Add(Test.Alerts.AlertAcceptCancelClickAccept, async t => | ||
{ | ||
var result = await DisplayAlert( | ||
"Alert Title Here", | ||
"Alert Message Here", | ||
"ACCEPT", "CANCEL"); | ||
if (result) | ||
t.ReportSuccessEvent(); | ||
else | ||
t.ReportFailEvent(); | ||
}); | ||
|
||
// Test alert with options to Accept or Cancel, Cancel is the correct option | ||
Add(Test.Alerts.AlertAcceptCancelClickCancel, async t => | ||
{ | ||
var result = await DisplayAlert( | ||
"Alert Title Here", | ||
"Alert Message Here", | ||
"ACCEPT", "CANCEL"); | ||
if (result) | ||
t.ReportFailEvent(); | ||
else | ||
t.ReportSuccessEvent(); | ||
}); | ||
|
||
// ACTION SHEETS | ||
|
||
// Test action sheet with items and Cancel, Item 2 is the correct option | ||
Add(Test.Alerts.ActionSheetClickItem, async t => | ||
{ | ||
var result = await DisplayActionSheet( | ||
"Action Sheet Title Here", | ||
"CANCEL", "DESTROY", | ||
"ITEM 1", "ITEM 2", "ITEM 3"); | ||
if (result == "ITEM 2") | ||
t.ReportSuccessEvent(); | ||
else | ||
t.ReportFailEvent(); | ||
}); | ||
|
||
// Test action sheet with items and Cancel, Cancel is the correct option | ||
Add(Test.Alerts.ActionSheetClickCancel, async t => | ||
{ | ||
var result = await DisplayActionSheet( | ||
"Action Sheet Title Here", | ||
"CANCEL", "DESTROY", | ||
"ITEM 1", "ITEM 2", "ITEM 3"); | ||
if (result == "CANCEL") | ||
t.ReportSuccessEvent(); | ||
else | ||
t.ReportFailEvent(); | ||
}); | ||
|
||
// Test action sheet with items and Cancel, Destroy is the correct option | ||
Add(Test.Alerts.ActionSheetClickDestroy, async t => | ||
{ | ||
var result = await DisplayActionSheet( | ||
"Action Sheet Title Here", | ||
"CANCEL", "DESTROY", | ||
"ITEM 1", "ITEM 2", "ITEM 3"); | ||
if (result == "DESTROY") | ||
t.ReportSuccessEvent(); | ||
else | ||
t.ReportFailEvent(); | ||
}); | ||
} | ||
|
||
ExpectedEventViewContainer<Button> | ||
Add(Test.Alerts test, Func<ExpectedEventViewContainer<Button>, Task> action) => | ||
Add(new ExpectedEventViewContainer<Button>(test, new Button { Text = "Click Me!" })) | ||
.With(t => t.View.Clicked += (_, _) => action(t)); | ||
} | ||
} |
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
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
142 changes: 142 additions & 0 deletions
142
src/Controls/tests/UITests/Tests/Concepts/AlertsGalleryTests.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,142 @@ | ||
using Maui.Controls.Sample; | ||
using NUnit.Framework; | ||
using OpenQA.Selenium.Appium; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.AppiumTests | ||
{ | ||
public class AlertsGalleryTests : CoreGalleryBasePageTest | ||
{ | ||
public AlertsGalleryTests(TestDevice device) | ||
: base(device) | ||
{ | ||
} | ||
|
||
protected override void NavigateToGallery() | ||
{ | ||
App.NavigateToGallery("Alerts Gallery"); | ||
} | ||
|
||
[Test] | ||
public void AlertCancel() | ||
{ | ||
if (Device == TestDevice.Windows) | ||
Assert.Ignore("UI testing alert code is not yet implemented on Windows."); | ||
|
||
var test = Test.Alerts.AlertCancel; | ||
|
||
var remote = new EventViewContainerRemote(UITestContext, test); | ||
remote.GoTo(test.ToString()); | ||
|
||
var textBeforeClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (none)", textBeforeClick); | ||
|
||
remote.TapView(); | ||
|
||
var alert = App.WaitForElement(() => App.GetAlert()); | ||
Assert.NotNull(alert); | ||
|
||
var alertText = alert.GetAlertText(); | ||
CollectionAssert.Contains(alertText, "Alert Title Here"); | ||
CollectionAssert.Contains(alertText, "Alert Message Here"); | ||
|
||
var buttons = alert.GetAlertButtons(); | ||
CollectionAssert.IsNotEmpty(buttons); | ||
Assert.True(buttons.Count == 1, $"Expected 1 buttonText, found {buttons.Count}."); | ||
|
||
var cancel = buttons.First(); | ||
Assert.AreEqual("CANCEL", cancel.GetText()); | ||
|
||
cancel.Click(); | ||
|
||
App.WaitForNoElement(() => App.GetAlert()); | ||
|
||
var textAfterClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (SUCCESS 1)", textAfterClick); | ||
} | ||
|
||
[Test] | ||
[TestCase(Test.Alerts.AlertAcceptCancelClickAccept, "ACCEPT")] | ||
[TestCase(Test.Alerts.AlertAcceptCancelClickCancel, "CANCEL")] | ||
public void AlertAcceptCancel(Test.Alerts test, string buttonText) | ||
{ | ||
if (Device == TestDevice.Windows) | ||
Assert.Ignore("UI testing alert code is not yet implemented on Windows."); | ||
|
||
var remote = new EventViewContainerRemote(UITestContext, test); | ||
remote.GoTo(test.ToString()); | ||
|
||
var textBeforeClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (none)", textBeforeClick); | ||
|
||
remote.TapView(); | ||
|
||
var alert = App.WaitForElement(() => App.GetAlert()); | ||
Assert.NotNull(alert); | ||
|
||
var alertText = alert.GetAlertText(); | ||
CollectionAssert.Contains(alertText, "Alert Title Here"); | ||
CollectionAssert.Contains(alertText, "Alert Message Here"); | ||
|
||
var buttons = alert.GetAlertButtons() | ||
.Select(b => (Element: b, Text: b.GetText())) | ||
.ToList(); | ||
CollectionAssert.IsNotEmpty(buttons); | ||
Assert.True(buttons.Count == 2, $"Expected 2 buttons, found {buttons.Count}."); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "ACCEPT"); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL"); | ||
|
||
var button = buttons.Single(b => b.Text == buttonText); | ||
button.Element.Click(); | ||
|
||
App.WaitForNoElement(() => App.GetAlert()); | ||
|
||
var textAfterClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (SUCCESS 1)", textAfterClick); | ||
} | ||
|
||
[Test] | ||
[TestCase(Test.Alerts.ActionSheetClickItem, "ITEM 2")] | ||
[TestCase(Test.Alerts.ActionSheetClickCancel, "CANCEL")] | ||
[TestCase(Test.Alerts.ActionSheetClickDestroy, "DESTROY")] | ||
public void ActionSheetClickItem(Test.Alerts test, string itemText) | ||
{ | ||
if (Device == TestDevice.Windows) | ||
Assert.Ignore("UI testing alert code is not yet implemented on Windows."); | ||
|
||
var remote = new EventViewContainerRemote(UITestContext, test); | ||
remote.GoTo(test.ToString()); | ||
|
||
var textBeforeClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (none)", textBeforeClick); | ||
|
||
remote.TapView(); | ||
|
||
var alert = App.WaitForElement(() => App.GetAlert()); | ||
Assert.NotNull(alert); | ||
|
||
var alertText = alert.GetAlertText(); | ||
CollectionAssert.Contains(alertText, "Action Sheet Title Here"); | ||
|
||
var buttons = alert.GetAlertButtons() | ||
.Select(b => (Element: b, Text: b.GetText())) | ||
.ToList(); | ||
CollectionAssert.IsNotEmpty(buttons); | ||
Assert.True(buttons.Count == 5, $"Expected 5 buttons, found {buttons.Count}."); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL"); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "DESTROY"); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 1"); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 2"); | ||
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 3"); | ||
|
||
var button = buttons.Single(b => b.Text == itemText); | ||
button.Element.Click(); | ||
|
||
App.WaitForNoElement(() => App.GetAlert()); | ||
|
||
var textAfterClick = remote.GetEventLabel().GetText(); | ||
Assert.AreEqual($"Event: {test} (SUCCESS 1)", textAfterClick); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/TestUtils/src/UITest.Appium/Actions/AppiumAndroidAlertActions.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,95 @@ | ||
using System.Collections.ObjectModel; | ||
using OpenQA.Selenium; | ||
using OpenQA.Selenium.Appium; | ||
using UITest.Core; | ||
|
||
namespace UITest.Appium | ||
{ | ||
public class AppiumAndroidAlertActions : ICommandExecutionGroup | ||
{ | ||
const string GetAlertsCommand = "getAlerts"; | ||
const string GetAlertButtonsCommand = "getAlertButtons"; | ||
const string GetAlertTextCommand = "getAlertText"; | ||
|
||
readonly List<string> _commands = new() | ||
{ | ||
GetAlertsCommand, | ||
GetAlertButtonsCommand, | ||
GetAlertTextCommand, | ||
}; | ||
readonly AppiumApp _appiumApp; | ||
|
||
public AppiumAndroidAlertActions(AppiumApp appiumApp) | ||
{ | ||
_appiumApp = appiumApp; | ||
} | ||
|
||
public bool IsCommandSupported(string commandName) | ||
{ | ||
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters) | ||
{ | ||
return commandName switch | ||
{ | ||
GetAlertsCommand => GetAlerts(parameters), | ||
GetAlertButtonsCommand => GetAlertButtons(parameters), | ||
GetAlertTextCommand => GetAlertText(parameters), | ||
_ => CommandResponse.FailedEmptyResponse, | ||
}; | ||
} | ||
|
||
CommandResponse GetAlerts(IDictionary<string, object> parameters) | ||
{ | ||
var alerts = _appiumApp.Query.ById("parentPanel"); | ||
|
||
if (alerts is null || alerts.Count == 0) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
return new CommandResponse(alerts, CommandResponseResult.Success); | ||
} | ||
|
||
CommandResponse GetAlertButtons(IDictionary<string, object> parameters) | ||
{ | ||
var alert = GetAppiumElement(parameters["element"]); | ||
if (alert is null) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
var items = AppiumQuery.ByClass("android.widget.ListView") | ||
.FindElements(alert, _appiumApp) | ||
.FirstOrDefault() | ||
?.ByClass("android.widget.TextView"); | ||
|
||
var buttons = AppiumQuery.ByClass("android.widget.Button") | ||
.FindElements(alert, _appiumApp); | ||
|
||
var all = new List<IUIElement>(); | ||
if (items is not null) | ||
all.AddRange(items); | ||
all.AddRange(buttons); | ||
|
||
return new CommandResponse(all, CommandResponseResult.Success); | ||
} | ||
|
||
CommandResponse GetAlertText(IDictionary<string, object> parameters) | ||
{ | ||
var alert = GetAppiumElement(parameters["element"]); | ||
if (alert is null) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
var text = AppiumQuery.ByClass("android.widget.TextView").FindElements(alert, _appiumApp); | ||
var strings = text.Select(t => t.GetText()).ToList(); | ||
|
||
return new CommandResponse(strings, CommandResponseResult.Success); | ||
} | ||
|
||
static AppiumElement? GetAppiumElement(object element) => | ||
element switch | ||
{ | ||
AppiumElement appiumElement => appiumElement, | ||
AppiumDriverElement driverElement => driverElement.AppiumElement, | ||
_ => null | ||
}; | ||
} | ||
} |
Oops, something went wrong.