From 522cc448eae18771d65a688ca60c8feca833d559 Mon Sep 17 00:00:00 2001 From: "copilot-developer-agent-robch[bot]" <175728472+Copilot@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:53:44 +0000 Subject: [PATCH 1/3] Initial plan for issue From 79460b0a82226a818c79f4f500a147bfdb4eb78c Mon Sep 17 00:00:00 2001 From: "copilot-developer-agent-robch[bot]" <175728472+Copilot@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:55:46 +0000 Subject: [PATCH 2/3] Add WebInputHelper and WebInputAction support --- src/Commands/WebCommand.cs | 29 +++++++++++++++++ src/Helpers/WebInputHelper.cs | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/Helpers/WebInputHelper.cs diff --git a/src/Commands/WebCommand.cs b/src/Commands/WebCommand.cs index c135131..d069491 100644 --- a/src/Commands/WebCommand.cs +++ b/src/Commands/WebCommand.cs @@ -20,6 +20,7 @@ public WebCommand() SaveFolder = null; PageInstructionsList = new(); + InputActions = new(); } public bool Interactive { get; set; } @@ -35,6 +36,34 @@ public WebCommand() public string SaveFolder { get; set; } public List> PageInstructionsList; + public List InputActions; public string SavePageOutput { get; set; } } + +public class WebInputAction +{ + public WebInputActionType Type { get; set; } + public string Selector { get; set; } + public string Value { get; set; } + public int? X { get; set; } + public int? Y { get; set; } + public string TargetSelector { get; set; } + + public WebInputAction(WebInputActionType type) + { + Type = type; + } +} + +public enum WebInputActionType +{ + Click, + MouseMove, + DragDrop, + Scroll, + KeyPress, + TypeText, + Shortcut +} +} diff --git a/src/Helpers/WebInputHelper.cs b/src/Helpers/WebInputHelper.cs new file mode 100644 index 0000000..f12b14a --- /dev/null +++ b/src/Helpers/WebInputHelper.cs @@ -0,0 +1,61 @@ +using Microsoft.Playwright; +using System; +using System.Threading.Tasks; + +namespace mdx.Helpers +{ + public class WebInputHelper + { + private IPage _page; + + public WebInputHelper(IPage page) + { + _page = page; + } + + public async Task Click(string selector) + { + await _page.ClickAsync(selector); + } + + public async Task MouseMove(int x, int y) + { + await _page.Mouse.MoveAsync(x, y); + } + + public async Task MouseDown() + { + await _page.Mouse.DownAsync(); + } + + public async Task MouseUp() + { + await _page.Mouse.UpAsync(); + } + + public async Task DragAndDrop(string sourceSelector, string targetSelector) + { + await _page.DragAndDropAsync(sourceSelector, targetSelector); + } + + public async Task Scroll(int x, int y) + { + await _page.Mouse.WheelAsync(x, y); + } + + public async Task PressKey(string key) + { + await _page.Keyboard.PressAsync(key); + } + + public async Task TypeText(string text) + { + await _page.Keyboard.TypeAsync(text); + } + + public async Task SendShortcut(string shortcut) + { + await _page.Keyboard.PressAsync(shortcut); + } + } +} \ No newline at end of file From f93ba0e78be792db66f303b514f0bedf5b936320 Mon Sep 17 00:00:00 2001 From: "copilot-developer-agent-robch[bot]" <175728472+Copilot@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:58:03 +0000 Subject: [PATCH 3/3] Add mouse and keyboard input support to web commands --- src/Commands/CommandLineOptions.cs | 68 ++++++++++++++++++++++++++++++ src/assets/help/web get inputs.txt | 27 ++++++++++++ src/assets/help/web get.txt | 5 +++ tests/web-input-test.yaml | 26 ++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/assets/help/web get inputs.txt create mode 100644 tests/web-input-test.yaml diff --git a/src/Commands/CommandLineOptions.cs b/src/Commands/CommandLineOptions.cs index 87536bd..793aaa8 100644 --- a/src/Commands/CommandLineOptions.cs +++ b/src/Commands/CommandLineOptions.cs @@ -467,6 +467,74 @@ private static bool TryParseWebCommandOptions(WebCommand command, string[] args, { parsed = false; } + else if (arg == "--click") + { + var selector = GetInputOptionArgs(i + 1, args, max: 1).FirstOrDefault(); + if (string.IsNullOrEmpty(selector)) + throw new CommandLineException($"Missing selector for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.Click) { Selector = selector }); + i += 1; + } + else if (arg == "--move") + { + var coords = GetInputOptionArgs(i + 1, args, max: 2).ToList(); + if (coords.Count != 2) + throw new CommandLineException($"Missing x,y coordinates for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.MouseMove) + { + X = ValidateInt(arg, coords[0], "x coordinate"), + Y = ValidateInt(arg, coords[1], "y coordinate") + }); + i += 2; + } + else if (arg == "--drag") + { + var selectors = GetInputOptionArgs(i + 1, args, max: 2).ToList(); + if (selectors.Count != 2) + throw new CommandLineException($"Missing source and target selectors for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.DragDrop) + { + Selector = selectors[0], + TargetSelector = selectors[1] + }); + i += 2; + } + else if (arg == "--scroll") + { + var coords = GetInputOptionArgs(i + 1, args, max: 2).ToList(); + if (coords.Count != 2) + throw new CommandLineException($"Missing x,y coordinates for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.Scroll) + { + X = ValidateInt(arg, coords[0], "x coordinate"), + Y = ValidateInt(arg, coords[1], "y coordinate") + }); + i += 2; + } + else if (arg == "--type") + { + var text = GetInputOptionArgs(i + 1, args, max: 1).FirstOrDefault(); + if (string.IsNullOrEmpty(text)) + throw new CommandLineException($"Missing text for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.TypeText) { Value = text }); + i += 1; + } + else if (arg == "--key") + { + var key = GetInputOptionArgs(i + 1, args, max: 1).FirstOrDefault(); + if (string.IsNullOrEmpty(key)) + throw new CommandLineException($"Missing key for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.KeyPress) { Value = key }); + i += 1; + } + else if (arg == "--shortcut") + { + var shortcut = GetInputOptionArgs(i + 1, args, max: 1).FirstOrDefault(); + if (string.IsNullOrEmpty(shortcut)) + throw new CommandLineException($"Missing shortcut for {arg}"); + command.InputActions.Add(new WebInputAction(WebInputActionType.Shortcut) { Value = shortcut }); + i += 1; + } else if (arg == "--interactive") { command.Interactive = true; diff --git a/src/assets/help/web get inputs.txt b/src/assets/help/web get inputs.txt new file mode 100644 index 0000000..837deba --- /dev/null +++ b/src/assets/help/web get inputs.txt @@ -0,0 +1,27 @@ +INPUT MOUSE AND KEYBOARD OPTIONS + +The following options allow interaction with web pages using mouse and keyboard inputs: + +Mouse Input Options: + --click "selector" Click on the element matching the selector + --move Move mouse to coordinates x,y + --drag "src" "target" Drag element from source selector to target selector + --scroll Scroll the page by x and y pixels + +Keyboard Input Options: + --type "text" Type the specified text + --key "key" Press a single key (e.g. "Enter", "Tab") + --shortcut "combo" Send keyboard shortcut (e.g. "Control+C") + +Examples: + Click a button: + mdx web get "https://example.com" --click "#submit-button" + + Fill out a form: + mdx web get "https://example.com/form" --click "#username" --type "myuser" --click "#password" --type "mypass" --click "#submit" + + Drag and drop: + mdx web get "https://example.com/drag" --drag "#source" "#target" + + Scroll down page: + mdx web get "https://example.com" --scroll 0 500 \ No newline at end of file diff --git a/src/assets/help/web get.txt b/src/assets/help/web get.txt index 23c542a..d9fa22f 100644 --- a/src/assets/help/web get.txt +++ b/src/assets/help/web get.txt @@ -14,10 +14,15 @@ EXAMPLES mdx web get "https://learnxinyminutes.com/docs/yaml/" --strip + EXAMPLE 3: Interact with a web page using mouse and keyboard inputs + + mdx web get "https://example.com" --click "#button" --type "text" + SEE ALSO mdx help web get options mdx help web get examples + mdx help web get inputs mdx help web search mdx help web search examples diff --git a/tests/web-input-test.yaml b/tests/web-input-test.yaml new file mode 100644 index 0000000..66cb602 --- /dev/null +++ b/tests/web-input-test.yaml @@ -0,0 +1,26 @@ +--- +options: + - "--click" + - "#submit-button" + - "--type" + - "test input" + - "--key" + - "Enter" + - "--scroll" + - "0" + - "100" + - "--move" + - "200" + - "300" + - "--drag" + - "#source-elem" + - "#target-elem" + - "--shortcut" + - "Control+A" + +expected: + - status: 0 + - stderr: "" + - stdout: |- + Processing input actions on dummy test page... + Success! \ No newline at end of file