From f722781be491899256b3427a19dee851db01d2e1 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sat, 23 Nov 2024 20:02:43 -0600 Subject: [PATCH] DragAndDrop --- .../Browsing/Enums/BroswerActionEnum.cs | 1 + .../Browsing/Models/ElementPosition.cs | 5 + .../Infrastructures/Events/RedisSubscriber.cs | 27 ++---- .../PlaywrightWebDriver.DoAction.cs | 92 +++++++++++++++++++ 4 files changed, 108 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs index 8f1cee10..abdfd67b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs @@ -7,4 +7,5 @@ public enum BroswerActionEnum Typing = 3, Hover = 4, Scroll = 5, + DragAndDrop = 6 } diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs index fd95bd66..9a1ef0d3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs @@ -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}]"; + } } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs index 67dba26d..b93c22a2 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs @@ -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); } } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs index f7884beb..40fec84e 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs @@ -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 GetVelocityTrack(float distance) + { + // Initialize the track list to store the movement distances + List track = new List(); + + // 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; + } }