Skip to content

Commit dfcfda2

Browse files
committed
added WaitForAndLog and WaitForAndTap ext methods
1 parent 69d1bc2 commit dfcfda2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,62 @@ public static bool WaitForTextToBePresentInElement(this IApp app, string automat
678678
}
679679
}
680680

681+
public static IUIElement? WaitForAndLog(this IApp app, string elementId, TimeSpan? timeout = null)
682+
{
683+
string beforeMessage = $"Waiting for {elementId} - {DateTime.Now.ToString("HH:mm:ss")}";
684+
Console.WriteLine(beforeMessage);
685+
var result = app.WaitForElement(elementId, beforeMessage, timeout);
686+
string afterMessage = $"Found {elementId} - {DateTime.Now.ToString("HH:mm:ss")}";
687+
Console.WriteLine(afterMessage);
688+
try
689+
{
690+
if (result is not null && result.IsDisplayed())
691+
{
692+
Console.WriteLine($"{elementId} was visible");
693+
return (IUIElement?)result;
694+
}
695+
else
696+
{
697+
Console.WriteLine($"{elementId} was not visible");
698+
return null;
699+
}
700+
}
701+
catch (Exception ex)
702+
{
703+
Console.WriteLine(ex.InnerException?.Message.ToString());
704+
Console.WriteLine($"{elementId} was not visible and threw an exception");
705+
return null;
706+
}
707+
}
708+
709+
/// <summary>
710+
/// Method that waits for element to be visible, selects element that is visible then taps.
711+
/// </summary>
712+
/// <param name="automationId"></param>
713+
/// <param name="delay"></param>
714+
public static void WaitForAndTap(this IApp app, string automationId, int? delay = null)
715+
{
716+
var element = WaitForAndLog(app, automationId);
717+
718+
if (element is null)
719+
{
720+
string nullElement = $"Element with id '{automationId} was null' - {DateTime.Now.ToString("HH:mm:ss")}";
721+
Console.WriteLine(nullElement);
722+
throw new NullReferenceException();
723+
}
724+
string beforeMessage = $"Will tap element: '{automationId}' - {DateTime.Now.ToString("HH:mm:ss")}";
725+
Console.WriteLine(beforeMessage);
726+
element.Tap();
727+
string afterMessage = $"Tapped element: ' {automationId}' - {DateTime.Now.ToString("HH:mm:ss")}";
728+
Console.WriteLine(afterMessage);
729+
if (delay is not null)
730+
{
731+
Task.Delay(delay.Value).Wait();
732+
string delayMessage = $"Delayed after tapping element: ' {automationId}' - {DateTime.Now.ToString("HH:mm:ss")}";
733+
Console.WriteLine(delayMessage);
734+
}
735+
}
736+
681737
/// <summary>
682738
/// Presses the volume up button on the device.
683739
/// </summary>

0 commit comments

Comments
 (0)