Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): support clipboard api #6769

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions test/browser/fixtures/user-event/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect, test } from 'vitest';
import { page, userEvent, server } from '@vitest/browser/context';

test('clipboard', async () => {
document.body.innerHTML = `
<input placeholder="first" />
<input placeholder="second" />
<input placeholder="third" />
`;

// https://webdriver.io/docs/api/browser/keys/
// https://playwright.dev/docs/api/class-keyboard
const modifier =
server.provider === 'webdriverio'
? server.platform === 'darwin'
? 'Command'
: 'Control'
: server.provider === 'playwright'
? 'ControlOrMeta'
: 'Control';
const copy = `{${modifier}>}{c}{/${modifier}}`;
const cut = `{${modifier}>}{x}{/${modifier}}`;
const paste = `{${modifier}>}{v}{/${modifier}}`;

// write first "hello" and copy to clipboard
await userEvent.click(page.getByPlaceholder('first'));
await userEvent.keyboard('hello');
await userEvent.keyboard(`{selectall}`);
await userEvent.keyboard(copy);

// paste twice into second
await userEvent.click(page.getByPlaceholder('second'));
await userEvent.keyboard(paste);
await userEvent.keyboard(paste);

// cut first "hello"
await userEvent.click(page.getByPlaceholder('first'));
await userEvent.keyboard(`{selectall}`);
await userEvent.keyboard(cut);

// paste it to third
await userEvent.click(page.getByPlaceholder('third'));
await userEvent.keyboard(paste);

// hellohello
expect([
(page.getByPlaceholder('first').element() as any).value,
(page.getByPlaceholder('second').element() as any).value,
(page.getByPlaceholder('third').element() as any).value,
]).toMatchInlineSnapshot(`
[
"",
"hellohello",
"hello",
]
`)
});
5 changes: 4 additions & 1 deletion test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@
})

test('user-event', async () => {
const { ctx } = await runBrowserTests({
const { ctx, stderr } = await runBrowserTests({
root: './fixtures/user-event',
})
onTestFailed(() => console.error(stderr))

expect(Object.fromEntries(ctx.state.getFiles().map(f => [f.name, f.result.state]))).toMatchInlineSnapshot(`

Check failure on line 147 in test/browser/specs/runner.test.ts

View workflow job for this annotation

GitHub Actions / Browser: chromium, macos-latest

specs/runner.test.ts > user-event

Error: Snapshot `user-event 1` mismatched - Expected + Received { "cleanup-retry.test.ts": "pass", "cleanup1.test.ts": "pass", "cleanup2.test.ts": "pass", - "clipboard.test.ts": "pass", + "clipboard.test.ts": "fail", } ❯ specs/runner.test.ts:147:87

Check failure on line 147 in test/browser/specs/runner.test.ts

View workflow job for this annotation

GitHub Actions / Browser: firefox, macos-latest

specs/runner.test.ts > user-event

Error: Snapshot `user-event 1` mismatched - Expected + Received { "cleanup-retry.test.ts": "pass", "cleanup1.test.ts": "pass", "cleanup2.test.ts": "pass", - "clipboard.test.ts": "pass", + "clipboard.test.ts": "fail", } ❯ specs/runner.test.ts:147:87
{
"cleanup-retry.test.ts": "pass",
"cleanup1.test.ts": "pass",
"cleanup2.test.ts": "pass",
"clipboard.test.ts": "pass",
}
`)
})
Loading