Skip to content

Commit

Permalink
Fix the older macOS testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Jun 10, 2024
1 parent 3dc6b4d commit d0b3f84
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,29 @@ public void ActionSheetClickItem(Test.Alerts test, string itemText)
.Select(b => (Element: b, Text: b.GetText()))
.ToList();
CollectionAssert.IsNotEmpty(buttons);
ClassicAssert.True(buttons.Count == 5, $"Expected 5 buttons, found {buttons.Count}.");
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL");
ClassicAssert.True(buttons.Count >= 4 && buttons.Count <= 5, $"Expected 4 or 5 buttons, found {buttons.Count}.");
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();
// handle the case where the dismiss button is an actual button
if (buttons.Count == 5)
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL");

if (buttons.Count == 4 && itemText == "CANCEL")
{
// handle the case where the dismiss button is a "click outside the popup"

alert.DismissAlert();
}
else
{
// handle the case where the dismiss button is an actual button

var button = buttons.Single(b => b.Text == itemText);
button.Element.Click();
}

App.WaitForNoElement(() => App.GetAlert());

Expand Down
17 changes: 7 additions & 10 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumAppleAlertActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@ public abstract class AppiumAppleAlertActions : ICommandExecutionGroup
GetAlertButtonsCommand,
GetAlertTextCommand,
};
readonly AppiumApp _appiumApp;

protected readonly AppiumApp _appiumApp;

public AppiumAppleAlertActions(AppiumApp appiumApp)
{
_appiumApp = appiumApp;
}

public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public virtual bool IsCommandSupported(string commandName) =>
_commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);

public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
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);

Expand Down Expand Up @@ -73,7 +70,7 @@ CommandResponse GetAlertText(IDictionary<string, object> parameters)
return new CommandResponse(strings, CommandResponseResult.Success);
}

static AppiumElement? GetAppiumElement(object element) =>
protected static AppiumElement? GetAppiumElement(object element) =>
element switch
{
AppiumElement appiumElement => appiumElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,54 @@ public class AppiumCatalystAlertActions : AppiumAppleAlertActions
{
// Selects the inner "popover contents" of a popover window.
const string PossibleActionSheetXPath =
"//XCUIElementTypeWindow/XCUIElementTypePopover/XCUIElementTypeWindow/XCUIElementTypePopover";
"/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
Expand Down
12 changes: 12 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ public static IReadOnlyCollection<IUIElement> GetAlerts(this IApp app)
return (IReadOnlyCollection<IUIElement>?)result.Value ?? Array.Empty<IUIElement>();
}

/// <summary>
/// Dismisses the alert.
/// </summary>
/// <param name="alertElement">The element that represents the alert or action sheet.</param>
public static void DismissAlert(this IUIElement alertElement)
{
alertElement.Command.Execute("dismissAlert", new Dictionary<string, object>
{
["element"] = alertElement
});
}

/// <summary>
/// Return the buttons in the alert or action sheet.
/// </summary>
Expand Down

0 comments on commit d0b3f84

Please sign in to comment.