From f6de0e1a6fcae87f2dde39cde572fa100fd91b70 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 14 Feb 2025 09:25:20 +0000 Subject: [PATCH] helper methods added to visible elements' detection --- .../HelperExtensions.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs b/src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs index 660ac6d..190ce98 100644 --- a/src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs +++ b/src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs @@ -1665,5 +1665,47 @@ static void WaitForNone(Func query, { Wait(query, i => i == null, timeoutMessage, timeout, retryFrequency); } + + /// + /// Assert an element exists and is visible. + /// + public static bool DoesElementExist(this IApp app, string automationId) + { + var exists = false; + + var elements = app.FindElements(automationId); + exists = elements.Count > 0; + + return exists && elements.First().IsDisplayed(); + } + + /// + /// Assert an element found by text exists and is visible. + /// + public static IUIElement? FindVisibleElementByText(this IApp app, string text, int timeoutInSeconds = 3) + { + string beforeMessage = $"Find visible element by text: '{text}' - {DateTime.Now.ToString("HH:mm:ss")}"; + Console.WriteLine(beforeMessage); + + var timeout = TimeSpan.FromSeconds(timeoutInSeconds); + var stopwatch = Stopwatch.StartNew(); + + while (stopwatch.Elapsed < timeout) + { + var elementIsVisible = app.FindElementByText(text)?.IsDisplayed(); + if (elementIsVisible is not null && elementIsVisible.Value) + { + var element = app.FindElementByText(text); + string afterMessage = $"Found visible element by text: {text} - {DateTime.Now.ToString("HH:mm:ss")}"; + Console.WriteLine(afterMessage); + return element; + } + Thread.Sleep(100); + } + stopwatch.Stop(); + string notVisibleMessage = $"Element with text: {text} not visible within timeout - {DateTime.Now.ToString("HH:mm:ss")}"; + Console.WriteLine(notVisibleMessage); + return null; + } } } \ No newline at end of file