Skip to content

Commit

Permalink
Added method for clearing the clipboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
jensakejohansson committed Jan 16, 2025
1 parent 63f7772 commit 87ea306
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/FlaUI.WebDriver.UITests/ExecuteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> {
["b64Content"] = "Pasted!"});
driver.ExecuteScript("windows: clearClipboard");
var result = driver.ExecuteScript("windows: getClipboard", new Dictionary<string, object> {});
Assert.That(result, Is.EqualTo(""));
}

[Test]
public void ExecuteScript_WindowsHoverXY_IsSupported()
{
Expand Down
16 changes: 15 additions & 1 deletion src/FlaUI.WebDriver/Controllers/ExecuteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public async Task<ActionResult> 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");
}
}

Expand Down Expand Up @@ -124,6 +127,17 @@ private async Task<ActionResult> ExecuteWindowsGetClipboardScript(Session sessio
return WebDriverResult.Success(result);
}

private async Task<ActionResult> 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<ActionResult> ExecuteWindowsClickScript(Session session, ExecuteScriptRequest executeScriptRequest)
{
if (executeScriptRequest.Args.Count != 1)
Expand Down
1 change: 1 addition & 0 deletions src/FlaUI.WebDriver/Services/IWindowsExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IWindowsExtensionService
Task ExecuteKeyScript(Session session, WindowsKeyScript action);
Task<string> ExecuteGetClipboardScript(Session session, WindowsGetClipboardScript action);
Task ExecuteSetClipboardScript(Session session, WindowsSetClipboardScript action);
Task ExecuteClearClipboardScript(Session session);
}
}
12 changes: 9 additions & 3 deletions src/FlaUI.WebDriver/Services/WindowsExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public WindowsExtensionService(ILogger<WindowsExtensionService> logger)

public Task<string> ExecuteGetClipboardScript(Session session, WindowsGetClipboardScript action)
{
switch(action.ContentType)
switch (action.ContentType)
{
default:
case "plaintext":
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -138,15 +144,15 @@ private Point GetPoint(string? elementId, int? x, int? y, Session session)
Y = element.BoundingRectangle.Top + y.Value
};
}

return element.BoundingRectangle.Center();
}

if (x.HasValue && y.HasValue)
{
return new Point { X = x.Value, Y = y.Value };
}

throw WebDriverResponseException.InvalidArgument("Either element ID or x and y must be provided");
}

Expand Down

0 comments on commit 87ea306

Please sign in to comment.