DragAndDrop

This commit is contained in:
Haiping Chen 2024-11-23 20:02:43 -06:00
parent e92b72ceb5
commit f722781be4
4 changed files with 108 additions and 17 deletions

View file

@ -7,4 +7,5 @@ public enum BroswerActionEnum
Typing = 3,
Hover = 4,
Scroll = 5,
DragAndDrop = 6
}

View file

@ -5,4 +5,9 @@ public class ElementPosition
public float X { get; set; } = default!;
public float Y { get; set; } = default!;
public override string ToString()
{
return $"[{X}, {Y}]";
}
}

View file

@ -42,30 +42,23 @@ public class RedisSubscriber : IEventSubscriber
while (true)
{
try
if (priorityEnabled)
{
if (priorityEnabled)
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
{
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
{
continue;
}
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
{
continue;
}
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
continue;
}
else
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
{
await HandleGroupMessage(db, channel, group, received);
continue;
}
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
}
catch (Exception ex)
else
{
_logger.LogError($"Error processing message: {ex.Message}\r\n{ex}");
await HandleGroupMessage(db, channel, group, received);
}
}
}

View file

@ -79,10 +79,102 @@ public partial class PlaywrightWebDriver
{
await locator.HoverAsync();
}
else if (action.Action == BroswerActionEnum.DragAndDrop)
{
// Locate the element to drag
var box = await locator.BoundingBoxAsync();
if (box != null)
{
// Calculate start position
float startX = box.X + box.Width / 2; // Start at the center of the element
float startY = box.Y + box.Height / 2;
// Drag offsets
float offsetX = action.Position.X;
// Move horizontally
if (action.Position.Y == 0)
{
// Perform drag-and-move
// Move mouse to the start position
var mouse = page.Mouse;
await mouse.MoveAsync(startX, startY);
await mouse.DownAsync();
// Move mouse smoothly in increments
var tracks = GetVelocityTrack(offsetX);
foreach (var track in tracks)
{
startX += track;
await page.Mouse.MoveAsync(startX, 0, new MouseMoveOptions
{
Steps = 3
});
}
// Release mouse button
await mouse.UpAsync();
}
else
{
throw new NotImplementedException();
}
}
}
if (action.WaitTime > 0)
{
await Task.Delay(1000 * action.WaitTime);
}
}
public static List<int> GetVelocityTrack(float distance)
{
// Initialize the track list to store the movement distances
List<int> track = new List<int>();
// Initialize variables
float current = 0; // Current position
float mid = distance * 4 / 5; // Deceleration threshold
float t = 0.2f; // Time interval
float v = 1; // Initial velocity
// Generate the track
while (current < distance)
{
float a; // Acceleration
// Determine acceleration based on position
if (current < mid)
{
a = 4; // Accelerate
}
else
{
a = -3; // Decelerate
}
// Calculate new velocity
float v0 = v;
v = v0 + a * t;
// Calculate the movement during this interval
float move = v0 * t + 0.5f * a * t * t;
// Update current position
if (current + move > distance)
{
move = distance - current;
track.Add((int)Math.Round(move));
break;
}
current += move;
// Add rounded movement to the track
track.Add((int)Math.Round(move));
}
return track;
}
}