From 87ea306a3e1b0f3278c96f06331d6fa2c304c718 Mon Sep 17 00:00:00 2001 From: jensakejohansson Date: Thu, 16 Jan 2025 15:09:39 +0100 Subject: [PATCH] Added method for clearing the clipboard. --- README.md | 2 +- src/FlaUI.WebDriver.UITests/ExecuteTests.cs | 12 ++++++++++++ .../Controllers/ExecuteController.cs | 16 +++++++++++++++- .../Services/IWindowsExtensionService.cs | 1 + .../Services/WindowsExtensionService.cs | 12 +++++++++--- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 824ee5c..63661e2 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ const result = driver.executeScript("powerShell", [{ command: `1+1` }]); ## Windows extensions -To enable easy switching from appium-windows-driver, there is a rudimentary implementation of `windows: click`, `windows: hover`, `windows: scroll`, `windows: keys`, `windows: getClipboard` and `windows: setClipboard`. +To enable easy switching from appium-windows-driver, there is a rudimentary implementation of `windows: click`, `windows: hover`, `windows: scroll`, `windows: keys`, `windows: getClipboard`, `windows: setClipboard` and `windows: clearClipboard`. ## Supported WebDriver Commands diff --git a/src/FlaUI.WebDriver.UITests/ExecuteTests.cs b/src/FlaUI.WebDriver.UITests/ExecuteTests.cs index 6380d81..8fdeb02 100644 --- a/src/FlaUI.WebDriver.UITests/ExecuteTests.cs +++ b/src/FlaUI.WebDriver.UITests/ExecuteTests.cs @@ -64,6 +64,18 @@ public void ExecuteScript_WindowsSetClipboard_IsSupported() Assert.That(element.Text, Is.EqualTo("Test TextBoxPasted!")); } + [Test] + public void ExecuteScript_WindowsClearClipboard_IsSupported() + { + var driverOptions = FlaUIDriverOptions.TestApp(); + using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions); + driver.ExecuteScript("windows: setClipboard", new Dictionary { + ["b64Content"] = "Pasted!"}); + driver.ExecuteScript("windows: clearClipboard"); + var result = driver.ExecuteScript("windows: getClipboard", new Dictionary {}); + Assert.That(result, Is.EqualTo("")); + } + [Test] public void ExecuteScript_WindowsHoverXY_IsSupported() { diff --git a/src/FlaUI.WebDriver/Controllers/ExecuteController.cs b/src/FlaUI.WebDriver/Controllers/ExecuteController.cs index c6d6858..4884df1 100644 --- a/src/FlaUI.WebDriver/Controllers/ExecuteController.cs +++ b/src/FlaUI.WebDriver/Controllers/ExecuteController.cs @@ -41,8 +41,11 @@ public async Task ExecuteScript([FromRoute] string sessionId, [Fro return await ExecuteWindowsSetClipboardScript(session, executeScriptRequest); case "windows: getClipboard": return await ExecuteWindowsGetClipboardScript(session, executeScriptRequest); + case "windows: clearClipboard": + return await ExecuteWindowsClearClipboardScript(session, executeScriptRequest); default: - throw WebDriverResponseException.UnsupportedOperation("Only 'powerShell', 'windows: keys', 'windows: click', 'windows: hover' scripts are supported"); + throw WebDriverResponseException.UnsupportedOperation("Only 'powerShell', 'windows: keys', 'windows: click', 'windows: hover', 'windows: scroll', " + + "'windows: setClipboard', 'windows: getClipboard', 'windows: clearClipboard' scripts are supported"); } } @@ -124,6 +127,17 @@ private async Task ExecuteWindowsGetClipboardScript(Session sessio return WebDriverResult.Success(result); } + private async Task ExecuteWindowsClearClipboardScript(Session session, ExecuteScriptRequest executeScriptRequest) + { + if (executeScriptRequest.Args.Count > 1) + { + throw WebDriverResponseException.InvalidArgument($"No arguments expected for the windows: getClipboard script, but got {executeScriptRequest.Args.Count} arguments"); + } + + await _windowsExtensionService.ExecuteClearClipboardScript(session); + return WebDriverResult.Success(); + } + private async Task ExecuteWindowsClickScript(Session session, ExecuteScriptRequest executeScriptRequest) { if (executeScriptRequest.Args.Count != 1) diff --git a/src/FlaUI.WebDriver/Services/IWindowsExtensionService.cs b/src/FlaUI.WebDriver/Services/IWindowsExtensionService.cs index f2abe95..bdbae2e 100644 --- a/src/FlaUI.WebDriver/Services/IWindowsExtensionService.cs +++ b/src/FlaUI.WebDriver/Services/IWindowsExtensionService.cs @@ -10,5 +10,6 @@ public interface IWindowsExtensionService Task ExecuteKeyScript(Session session, WindowsKeyScript action); Task ExecuteGetClipboardScript(Session session, WindowsGetClipboardScript action); Task ExecuteSetClipboardScript(Session session, WindowsSetClipboardScript action); + Task ExecuteClearClipboardScript(Session session); } } \ No newline at end of file diff --git a/src/FlaUI.WebDriver/Services/WindowsExtensionService.cs b/src/FlaUI.WebDriver/Services/WindowsExtensionService.cs index b3067e4..c8e1b56 100644 --- a/src/FlaUI.WebDriver/Services/WindowsExtensionService.cs +++ b/src/FlaUI.WebDriver/Services/WindowsExtensionService.cs @@ -17,7 +17,7 @@ public WindowsExtensionService(ILogger logger) public Task ExecuteGetClipboardScript(Session session, WindowsGetClipboardScript action) { - switch(action.ContentType) + switch (action.ContentType) { default: case "plaintext": @@ -59,6 +59,12 @@ public Task ExecuteSetClipboardScript(Session session, WindowsSetClipboardScript return Task.CompletedTask; } + public Task ExecuteClearClipboardScript(Session session) + { + ExecuteOnClipboardThread(() => System.Windows.Forms.Clipboard.Clear()); + return Task.CompletedTask; + } + private void ExecuteOnClipboardThread(System.Action action) { // See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard?view=windowsdesktop-8.0#remarks @@ -138,7 +144,7 @@ private Point GetPoint(string? elementId, int? x, int? y, Session session) Y = element.BoundingRectangle.Top + y.Value }; } - + return element.BoundingRectangle.Center(); } @@ -146,7 +152,7 @@ private Point GetPoint(string? elementId, int? x, int? y, Session session) { return new Point { X = x.Value, Y = y.Value }; } - + throw WebDriverResponseException.InvalidArgument("Either element ID or x and y must be provided"); }