Skip to content

Added more tap specific extension methods to the HelperExtensions class #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Plugin.Maui.UITestHelpers.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Interfaces;
using OpenQA.Selenium.Interactions;
using Plugin.Maui.UITestHelpers.Core;

namespace Plugin.Maui.UITestHelpers.Appium
Expand Down Expand Up @@ -510,6 +511,55 @@ public static void DismissAlert(this IUIElement alertElement)
});
}

/// <summary>
/// Tap then wait a short period of time for other elements to load.
/// /// <param name="delayInMilliseconds">Amount of time to wait in milliseconds.</param>
/// </summary>
public static void TapWithDelay(this IUIElement element, int delayInMilliseconds = 500)
{
element.Tap();
Task.Delay(delayInMilliseconds).Wait();
}

/// <summary>
/// Tap an element by its text.
/// </summary>
public static void TapByText(this IApp app, string text)
{
var driver = (app as AppiumApp)?.Driver;
if (driver == null)
{
throw new InvalidOperationException($"Driver was null");
}

// Find the element by XPath
var element = (app.FindElementByText(text) as AppiumDriverElement)?.AppiumElement;
if (element == null)
{
throw new EntryPointNotFoundException($"Element with XPath '{text}' not found");
}

// Get the element's size and location
var elementLocation = element.Location;
var elementSize = element.Size;

// Calculate the center of the element
int centerX = elementLocation.X + (elementSize.Width / 2);
int centerY = elementLocation.Y + (elementSize.Height / 2);

// Create W3C actions
var actions = new PointerInputDevice(PointerKind.Touch);
var sequence = new ActionSequence(actions, 0);

// Perform touch tap at the center of the element
sequence.AddAction(actions.CreatePointerMove(CoordinateOrigin.Viewport, centerX, centerY, TimeSpan.Zero));
sequence.AddAction(actions.CreatePointerDown(MouseButton.Left));
sequence.AddAction(actions.CreatePointerUp(MouseButton.Left));

// Execute the tap action
driver.PerformActions(new List<ActionSequence> { sequence });
}

/// <summary>
/// Return the buttons in the alert or action sheet.
/// </summary>
Expand Down