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 : SystemClipboard

Example

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
SystemClipboard FakeClipboard

Constructors

NameDescription
FakeClipboard()

FakeClipboard()

FakeClipboard.cs
public FakeClipboard()

No documentation is available.

Properties

NameDescription
ChangeCountGets the clipboard change count (https://w3c.github.io/clipboard-apis/#clipboard-change-count): a value that changes each time the data is modified.
ContentChangedGets 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.
ItemsGets the system clipboard items (https://w3c.github.io/clipboard-apis/#system-clipboard-data).
ProvideContentGets 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.cs

Gets the clipboard change count (https://w3c.github.io/clipboard-apis/#clipboard-change-count): a value that changes each time the data is modified.

public override long ChangeCount { get; }

Property value

long

ContentChanged

FakeClipboard.cs

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.

public Action<IReadOnlyList<SystemClipboardItem>>? ContentChanged { get; init; }

Gets the system clipboard items (https://w3c.github.io/clipboard-apis/#system-clipboard-data).

public override IReadOnlyList<SystemClipboardItem> Items { get; }

ProvideContent

FakeClipboard.cs

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.

public Func<IReadOnlyList<SystemClipboardItem>>? ProvideContent { get; init; }

Methods

NameDescription
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.cs

Creates a clipboard item with a plain text representation (text/plain, UTF-8 encoded).

public static SystemClipboardItem CreateTextItem(string text)

Parameters

text string
The text.

Returns

SystemClipboardItem The item.

GetText(IReadOnlyList<SystemClipboardItem>)

FakeClipboard.cs

Returns 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

items IReadOnlyList<SystemClipboardItem>
The items, for instance the ones passed to FakeClipboard.ContentChanged.

Returns

string? The text, or null.

NotifyContentChanged()

FakeClipboard.cs

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.

public void NotifyContentChanged()

Write(IReadOnlyList<SystemClipboardItem>)

FakeClipboard.cs

Replaces 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)