@szymonrybczak/playwright-cloudflare 0.0.2

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 ADDED
@@ -0,0 +1,5 @@
1
+ # playwright-cloudflare
2
+
3
+ This package contains the flavor of the [Playwright](https://developers.cloudflare.com/browser-rendering/platform/playwright/) library for Cloudflare's [Browser Rendering](https://developers.cloudflare.com/browser-rendering/).
4
+
5
+ See the top level [README](https://github.com/cloudflare/playwright/) of this repository for more information.
package/index.d.ts ADDED
@@ -0,0 +1,157 @@
1
+ import * as FS from 'fs';
2
+ import type { Browser } from './types/types';
3
+ import { chromium, request, selectors, devices } from './types/types';
4
+ import { env } from 'cloudflare:workers';
5
+
6
+ export * from './types/types';
7
+
8
+ declare module './types/types' {
9
+ interface Browser {
10
+ /**
11
+ * Get the Browser Rendering session ID associated with this browser
12
+ *
13
+ * @public
14
+ */
15
+ sessionId(): string;
16
+ }
17
+ }
18
+
19
+ /**
20
+ * @public
21
+ */
22
+ export interface BrowserWorker {
23
+ fetch: typeof fetch;
24
+ }
25
+
26
+ export type BrowserEndpoint = BrowserWorker | string | URL;
27
+
28
+ /**
29
+ * @public
30
+ */
31
+ export interface AcquireResponse {
32
+ sessionId: string;
33
+ }
34
+
35
+ /**
36
+ * @public
37
+ */
38
+ export interface ActiveSession {
39
+ sessionId: string;
40
+ startTime: number; // timestamp
41
+ // connection info, if present means there's a connection established
42
+ // from a worker to that session
43
+ connectionId?: string;
44
+ connectionStartTime?: number;
45
+ }
46
+
47
+ /**
48
+ * @public
49
+ */
50
+ export interface ClosedSession extends ActiveSession {
51
+ endTime: number; // timestamp
52
+ closeReason: number; // close reason code
53
+ closeReasonText: string; // close reason description
54
+ }
55
+
56
+ export interface AcquireResponse {
57
+ sessionId: string;
58
+ }
59
+
60
+ /**
61
+ * @public
62
+ */
63
+ export interface SessionsResponse {
64
+ sessions: ActiveSession[];
65
+ }
66
+
67
+ /**
68
+ * @public
69
+ */
70
+ export interface HistoryResponse {
71
+ history: ClosedSession[];
72
+ }
73
+
74
+ /**
75
+ * @public
76
+ */
77
+ export interface LimitsResponse {
78
+ activeSessions: Array<{id: string}>;
79
+ maxConcurrentSessions: number;
80
+ allowedBrowserAcquisitions: number; // 1 if allowed, 0 otherwise
81
+ timeUntilNextAllowedBrowserAcquisition: number;
82
+ }
83
+
84
+ /**
85
+ * @public
86
+ */
87
+ export interface WorkersLaunchOptions {
88
+ keep_alive?: number; // milliseconds to keep browser alive even if it has no activity (from 10_000ms to 600_000ms, default is 60_000)
89
+ }
90
+
91
+ /**
92
+ * @public
93
+ */
94
+ export interface WorkersConnectOptions {
95
+ sessionId: string; // session ID to connect to
96
+ }
97
+
98
+ // Extracts the keys whose values match a specified type `ValueType`
99
+ type KeysByValueType<T, ValueType> = {
100
+ [K in keyof T]: T[K] extends ValueType ? K : never;
101
+ }[keyof T];
102
+
103
+ export type BrowserBindingKey = KeysByValueType<typeof env, BrowserWorker>;
104
+
105
+ export function endpointURLString(binding: BrowserWorker | BrowserBindingKey, options?: WorkersLaunchOptions | WorkersConnectOptions): string;
106
+
107
+ export function connect(endpoint: string | URL): Promise<Browser>;
108
+ export function connect(endpoint: BrowserWorker, sessionIdOrOptions: string | WorkersConnectOptions): Promise<Browser>;
109
+
110
+ export function launch(endpoint: BrowserEndpoint, options?: WorkersLaunchOptions): Promise<Browser>;
111
+
112
+ export function acquire(endpoint: BrowserEndpoint, options?: WorkersLaunchOptions): Promise<AcquireResponse>;
113
+
114
+ /**
115
+ * Returns active sessions
116
+ *
117
+ * @remarks
118
+ * Sessions with a connnectionId already have a worker connection established
119
+ *
120
+ * @param endpoint - Cloudflare worker binding
121
+ * @returns List of active sessions
122
+ */
123
+ export function sessions(endpoint: BrowserEndpoint): Promise<ActiveSession[]>;
124
+
125
+ /**
126
+ * Returns recent sessions (active and closed)
127
+ *
128
+ * @param endpoint - Cloudflare worker binding
129
+ * @returns List of recent sessions (active and closed)
130
+ */
131
+ export function history(endpoint: BrowserEndpoint): Promise<ClosedSession[]>;
132
+
133
+ /**
134
+ * Returns current limits
135
+ *
136
+ * @param endpoint - Cloudflare worker binding
137
+ * @returns current limits
138
+ */
139
+ export function limits(endpoint: BrowserEndpoint): Promise<LimitsResponse>;
140
+
141
+ const playwright = {
142
+ chromium,
143
+ selectors,
144
+ request,
145
+ devices,
146
+ endpointURLString,
147
+ connect,
148
+ launch,
149
+ limits,
150
+ sessions,
151
+ history,
152
+ acquire,
153
+ };
154
+
155
+ export type Playwright = typeof playwright;
156
+
157
+ export default playwright;
package/internal.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { isUnderTest } from 'playwright-core/lib/utils';
2
+ import { BrowserBindingName } from './tests/src/utils';
3
+
4
+ export * from './tests';
5
+ export { expect, _baseTest, Fixtures, mergeTests } from './types/test';
6
+
7
+ export type TestStatus = 'passed' | 'failed' | 'timedOut' | 'skipped' | 'interrupted';
8
+
9
+ export type TestBeginPayload = {
10
+ testId: string;
11
+ startWallTime: number; // milliseconds since unix epoch
12
+ };
13
+
14
+ export type TestEndPayload = {
15
+ testId: string;
16
+ duration: number;
17
+ status: TestStatus;
18
+ errors: TestInfoError[];
19
+ hasNonRetriableError: boolean;
20
+ expectedStatus: TestStatus;
21
+ annotations: { type: string, description?: string }[];
22
+ timeout: number;
23
+ };
24
+
25
+ export type DonePayload = {
26
+ fatalErrors: TestInfoError[];
27
+ skipTestsDueToSetupFailure: string[]; // test ids
28
+ fatalUnknownTestIds?: string[];
29
+ };
30
+
31
+ export interface TestInfoError {
32
+ message?: string;
33
+ stack?: string;
34
+ value?: string;
35
+ }
36
+
37
+ export type SuiteInfo = {
38
+ type: 'file' | 'describe';
39
+ file: string;
40
+ title: string;
41
+ fullTitle: string;
42
+ entries: (SuiteInfo | TestCaseInfo)[];
43
+ }
44
+
45
+ export type TestCaseInfo = {
46
+ type: 'test';
47
+ file: string;
48
+ title: string;
49
+ fullTitle: string;
50
+ testId: string;
51
+ }
52
+
53
+ export function setCurrentTestFile(file?: string): void;
54
+ export function testSuites(): Promise<SuiteInfo[]>;
55
+
56
+ export type TestContext = {
57
+ env: Env;
58
+ sessionId: string;
59
+ assetsUrl: string;
60
+ retry: number;
61
+ binding: BrowserBindingName;
62
+ };
63
+
64
+ export function currentTestContext(): TestContext;
65
+
66
+ export type Attachment = {
67
+ name: string;
68
+ body: string;
69
+ contentType: string;
70
+ };
71
+
72
+ export type TestResult = TestEndPayload & {
73
+ attachments?: Attachment[];
74
+ };
75
+
76
+ export class TestRunner {
77
+ constructor(testContext: TestContext, options?: { timeout?: number });
78
+ runTest(file: string, testId: string): Promise<TestResult>;
79
+ }
80
+
81
+ interface Debug {
82
+ disable: () => string;
83
+ enable: (namespaces: string) => void;
84
+ enabled: (namespaces: string) => boolean;
85
+ }
86
+
87
+ export const debug: Debug;
88
+
89
+ export function isUnderTest(): boolean;
90
+
91
+ export async function runWithExpectApiListener<T>(fn: () => Promise<T>): Promise<T>;
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@szymonrybczak/playwright-cloudflare",
3
+ "description": "Playwright for Cloudflare Workers",
4
+ "version": "0.0.2",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./index.d.ts",
10
+ "default": "./lib/index.js"
11
+ },
12
+ "./test": {
13
+ "types": "./test.d.ts",
14
+ "default": "./lib/test.js"
15
+ },
16
+ "./internal": {
17
+ "types": "./internal.d.ts",
18
+ "default": "./lib/internal.js"
19
+ }
20
+ },
21
+ "scripts": {
22
+ "generate-injected:core": "cd ../.. && node ./utils/generate_injected.js",
23
+ "ci:core:bundles:utils": "cd ../playwright-core/bundles/utils && npm ci",
24
+ "ci:core:bundles:zip": "cd ../playwright-core/bundles/zip && npm ci",
25
+ "ci:test:bundles:expect": "cd ../playwright/bundles/expect && npm ci",
26
+ "ci:bundles:pngjs": "cd ./bundles/pngjs && npm ci",
27
+ "ci:bundles": "npm run ci:core:bundles:utils && npm run ci:core:bundles:zip && npm run ci:test:bundles:expect && npm run ci:bundles:pngjs",
28
+ "ci:tests": "cd tests && npm ci",
29
+ "build:bundles": "node utils/build_bundles.js",
30
+ "build:types": "node utils/copy_types.js",
31
+ "build": "npm run generate-injected:core && npm run ci:bundles && npm run build:bundles && npm run build:types && npx vite build",
32
+ "test:generate:worker": "node ./utils/generate_worker_tests.js && cd tests && npx vite build",
33
+ "test:generate:proxy": "node ./utils/generate_proxy_tests.js",
34
+ "test:deploy": "npm run test:generate:worker && npm run ci:tests && cd tests && npx wrangler deploy",
35
+ "test:dev": "npm run test:generate:worker && cd tests && npx wrangler dev",
36
+ "test:full": "npm run test:generate:proxy && cd tests && npx playwright test",
37
+ "test:smoke": "npm run test:generate:proxy && cd tests && npx playwright test --grep \"@smoke\""
38
+ },
39
+ "devDependencies": {
40
+ "@tsconfig/node18": "^18.2.6",
41
+ "pkg-pr-new": "^0.0.59",
42
+ "vite": "^6.1.0"
43
+ }
44
+ }
package/test.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './index';
2
+ export { expect, mergeExpects } from './types/test';
3
+