Skip to content

Commit d0b3f84

Browse files
committed
Fix the older macOS testing
1 parent 3dc6b4d commit d0b3f84

4 files changed

Lines changed: 79 additions & 15 deletions

File tree

src/Controls/tests/TestCases.Shared.Tests/Tests/Concepts/AlertsGalleryTests.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,29 @@ public void ActionSheetClickItem(Test.Alerts test, string itemText)
115115
.Select(b => (Element: b, Text: b.GetText()))
116116
.ToList();
117117
CollectionAssert.IsNotEmpty(buttons);
118-
ClassicAssert.True(buttons.Count == 5, $"Expected 5 buttons, found {buttons.Count}.");
119-
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL");
118+
ClassicAssert.True(buttons.Count >= 4 && buttons.Count <= 5, $"Expected 4 or 5 buttons, found {buttons.Count}.");
120119
CollectionAssert.Contains(buttons.Select(b => b.Text), "DESTROY");
121120
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 1");
122121
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 2");
123122
CollectionAssert.Contains(buttons.Select(b => b.Text), "ITEM 3");
124123

125-
var button = buttons.Single(b => b.Text == itemText);
126-
button.Element.Click();
124+
// handle the case where the dismiss button is an actual button
125+
if (buttons.Count == 5)
126+
CollectionAssert.Contains(buttons.Select(b => b.Text), "CANCEL");
127+
128+
if (buttons.Count == 4 && itemText == "CANCEL")
129+
{
130+
// handle the case where the dismiss button is a "click outside the popup"
131+
132+
alert.DismissAlert();
133+
}
134+
else
135+
{
136+
// handle the case where the dismiss button is an actual button
137+
138+
var button = buttons.Single(b => b.Text == itemText);
139+
button.Element.Click();
140+
}
127141

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

src/TestUtils/src/UITest.Appium/Actions/AppiumAppleAlertActions.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,25 @@ public abstract class AppiumAppleAlertActions : ICommandExecutionGroup
1515
GetAlertButtonsCommand,
1616
GetAlertTextCommand,
1717
};
18-
readonly AppiumApp _appiumApp;
18+
19+
protected readonly AppiumApp _appiumApp;
1920

2021
public AppiumAppleAlertActions(AppiumApp appiumApp)
2122
{
2223
_appiumApp = appiumApp;
2324
}
2425

25-
public bool IsCommandSupported(string commandName)
26-
{
27-
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
28-
}
26+
public virtual bool IsCommandSupported(string commandName) =>
27+
_commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
2928

30-
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
31-
{
32-
return commandName switch
29+
public virtual CommandResponse Execute(string commandName, IDictionary<string, object> parameters) =>
30+
commandName switch
3331
{
3432
GetAlertsCommand => GetAlerts(parameters),
3533
GetAlertButtonsCommand => GetAlertButtons(parameters),
3634
GetAlertTextCommand => GetAlertText(parameters),
3735
_ => CommandResponse.FailedEmptyResponse,
3836
};
39-
}
4037

4138
protected abstract IReadOnlyCollection<IUIElement> OnGetAlerts(AppiumApp appiumApp, IDictionary<string, object> parameters);
4239

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

76-
static AppiumElement? GetAppiumElement(object element) =>
73+
protected static AppiumElement? GetAppiumElement(object element) =>
7774
element switch
7875
{
7976
AppiumElement appiumElement => appiumElement,

src/TestUtils/src/UITest.Appium/Actions/AppiumCatalystAlertActions.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,54 @@ public class AppiumCatalystAlertActions : AppiumAppleAlertActions
66
{
77
// Selects the inner "popover contents" of a popover window.
88
const string PossibleActionSheetXPath =
9-
"//XCUIElementTypeWindow/XCUIElementTypePopover/XCUIElementTypeWindow/XCUIElementTypePopover";
9+
"/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypePopover";
10+
11+
const string DismissAlertCommand = "dismissAlert";
12+
13+
readonly List<string> _commands = new()
14+
{
15+
DismissAlertCommand,
16+
};
1017

1118
public AppiumCatalystAlertActions(AppiumApp appiumApp)
1219
: base(appiumApp)
1320
{
1421
}
1522

23+
public override bool IsCommandSupported(string commandName) =>
24+
_commands.Contains(commandName, StringComparer.OrdinalIgnoreCase) || base.IsCommandSupported(commandName);
25+
26+
public override CommandResponse Execute(string commandName, IDictionary<string, object> parameters) =>
27+
commandName switch
28+
{
29+
DismissAlertCommand => DismissAlert(parameters),
30+
_ => base.Execute(commandName, parameters),
31+
};
32+
33+
CommandResponse DismissAlert(IDictionary<string, object> parameters)
34+
{
35+
var alert = GetAppiumElement(parameters["element"]);
36+
if (alert is null)
37+
return CommandResponse.FailedEmptyResponse;
38+
39+
// XCUIElementTypePopover == 18
40+
if (!"18".Equals(alert.GetAttribute("elementType"), StringComparison.OrdinalIgnoreCase))
41+
return CommandResponse.FailedEmptyResponse;
42+
43+
var dismissRegions = AppiumQuery.ById("PopoverDismissRegion").FindElements(_appiumApp).ToList();
44+
for (var i = dismissRegions.Count - 1; i >= 0; i--)
45+
{
46+
var region = GetAppiumElement(dismissRegions[i])!;
47+
if ("true".Equals(region.GetAttribute("enabled"), StringComparison.OrdinalIgnoreCase))
48+
{
49+
region.Click();
50+
return CommandResponse.SuccessEmptyResponse;
51+
}
52+
}
53+
54+
return CommandResponse.FailedEmptyResponse;
55+
}
56+
1657
protected override IReadOnlyCollection<IUIElement> OnGetAlerts(AppiumApp appiumApp, IDictionary<string, object> parameters)
1758
{
1859
// Catalyst uses action sheets for alerts and macOS 14

src/TestUtils/src/UITest.Appium/HelperExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ public static IReadOnlyCollection<IUIElement> GetAlerts(this IApp app)
327327
return (IReadOnlyCollection<IUIElement>?)result.Value ?? Array.Empty<IUIElement>();
328328
}
329329

330+
/// <summary>
331+
/// Dismisses the alert.
332+
/// </summary>
333+
/// <param name="alertElement">The element that represents the alert or action sheet.</param>
334+
public static void DismissAlert(this IUIElement alertElement)
335+
{
336+
alertElement.Command.Execute("dismissAlert", new Dictionary<string, object>
337+
{
338+
["element"] = alertElement
339+
});
340+
}
341+
330342
/// <summary>
331343
/// Return the buttons in the alert or action sheet.
332344
/// </summary>

0 commit comments

Comments
 (0)