diff --git a/Microsoft.Maui.Automation.Core/ApplicationExtensions.cs b/Microsoft.Maui.Automation.Core/ApplicationExtensions.cs index d39277e..288b0a7 100644 --- a/Microsoft.Maui.Automation.Core/ApplicationExtensions.cs +++ b/Microsoft.Maui.Automation.Core/ApplicationExtensions.cs @@ -9,11 +9,17 @@ namespace Microsoft.Maui.Automation public static class ApiExtensions { - //public static async Task ById(IApplication app, string id) - //{ + public static IAsyncEnumerable By(IWindow window, params IViewSelector[] selectors) + => All(window.Application!, window.Id, selectors); - //} + public static IAsyncEnumerable By(IWindow window, IViewSelector selector) + => All(window.Application!, window.Id, selector); + public static IAsyncEnumerable ByAutomationId(IWindow window, string automationId, StringComparison comparison = StringComparison.Ordinal) + => By(window, new AutomationIdSelector(automationId, comparison)); + + public static IAsyncEnumerable ById(IWindow window, string id, StringComparison comparison = StringComparison.Ordinal) + => By(window, new IdSelector(id, comparison)); public static async IAsyncEnumerable All(IApplication app, params IViewSelector[] selectors) { @@ -21,22 +27,52 @@ namespace Microsoft.Maui.Automation foreach (var window in windows) { - foreach (var view in await All(app, window, selectors)) + await foreach (var view in All(app, window.Id, selectors)) yield return view; } } - public static Task All(IApplication app, IWindow window, params IViewSelector[] selectors) + public static async IAsyncEnumerable Any(IApplication app, params IViewSelector[] selectors) + { + var windows = await app.Windows(); + + foreach (var window in windows) + { + await foreach (var view in Any(app, window.Id, selectors)) + yield return view; + } + } + + public static IAsyncEnumerable All(IApplication app, IWindow window, params IViewSelector[] selectors) => All(app, window.Id, selectors); - public static async Task All(IApplication app, string windowId, params IViewSelector[] selectors) + public static async IAsyncEnumerable All(IApplication app, string windowId, params IViewSelector[] selectors) { - var window = await app.Window(windowId); + await foreach(var view in app.Descendants(windowId, viewId: null, new CompoundSelector(any: false, selectors))) + { + yield return view; + } + } - if (window != null) - return await All(app, window, selectors); + public static IAsyncEnumerable Any(IApplication app, IWindow window, params IViewSelector[] selectors) + => Any(app, window.Id, selectors); - return Array.Empty(); + public static async IAsyncEnumerable Any(IApplication app, string windowId, params IViewSelector[] selectors) + { + await foreach (var view in app.Descendants(windowId, viewId: null, new CompoundSelector(any: true, selectors))) + { + yield return view; + } + } + + public static async Task First(IApplication app, string windowId, bool matchesAnySelector, params IViewSelector[] selectors) + { + await foreach (var view in app.Descendants(windowId, viewId: null, new CompoundSelector(any: matchesAnySelector, selectors))) + { + return view; + } + + return null; } } }