playwright-clipboard-testing 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -36
- package/dist/clipboardHandler-CFIUvkc0.d.cts +51 -0
- package/dist/clipboardHandler-CFIUvkc0.d.ts +51 -0
- package/dist/constants/index.cjs +80 -0
- package/dist/constants/index.cjs.map +1 -0
- package/dist/constants/index.d.cts +51 -0
- package/dist/constants/index.d.ts +51 -0
- package/dist/constants/index.js +52 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/fixtures/index.cjs +130 -0
- package/dist/fixtures/index.cjs.map +1 -0
- package/dist/fixtures/index.d.cts +40 -0
- package/dist/fixtures/index.d.ts +40 -0
- package/dist/fixtures/index.js +100 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/index.cjs +206 -56
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -188
- package/dist/index.d.ts +11 -188
- package/dist/index.js +201 -45
- package/dist/index.js.map +1 -1
- package/dist/matchers/index.cjs +295 -0
- package/dist/matchers/index.cjs.map +1 -0
- package/dist/matchers/index.d.cts +16 -0
- package/dist/matchers/index.d.ts +16 -0
- package/dist/matchers/index.js +268 -0
- package/dist/matchers/index.js.map +1 -0
- package/dist/toBeBlank-uFClUdzK.d.cts +133 -0
- package/dist/toBeBlank-wAm_5G4T.d.ts +133 -0
- package/package.json +30 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/fixtures/index.ts
|
|
21
|
+
var fixtures_exports = {};
|
|
22
|
+
__export(fixtures_exports, {
|
|
23
|
+
ClipboardHandler: () => ClipboardHandler,
|
|
24
|
+
clipboardFixture: () => clipboardFixture,
|
|
25
|
+
clipboardFixtures: () => clipboardFixtures,
|
|
26
|
+
contextFixture: () => contextFixture
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(fixtures_exports);
|
|
29
|
+
|
|
30
|
+
// src/utils/clipboardHandler.ts
|
|
31
|
+
var ClipboardHandler = class {
|
|
32
|
+
constructor(page) {
|
|
33
|
+
this.page = page;
|
|
34
|
+
}
|
|
35
|
+
page;
|
|
36
|
+
/**
|
|
37
|
+
* Reads the current text content from the browser clipboard.
|
|
38
|
+
* If the content is a JSON-encoded string (e.g., has extra quotes),
|
|
39
|
+
* it will be returned as is. Use readJSON for automatic parsing.
|
|
40
|
+
*
|
|
41
|
+
* @returns A promise that resolves to the clipboard string content.
|
|
42
|
+
*/
|
|
43
|
+
async read() {
|
|
44
|
+
return await this.page.evaluate(() => navigator.clipboard.readText());
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Reads the clipboard content and parses it as JSON.
|
|
48
|
+
* If the content is a string literal (e.g., '"value"'), it returns the unwrapped string ('value').
|
|
49
|
+
*
|
|
50
|
+
* @template T - The expected type of the parsed JSON object.
|
|
51
|
+
* @returns A promise that resolves to the parsed JSON object of type T.
|
|
52
|
+
* @throws {Error} If the clipboard content is not a valid JSON string.
|
|
53
|
+
*/
|
|
54
|
+
async readJSON() {
|
|
55
|
+
const text = await this.read();
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(text);
|
|
58
|
+
} catch {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`[playwright-clipboard] Clipboard content is not a valid JSON: ${JSON.stringify(text)}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Writes the provided string data to the browser clipboard.
|
|
66
|
+
* @param data The data to write to the clipboard.
|
|
67
|
+
*/
|
|
68
|
+
async write(data) {
|
|
69
|
+
await this.page.evaluate((value) => navigator.clipboard.writeText(value), data);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Writes the provided data to the browser clipboard as a JSON string.
|
|
73
|
+
* @param data The data to write to the clipboard. It will be stringified as JSON.
|
|
74
|
+
*/
|
|
75
|
+
async writeJSON(data) {
|
|
76
|
+
const errorMessage = "[playwright-clipboard] Provided data cannot be stringified to valid JSON";
|
|
77
|
+
let jsonString;
|
|
78
|
+
try {
|
|
79
|
+
jsonString = JSON.stringify(data);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
const message = error instanceof Error ? error : String(error);
|
|
82
|
+
throw new Error(`${errorMessage}: ${message}`);
|
|
83
|
+
}
|
|
84
|
+
if (jsonString === void 0) {
|
|
85
|
+
throw new Error(`${errorMessage} (received undefined).`);
|
|
86
|
+
}
|
|
87
|
+
await this.write(jsonString);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Clears the browser clipboard by writing an empty string to it.
|
|
91
|
+
* This effectively removes any existing content from the clipboard.
|
|
92
|
+
*/
|
|
93
|
+
async clear() {
|
|
94
|
+
await this.write("");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/fixtures/clipboardFixture.ts
|
|
99
|
+
var clipboardFixture = async ({ page, browserName }, use) => {
|
|
100
|
+
if (browserName === "webkit") {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`[playwright-clipboard] Browser '${browserName}' is not supported. Clipboard API testing is currently supported only in Chromium-based and Firefox browsers.
|
|
103
|
+
Add test.skip(browserName === 'webkit', 'Clipboard API is only supported in Chromium and Firefox'); to your test body.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const handler = new ClipboardHandler(page);
|
|
107
|
+
await use(handler);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// src/fixtures/contextFixture.ts
|
|
111
|
+
var contextFixture = async ({ context, browserName }, use) => {
|
|
112
|
+
if (browserName === "chromium") {
|
|
113
|
+
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
|
|
114
|
+
}
|
|
115
|
+
await use(context);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// src/fixtures/index.ts
|
|
119
|
+
var clipboardFixtures = {
|
|
120
|
+
context: contextFixture,
|
|
121
|
+
clipboard: clipboardFixture
|
|
122
|
+
};
|
|
123
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
+
0 && (module.exports = {
|
|
125
|
+
ClipboardHandler,
|
|
126
|
+
clipboardFixture,
|
|
127
|
+
clipboardFixtures,
|
|
128
|
+
contextFixture
|
|
129
|
+
});
|
|
130
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/fixtures/index.ts","../../src/utils/clipboardHandler.ts","../../src/fixtures/clipboardFixture.ts","../../src/fixtures/contextFixture.ts"],"sourcesContent":["import { ClipboardHandler } from '../utils';\nimport { clipboardFixture } from './clipboardFixture.ts';\nimport { contextFixture } from './contextFixture.ts';\n\n/**\n * A collection of fixtures related to clipboard testing, including the clipboard fixture and context fixture.\n * These fixtures can be used in Playwright tests to facilitate clipboard interactions and context management.\n */\nexport const clipboardFixtures = {\n context: contextFixture,\n clipboard: clipboardFixture,\n};\n\nexport { ClipboardHandler, clipboardFixture, contextFixture };\n","/**\n * Playwright-compatible Clipboard utilities.\n *\n * Core functions for interacting with the browser clipboard within Playwright tests.\n * Provides wrappers for reading plain text and JSON.\n *\n * @remarks\n * These utilities require 'clipboard-read' and 'clipboard-write' permissions\n * to be granted in the browser context.\n */\n\nimport type { Page } from '@playwright/test';\n\nexport class ClipboardHandler {\n constructor(private readonly page: Page) {}\n\n /**\n * Reads the current text content from the browser clipboard.\n * If the content is a JSON-encoded string (e.g., has extra quotes),\n * it will be returned as is. Use readJSON for automatic parsing.\n *\n * @returns A promise that resolves to the clipboard string content.\n */\n async read(): Promise<string> {\n return await this.page.evaluate(() => navigator.clipboard.readText());\n }\n\n /**\n * Reads the clipboard content and parses it as JSON.\n * If the content is a string literal (e.g., '\"value\"'), it returns the unwrapped string ('value').\n *\n * @template T - The expected type of the parsed JSON object.\n * @returns A promise that resolves to the parsed JSON object of type T.\n * @throws {Error} If the clipboard content is not a valid JSON string.\n */\n async readJSON<T = unknown>(): Promise<T> {\n const text = await this.read();\n try {\n return JSON.parse(text);\n } catch {\n throw new Error(\n `[playwright-clipboard] Clipboard content is not a valid JSON: ${JSON.stringify(text)}`,\n );\n }\n }\n\n /**\n * Writes the provided string data to the browser clipboard.\n * @param data The data to write to the clipboard.\n */\n async write(data: string): Promise<void> {\n await this.page.evaluate((value) => navigator.clipboard.writeText(value), data);\n }\n\n /**\n * Writes the provided data to the browser clipboard as a JSON string.\n * @param data The data to write to the clipboard. It will be stringified as JSON.\n */\n async writeJSON<T = unknown>(data: T): Promise<void> {\n const errorMessage = '[playwright-clipboard] Provided data cannot be stringified to valid JSON';\n let jsonString: string | undefined;\n\n try {\n jsonString = JSON.stringify(data);\n } catch (error) {\n const message = error instanceof Error ? error : String(error);\n throw new Error(`${errorMessage}: ${message}`);\n }\n\n if (jsonString === undefined) {\n throw new Error(`${errorMessage} (received undefined).`);\n }\n\n await this.write(jsonString);\n }\n\n /**\n * Clears the browser clipboard by writing an empty string to it.\n * This effectively removes any existing content from the clipboard.\n */\n async clear(): Promise<void> {\n await this.write('');\n }\n}\n","import type { Page, TestFixture } from '@playwright/test';\nimport { ClipboardHandler } from '../utils';\nimport type { BrowserName } from './types.ts';\n\n/**\n * A fixture that provides an instance of the Clipboard utility for interacting with the system clipboard.\n * It allows reading from the clipboard during tests.\n */\nexport const clipboardFixture: TestFixture<\n ClipboardHandler,\n { page: Page; browserName: BrowserName }\n> = async ({ page, browserName }, use) => {\n if (browserName === 'webkit') {\n throw new Error(\n `[playwright-clipboard] Browser '${browserName}' is not supported. ` +\n 'Clipboard API testing is currently supported only in Chromium-based and Firefox browsers.\\n' +\n `Add test.skip(browserName === 'webkit', 'Clipboard API is only supported in Chromium and Firefox'); to your test body.`,\n );\n }\n\n const handler = new ClipboardHandler(page);\n await use(handler);\n};\n","import type { BrowserContext, TestFixture } from '@playwright/test';\nimport type { BrowserName } from './types.ts';\n\n/**\n * A fixture that provides a BrowserContext with clipboard permissions granted for Chromium browsers.\n */\nexport const contextFixture: TestFixture<\n BrowserContext,\n { context: BrowserContext; browserName: BrowserName }\n> = async ({ context, browserName }, use) => {\n if (browserName === 'chromium') {\n await context.grantPermissions(['clipboard-read', 'clipboard-write']);\n }\n\n await use(context);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaO,IAAM,mBAAN,MAAuB;AAAA,EAC5B,YAA6B,MAAY;AAAZ;AAAA,EAAa;AAAA,EAAb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS7B,MAAM,OAAwB;AAC5B,WAAO,MAAM,KAAK,KAAK,SAAS,MAAM,UAAU,UAAU,SAAS,CAAC;AAAA,EACtE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,WAAoC;AACxC,UAAM,OAAO,MAAM,KAAK,KAAK;AAC7B,QAAI;AACF,aAAO,KAAK,MAAM,IAAI;AAAA,IACxB,QAAQ;AACN,YAAM,IAAI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
|
|
|
1
|
+
import * as playwright_test from 'playwright/test';
|
|
2
|
+
import * as playwright_core from 'playwright-core';
|
|
3
|
+
import { C as ClipboardHandler } from '../clipboardHandler-CFIUvkc0.cjs';
|
|
4
|
+
import { TestFixture, Page, BrowserContext } from '@playwright/test';
|
|
5
|
+
|
|
6
|
+
type BrowserName = 'chromium' | 'firefox' | 'webkit';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A fixture that provides an instance of the Clipboard utility for interacting with the system clipboard.
|
|
10
|
+
* It allows reading from the clipboard during tests.
|
|
11
|
+
*/
|
|
12
|
+
declare const clipboardFixture: TestFixture<ClipboardHandler, {
|
|
13
|
+
page: Page;
|
|
14
|
+
browserName: BrowserName;
|
|
15
|
+
}>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A fixture that provides a BrowserContext with clipboard permissions granted for Chromium browsers.
|
|
19
|
+
*/
|
|
20
|
+
declare const contextFixture: TestFixture<BrowserContext, {
|
|
21
|
+
context: BrowserContext;
|
|
22
|
+
browserName: BrowserName;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A collection of fixtures related to clipboard testing, including the clipboard fixture and context fixture.
|
|
27
|
+
* These fixtures can be used in Playwright tests to facilitate clipboard interactions and context management.
|
|
28
|
+
*/
|
|
29
|
+
declare const clipboardFixtures: {
|
|
30
|
+
context: playwright_test.TestFixture<playwright_core.BrowserContext, {
|
|
31
|
+
context: playwright_core.BrowserContext;
|
|
32
|
+
browserName: BrowserName;
|
|
33
|
+
}>;
|
|
34
|
+
clipboard: playwright_test.TestFixture<ClipboardHandler, {
|
|
35
|
+
page: playwright_core.Page;
|
|
36
|
+
browserName: BrowserName;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { ClipboardHandler, clipboardFixture, clipboardFixtures, contextFixture };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as playwright_test from 'playwright/test';
|
|
2
|
+
import * as playwright_core from 'playwright-core';
|
|
3
|
+
import { C as ClipboardHandler } from '../clipboardHandler-CFIUvkc0.js';
|
|
4
|
+
import { TestFixture, Page, BrowserContext } from '@playwright/test';
|
|
5
|
+
|
|
6
|
+
type BrowserName = 'chromium' | 'firefox' | 'webkit';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A fixture that provides an instance of the Clipboard utility for interacting with the system clipboard.
|
|
10
|
+
* It allows reading from the clipboard during tests.
|
|
11
|
+
*/
|
|
12
|
+
declare const clipboardFixture: TestFixture<ClipboardHandler, {
|
|
13
|
+
page: Page;
|
|
14
|
+
browserName: BrowserName;
|
|
15
|
+
}>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A fixture that provides a BrowserContext with clipboard permissions granted for Chromium browsers.
|
|
19
|
+
*/
|
|
20
|
+
declare const contextFixture: TestFixture<BrowserContext, {
|
|
21
|
+
context: BrowserContext;
|
|
22
|
+
browserName: BrowserName;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A collection of fixtures related to clipboard testing, including the clipboard fixture and context fixture.
|
|
27
|
+
* These fixtures can be used in Playwright tests to facilitate clipboard interactions and context management.
|
|
28
|
+
*/
|
|
29
|
+
declare const clipboardFixtures: {
|
|
30
|
+
context: playwright_test.TestFixture<playwright_core.BrowserContext, {
|
|
31
|
+
context: playwright_core.BrowserContext;
|
|
32
|
+
browserName: BrowserName;
|
|
33
|
+
}>;
|
|
34
|
+
clipboard: playwright_test.TestFixture<ClipboardHandler, {
|
|
35
|
+
page: playwright_core.Page;
|
|
36
|
+
browserName: BrowserName;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { ClipboardHandler, clipboardFixture, clipboardFixtures, contextFixture };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/utils/clipboardHandler.ts
|
|
2
|
+
var ClipboardHandler = class {
|
|
3
|
+
constructor(page) {
|
|
4
|
+
this.page = page;
|
|
5
|
+
}
|
|
6
|
+
page;
|
|
7
|
+
/**
|
|
8
|
+
* Reads the current text content from the browser clipboard.
|
|
9
|
+
* If the content is a JSON-encoded string (e.g., has extra quotes),
|
|
10
|
+
* it will be returned as is. Use readJSON for automatic parsing.
|
|
11
|
+
*
|
|
12
|
+
* @returns A promise that resolves to the clipboard string content.
|
|
13
|
+
*/
|
|
14
|
+
async read() {
|
|
15
|
+
return await this.page.evaluate(() => navigator.clipboard.readText());
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Reads the clipboard content and parses it as JSON.
|
|
19
|
+
* If the content is a string literal (e.g., '"value"'), it returns the unwrapped string ('value').
|
|
20
|
+
*
|
|
21
|
+
* @template T - The expected type of the parsed JSON object.
|
|
22
|
+
* @returns A promise that resolves to the parsed JSON object of type T.
|
|
23
|
+
* @throws {Error} If the clipboard content is not a valid JSON string.
|
|
24
|
+
*/
|
|
25
|
+
async readJSON() {
|
|
26
|
+
const text = await this.read();
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(text);
|
|
29
|
+
} catch {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`[playwright-clipboard] Clipboard content is not a valid JSON: ${JSON.stringify(text)}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Writes the provided string data to the browser clipboard.
|
|
37
|
+
* @param data The data to write to the clipboard.
|
|
38
|
+
*/
|
|
39
|
+
async write(data) {
|
|
40
|
+
await this.page.evaluate((value) => navigator.clipboard.writeText(value), data);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Writes the provided data to the browser clipboard as a JSON string.
|
|
44
|
+
* @param data The data to write to the clipboard. It will be stringified as JSON.
|
|
45
|
+
*/
|
|
46
|
+
async writeJSON(data) {
|
|
47
|
+
const errorMessage = "[playwright-clipboard] Provided data cannot be stringified to valid JSON";
|
|
48
|
+
let jsonString;
|
|
49
|
+
try {
|
|
50
|
+
jsonString = JSON.stringify(data);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const message = error instanceof Error ? error : String(error);
|
|
53
|
+
throw new Error(`${errorMessage}: ${message}`);
|
|
54
|
+
}
|
|
55
|
+
if (jsonString === void 0) {
|
|
56
|
+
throw new Error(`${errorMessage} (received undefined).`);
|
|
57
|
+
}
|
|
58
|
+
await this.write(jsonString);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Clears the browser clipboard by writing an empty string to it.
|
|
62
|
+
* This effectively removes any existing content from the clipboard.
|
|
63
|
+
*/
|
|
64
|
+
async clear() {
|
|
65
|
+
await this.write("");
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/fixtures/clipboardFixture.ts
|
|
70
|
+
var clipboardFixture = async ({ page, browserName }, use) => {
|
|
71
|
+
if (browserName === "webkit") {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`[playwright-clipboard] Browser '${browserName}' is not supported. Clipboard API testing is currently supported only in Chromium-based and Firefox browsers.
|
|
74
|
+
Add test.skip(browserName === 'webkit', 'Clipboard API is only supported in Chromium and Firefox'); to your test body.`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const handler = new ClipboardHandler(page);
|
|
78
|
+
await use(handler);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/fixtures/contextFixture.ts
|
|
82
|
+
var contextFixture = async ({ context, browserName }, use) => {
|
|
83
|
+
if (browserName === "chromium") {
|
|
84
|
+
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
|
|
85
|
+
}
|
|
86
|
+
await use(context);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/fixtures/index.ts
|
|
90
|
+
var clipboardFixtures = {
|
|
91
|
+
context: contextFixture,
|
|
92
|
+
clipboard: clipboardFixture
|
|
93
|
+
};
|
|
94
|
+
export {
|
|
95
|
+
ClipboardHandler,
|
|
96
|
+
clipboardFixture,
|
|
97
|
+
clipboardFixtures,
|
|
98
|
+
contextFixture
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/clipboardHandler.ts","../../src/fixtures/clipboardFixture.ts","../../src/fixtures/contextFixture.ts","../../src/fixtures/index.ts"],"sourcesContent":["/**\n * Playwright-compatible Clipboard utilities.\n *\n * Core functions for interacting with the browser clipboard within Playwright tests.\n * Provides wrappers for reading plain text and JSON.\n *\n * @remarks\n * These utilities require 'clipboard-read' and 'clipboard-write' permissions\n * to be granted in the browser context.\n */\n\nimport type { Page } from '@playwright/test';\n\nexport class ClipboardHandler {\n constructor(private readonly page: Page) {}\n\n /**\n * Reads the current text content from the browser clipboard.\n * If the content is a JSON-encoded string (e.g., has extra quotes),\n * it will be returned as is. Use readJSON for automatic parsing.\n *\n * @returns A promise that resolves to the clipboard string content.\n */\n async read(): Promise<string> {\n return await this.page.evaluate(() => navigator.clipboard.readText());\n }\n\n /**\n * Reads the clipboard content and parses it as JSON.\n * If the content is a string literal (e.g., '\"value\"'), it returns the unwrapped string ('value').\n *\n * @template T - The expected type of the parsed JSON object.\n * @returns A promise that resolves to the parsed JSON object of type T.\n * @throws {Error} If the clipboard content is not a valid JSON string.\n */\n async readJSON<T = unknown>(): Promise<T> {\n const text = await this.read();\n try {\n return JSON.parse(text);\n } catch {\n throw new Error(\n `[playwright-clipboard] Clipboard content is not a valid JSON: ${JSON.stringify(text)}`,\n );\n }\n }\n\n /**\n * Writes the provided string data to the browser clipboard.\n * @param data The data to write to the clipboard.\n */\n async write(data: string): Promise<void> {\n await this.page.evaluate((value) => navigator.clipboard.writeText(value), data);\n }\n\n /**\n * Writes the provided data to the browser clipboard as a JSON string.\n * @param data The data to write to the clipboard. It will be stringified as JSON.\n */\n async writeJSON<T = unknown>(data: T): Promise<void> {\n const errorMessage = '[playwright-clipboard] Provided data cannot be stringified to valid JSON';\n let jsonString: string | undefined;\n\n try {\n jsonString = JSON.stringify(data);\n } catch (error) {\n const message = error instanceof Error ? error : String(error);\n throw new Error(`${errorMessage}: ${message}`);\n }\n\n if (jsonString === undefined) {\n throw new Error(`${errorMessage} (received undefined).`);\n }\n\n await this.write(jsonString);\n }\n\n /**\n * Clears the browser clipboard by writing an empty string to it.\n * This effectively removes any existing content from the clipboard.\n */\n async clear(): Promise<void> {\n await this.write('');\n }\n}\n","import type { Page, TestFixture } from '@playwright/test';\nimport { ClipboardHandler } from '../utils';\nimport type { BrowserName } from './types.ts';\n\n/**\n * A fixture that provides an instance of the Clipboard utility for interacting with the system clipboard.\n * It allows reading from the clipboard during tests.\n */\nexport const clipboardFixture: TestFixture<\n ClipboardHandler,\n { page: Page; browserName: BrowserName }\n> = async ({ page, browserName }, use) => {\n if (browserName === 'webkit') {\n throw new Error(\n `[playwright-clipboard] Browser '${browserName}' is not supported. ` +\n 'Clipboard API testing is currently supported only in Chromium-based and Firefox browsers.\\n' +\n `Add test.skip(browserName === 'webkit', 'Clipboard API is only supported in Chromium and Firefox'); to your test body.`,\n );\n }\n\n const handler = new ClipboardHandler(page);\n await use(handler);\n};\n","import type { BrowserContext, TestFixture } from '@playwright/test';\nimport type { BrowserName } from './types.ts';\n\n/**\n * A fixture that provides a BrowserContext with clipboard permissions granted for Chromium browsers.\n */\nexport const contextFixture: TestFixture<\n BrowserContext,\n { context: BrowserContext; browserName: BrowserName }\n> = async ({ context, browserName }, use) => {\n if (browserName === 'chromium') {\n await context.grantPermissions(['clipboard-read', 'clipboard-write']);\n }\n\n await use(context);\n};\n","import { ClipboardHandler } from '../utils';\nimport { clipboardFixture } from './clipboardFixture.ts';\nimport { contextFixture } from './contextFixture.ts';\n\n/**\n * A collection of fixtures related to clipboard testing, including the clipboard fixture and context fixture.\n * These fixtures can be used in Playwright tests to facilitate clipboard interactions and context management.\n */\nexport const clipboardFixtures = {\n context: contextFixture,\n clipboard: clipboardFixture,\n};\n\nexport { ClipboardHandler, clipboardFixture, contextFixture };\n"],"mappings":";AAa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