hi Team,
We have written a swipe functionality using W3C actions and it is working fine in iOS but in android it is not working.
Appium version - 2.10.3
uiautomator2 version - 3.6.1
Below is the code
using Leapwork.Mobile.Core;
using Leapwork.Mobile.Core.Enums.Commands;
using Leapwork.Mobile.Core.Options.Commands;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Generic;
using System.Drawing;
using PointerInputDevice = OpenQA.Selenium.Appium.Interactions.PointerInputDevice;
namespace Leapwork.Mobile.Appium.Commands
{
public abstract class AppiumMobileSwipeElementCommand<TExecutor, TDriver> : AppiumMobileCommand<TExecutor, TDriver>
where TExecutor : AppiumMobileDriver<TDriver>
where TDriver : AppiumDriver
{
protected AppiumMobileSwipeElementCommand(IMobileElement element, SwipeOptions options, int moveToDelayMilliseconds = 50)
{
Element = element;
Options = options;
var heightStep = element.Bounds.Height / 4;
var heightDelta = heightStep > 0 ? heightStep : 1;
heightDeltaScaled = heightDelta * options.Length;
var widthStep = element.Bounds.Width / 4;
var widthDelta = widthStep > 0 ? widthStep : 1;
widthDeltaScaled = widthDelta * options.Length;
MoveToDelayMilliseconds = moveToDelayMilliseconds;
}
private readonly int heightDeltaScaled;
private readonly int widthDeltaScaled;
protected readonly int MoveToDelayMilliseconds;
protected IMobileElement Element { get; }
protected SwipeOptions Options { get; }
protected override void Execute(TExecutor executor)
{
var inputDevice = new PointerInputDevice(PointerKind.Touch,"swipeTouch");
var swipeActionSequence = new ActionSequence(inputDevice, 0);
try
{
var center = GetElementCenter();
executor.Log.Debug($"Element center calculated at ({center.X}, {center.Y}).");
var destination = GetTouchDestination(center);
executor.Log.Debug($"Touch destination calculated at ({destination.X}, {destination.Y}).");
//Creating swipe sequence
swipeActionSequence.AddAction(inputDevice.CreatePointerMove(CoordinateOrigin.Viewport, center.X, center.Y, TimeSpan.Zero));
swipeActionSequence.AddAction(inputDevice.CreatePointerDown(PointerButton.TouchContact));
swipeActionSequence.AddAction(inputDevice.CreatePause(TimeSpan.FromMilliseconds(MoveToDelayMilliseconds)));
swipeActionSequence.AddAction(inputDevice.CreatePointerMove(CoordinateOrigin.Viewport, destination.X, destination.Y, TimeSpan.Zero));
swipeActionSequence.AddAction(inputDevice.CreatePointerUp(PointerButton.TouchContact));
//Performing swipe action sequence
executor.Driver.PerformActions(new List<ActionSequence> { swipeActionSequence });
executor.Log.Debug("Swipe actions performed successfully.");
}
catch (Exception ex)
{
executor.Log.Error($"Error while executing swipe action: {ex.Message}");
}
}
private Point GetElementCenter()
{
var x = Element.Bounds.Left + Element.Bounds.Width / 2;
var y = Element.Bounds.Top + Element.Bounds.Height / 2;
return new Point(x, y);
}
private Point GetTouchDestination(Point center)
{
var x = 0;
var y = 0;
switch (Options.Direction)
{
case ElementSwipeDirection.Left:
x = center.X - widthDeltaScaled;
y = center.Y;
break;
case ElementSwipeDirection.Right:
x = center.X + widthDeltaScaled;
y = center.Y;
break;
case ElementSwipeDirection.Up:
x = center.X;
y = center.Y - heightDeltaScaled;
break;
case ElementSwipeDirection.Down:
x = center.X;
y = center.Y + heightDeltaScaled;
break;
}
var point = new Point(x, y);
return point;
}
}
}
Kindly assist