Skip to content

Commit f6de0e1

Browse files
committed
helper methods added to visible elements' detection
1 parent 69d1bc2 commit f6de0e1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs

+42
Original file line numberDiff line numberDiff line change
@@ -1665,5 +1665,47 @@ static void WaitForNone(Func<IUIElement> query,
16651665
{
16661666
Wait(query, i => i == null, timeoutMessage, timeout, retryFrequency);
16671667
}
1668+
1669+
/// <summary>
1670+
/// Assert an element exists and is visible.
1671+
/// </summary>
1672+
public static bool DoesElementExist(this IApp app, string automationId)
1673+
{
1674+
var exists = false;
1675+
1676+
var elements = app.FindElements(automationId);
1677+
exists = elements.Count > 0;
1678+
1679+
return exists && elements.First().IsDisplayed();
1680+
}
1681+
1682+
/// <summary>
1683+
/// Assert an element found by text exists and is visible.
1684+
/// </summary>
1685+
public static IUIElement? FindVisibleElementByText(this IApp app, string text, int timeoutInSeconds = 3)
1686+
{
1687+
string beforeMessage = $"Find visible element by text: '{text}' - {DateTime.Now.ToString("HH:mm:ss")}";
1688+
Console.WriteLine(beforeMessage);
1689+
1690+
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
1691+
var stopwatch = Stopwatch.StartNew();
1692+
1693+
while (stopwatch.Elapsed < timeout)
1694+
{
1695+
var elementIsVisible = app.FindElementByText(text)?.IsDisplayed();
1696+
if (elementIsVisible is not null && elementIsVisible.Value)
1697+
{
1698+
var element = app.FindElementByText(text);
1699+
string afterMessage = $"Found visible element by text: {text} - {DateTime.Now.ToString("HH:mm:ss")}";
1700+
Console.WriteLine(afterMessage);
1701+
return element;
1702+
}
1703+
Thread.Sleep(100);
1704+
}
1705+
stopwatch.Stop();
1706+
string notVisibleMessage = $"Element with text: {text} not visible within timeout - {DateTime.Now.ToString("HH:mm:ss")}";
1707+
Console.WriteLine(notVisibleMessage);
1708+
return null;
1709+
}
16681710
}
16691711
}

0 commit comments

Comments
 (0)