FakeClipboard Class
A system clipboard for the tests: FakeClipboard.ProvideContent supplies what the pages paste or read, and FakeClipboard.ContentChanged is called with what they copy or write. Set it in BrowsingContextOptions.Clipboard with TestPageSettings.ConfigureBrowsingContext; the pages that use the same instance share it. Without it, each page has a clipboard of its own.
public sealed class FakeClipboard : SystemClipboardExample
var copied = new List<string?>();
var clipboard = new FakeClipboard
{
// What the pages paste or read
ProvideContent = () => [FakeClipboard.CreateTextItem("Pasted text")],
// Called with what the pages copy or write
ContentChanged = items => copied.Add(FakeClipboard.GetText(items)),
};
await using var factory = new WebApplicationFactory<Program>();
await using var browser = WebAppBrowser.Create(factory.Server, new WebAppBrowserOptions
{
// The pages that use the same clipboard share it
ConfigureBrowsingContext = options => options with { Clipboard = clipboard },
});
var page = await browser.OpenAsync("/secure");
await page.Find("#copy").ClickAsync();
await page.Find("#paste").ClickAsync();
Assert.Equal(["Copied text"], copied);
Assert.Equal("Pasted text", page.Find("output").TextContent);- Inheritance
SystemClipboardFakeClipboard
Constructors
| Name | Description |
|---|---|
| FakeClipboard() |
FakeClipboard()
FakeClipboard.cspublic FakeClipboard()No documentation is available.
Properties
| Name | Description |
|---|---|
| ChangeCount | Gets the clipboard change count (https://w3c.github.io/clipboard-apis/#clipboard-change-count): a value that changes each time the data is modified. |
| ContentChanged | Gets a function called with the new content each time the clipboard is written (the copy and cut actions, navigator.clipboard.write() and writeText()). It is called before SystemClipboard.Changed and the clipboardchange events, so their listeners read the new content. |
| Items | Gets the system clipboard items (https://w3c.github.io/clipboard-apis/#system-clipboard-data). |
| ProvideContent | Gets a function that returns the content of the clipboard each time it is read (the paste action, navigator.clipboard.read() and readText()), or null to read the content last written. |
ChangeCount
FakeClipboard.csContentChanged
FakeClipboard.csGets a function called with the new content each time the clipboard is written (the copy and cut actions, navigator.clipboard.write() and writeText()). It is called before SystemClipboard.Changed and the clipboardchange events, so their listeners read the new content.
public Action<IReadOnlyList<SystemClipboardItem>>? ContentChanged { get; init; }Property value
Items
FakeClipboard.csGets the system clipboard items (https://w3c.github.io/clipboard-apis/#system-clipboard-data).
public override IReadOnlyList<SystemClipboardItem> Items { get; }Property value
ProvideContent
FakeClipboard.csGets a function that returns the content of the clipboard each time it is read (the paste action, navigator.clipboard.read() and readText()), or null to read the content last written.
public Func<IReadOnlyList<SystemClipboardItem>>? ProvideContent { get; init; }Property value
Methods
| Name | Description |
|---|---|
| CreateTextItem(string) | Creates a clipboard item with a plain text representation (text/plain, UTF-8 encoded). |
| GetText(IReadOnlyList<SystemClipboardItem>) | Returns the plain text of clipboard items (the first text/plain representation, decoded as UTF-8), or null when there is none. |
| NotifyContentChanged() | Notifies the pages that the content returned by FakeClipboard.ProvideContent changed, as when another application writes to the clipboard: it fires the clipboardchange events. Call it on the thread of the event loop of the pages. |
| Write(IReadOnlyList<SystemClipboardItem>) | Replaces the data of the clipboard, then raises SystemClipboard.Changed. An override must call SystemClipboard.OnChanged when the data changed. |
CreateTextItem(string)
FakeClipboard.csCreates a clipboard item with a plain text representation (text/plain, UTF-8 encoded).
public static SystemClipboardItem CreateTextItem(string text)GetText(IReadOnlyList<SystemClipboardItem>)
FakeClipboard.csReturns the plain text of clipboard items (the first text/plain representation, decoded as UTF-8), or null when there is none.
public static string? GetText(IReadOnlyList<SystemClipboardItem> items)Parameters
itemsIReadOnlyList<SystemClipboardItem>- The items, for instance the ones passed to
FakeClipboard.ContentChanged.
Returns
string? The text, or null.
NotifyContentChanged()
FakeClipboard.csNotifies the pages that the content returned by FakeClipboard.ProvideContent changed, as when another application writes to the clipboard: it fires the clipboardchange events. Call it on the thread of the event loop of the pages.
public void NotifyContentChanged()Write(IReadOnlyList<SystemClipboardItem>)
FakeClipboard.csReplaces the data of the clipboard, then raises SystemClipboard.Changed. An override must call SystemClipboard.OnChanged when the data changed.
public override void Write(IReadOnlyList<SystemClipboardItem> items)