generated from jfversluis/Plugin.Maui.Feature
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9fcfb1
commit 2d6ffef
Showing
10 changed files
with
359 additions
and
4 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/Plugin.Maui.UITestHelpers.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,92 @@ | ||
using OpenQA.Selenium.Appium; | ||
using Plugin.Maui.UITestHelpers.Core; | ||
|
||
namespace Plugin.Maui.UITestHelpers.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 | ||
}; | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Plugin.Maui.UITestHelpers.Appium/Actions/AppiumAppleAlertActions.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,80 @@ | ||
using OpenQA.Selenium.Appium; | ||
using Plugin.Maui.UITestHelpers.Core; | ||
|
||
namespace Plugin.Maui.UITestHelpers.Appium; | ||
|
||
public abstract class AppiumAppleAlertActions : ICommandExecutionGroup | ||
{ | ||
const string GetAlertsCommand = "getAlerts"; | ||
const string GetAlertButtonsCommand = "getAlertButtons"; | ||
const string GetAlertTextCommand = "getAlertText"; | ||
|
||
readonly List<string> _commands = new() | ||
{ | ||
GetAlertsCommand, | ||
GetAlertButtonsCommand, | ||
GetAlertTextCommand, | ||
}; | ||
|
||
protected readonly AppiumApp _appiumApp; | ||
|
||
public AppiumAppleAlertActions(AppiumApp appiumApp) | ||
{ | ||
_appiumApp = appiumApp; | ||
} | ||
|
||
public virtual bool IsCommandSupported(string commandName) => | ||
_commands.Contains(commandName, StringComparer.OrdinalIgnoreCase); | ||
|
||
public virtual CommandResponse Execute(string commandName, IDictionary<string, object> parameters) => | ||
commandName switch | ||
{ | ||
GetAlertsCommand => GetAlerts(parameters), | ||
GetAlertButtonsCommand => GetAlertButtons(parameters), | ||
GetAlertTextCommand => GetAlertText(parameters), | ||
_ => CommandResponse.FailedEmptyResponse, | ||
}; | ||
|
||
protected abstract IReadOnlyCollection<IUIElement> OnGetAlerts(AppiumApp appiumApp, IDictionary<string, object> parameters); | ||
|
||
CommandResponse GetAlerts(IDictionary<string, object> parameters) | ||
{ | ||
var alerts = OnGetAlerts(_appiumApp, parameters); | ||
|
||
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 buttons = AppiumQuery.ByClass("XCUIElementTypeButton").FindElements(alert, _appiumApp); | ||
|
||
return new CommandResponse(buttons, CommandResponseResult.Success); | ||
} | ||
|
||
CommandResponse GetAlertText(IDictionary<string, object> parameters) | ||
{ | ||
var alert = GetAppiumElement(parameters["element"]); | ||
if (alert is null) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
var text = AppiumQuery.ByClass("XCUIElementTypeStaticText").FindElements(alert, _appiumApp); | ||
var strings = text.Select(t => t.GetText()).ToList(); | ||
|
||
return new CommandResponse(strings, CommandResponseResult.Success); | ||
} | ||
|
||
protected static AppiumElement? GetAppiumElement(object element) => | ||
element switch | ||
{ | ||
AppiumElement appiumElement => appiumElement, | ||
AppiumDriverElement driverElement => driverElement.AppiumElement, | ||
_ => null | ||
}; | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Plugin.Maui.UITestHelpers.Appium/Actions/AppiumCatalystAlertActions.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,68 @@ | ||
using Plugin.Maui.UITestHelpers.Core; | ||
|
||
namespace Plugin.Maui.UITestHelpers.Appium; | ||
|
||
public class AppiumCatalystAlertActions : AppiumAppleAlertActions | ||
{ | ||
// Selects the inner "popover contents" of a popover window. | ||
const string PossibleActionSheetXPath = | ||
"/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypePopover"; | ||
|
||
const string DismissAlertCommand = "dismissAlert"; | ||
|
||
readonly List<string> _commands = new() | ||
{ | ||
DismissAlertCommand, | ||
}; | ||
|
||
public AppiumCatalystAlertActions(AppiumApp appiumApp) | ||
: base(appiumApp) | ||
{ | ||
} | ||
|
||
public override bool IsCommandSupported(string commandName) => | ||
_commands.Contains(commandName, StringComparer.OrdinalIgnoreCase) || base.IsCommandSupported(commandName); | ||
|
||
public override CommandResponse Execute(string commandName, IDictionary<string, object> parameters) => | ||
commandName switch | ||
{ | ||
DismissAlertCommand => DismissAlert(parameters), | ||
_ => base.Execute(commandName, parameters), | ||
}; | ||
|
||
CommandResponse DismissAlert(IDictionary<string, object> parameters) | ||
{ | ||
var alert = GetAppiumElement(parameters["element"]); | ||
if (alert is null) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
// XCUIElementTypePopover == 18 | ||
if (!"18".Equals(alert.GetAttribute("elementType"), StringComparison.OrdinalIgnoreCase)) | ||
return CommandResponse.FailedEmptyResponse; | ||
|
||
var dismissRegions = AppiumQuery.ById("PopoverDismissRegion").FindElements(_appiumApp).ToList(); | ||
for (var i = dismissRegions.Count - 1; i >= 0; i--) | ||
{ | ||
var region = GetAppiumElement(dismissRegions[i])!; | ||
if ("true".Equals(region.GetAttribute("enabled"), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
region.Click(); | ||
return CommandResponse.SuccessEmptyResponse; | ||
} | ||
} | ||
|
||
return CommandResponse.FailedEmptyResponse; | ||
} | ||
|
||
protected override IReadOnlyCollection<IUIElement> OnGetAlerts(AppiumApp appiumApp, IDictionary<string, object> parameters) | ||
{ | ||
// Catalyst uses action sheets for alerts and macOS 14 | ||
var alerts = appiumApp.FindElements(AppiumQuery.ByClass("XCUIElementTypeSheet")); | ||
|
||
// But it also uses popovers for action sheets on macOS 13 | ||
if (alerts is null || alerts.Count == 0) | ||
alerts = appiumApp.FindElements(AppiumQuery.ByXPath(PossibleActionSheetXPath)); | ||
|
||
return alerts; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Plugin.Maui.UITestHelpers.Appium/Actions/AppiumIOSAlertActions.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,29 @@ | ||
using Plugin.Maui.UITestHelpers.Core; | ||
|
||
namespace Plugin.Maui.UITestHelpers.Appium; | ||
|
||
public class AppiumIOSAlertActions : AppiumAppleAlertActions | ||
{ | ||
// Selects VISIBLE "Other" elements that are the direct child of | ||
// a VISIBLE window AND are OVERLAYED on top of the first window. | ||
const string PossibleAlertXPath = | ||
"//XCUIElementTypeWindow[@visible='true']/XCUIElementTypeOther[@visible='true' and @index > 0]"; | ||
|
||
public AppiumIOSAlertActions(AppiumApp appiumApp) | ||
: base(appiumApp) | ||
{ | ||
} | ||
|
||
protected override IReadOnlyCollection<IUIElement> OnGetAlerts(AppiumApp appiumApp, IDictionary<string, object> parameters) | ||
{ | ||
// First try the type used on iOS. | ||
var alerts = appiumApp.FindElements(AppiumQuery.ByClass("XCUIElementTypeAlert")); | ||
|
||
// It appears iOS sometimes uses the XCUIElementTypeOther class for action sheets | ||
// so we need a way to do a more fuzzy check. | ||
if (alerts is null || alerts.Count == 0) | ||
alerts = appiumApp.FindElements(AppiumQuery.ByXPath(PossibleAlertXPath)); | ||
|
||
return alerts; | ||
} | ||
} |
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
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
Oops, something went wrong.