electron-playwright-cli 0.1.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/LICENSE +201 -0
- package/README.md +69 -0
- package/package.json +62 -0
- package/playwright-cli.js +256 -0
- package/playwright-electron/electron-context-factory.js +49 -0
- package/playwright-electron/electron-daemon.js +119 -0
- package/playwright-electron/electron-tools.js +69 -0
- package/skills/playwright-cli/SKILL.md +272 -0
- package/skills/playwright-cli/references/request-mocking.md +87 -0
- package/skills/playwright-cli/references/running-code.md +226 -0
- package/skills/playwright-cli/references/session-management.md +145 -0
- package/skills/playwright-cli/references/storage-state.md +270 -0
- package/skills/playwright-cli/references/test-generation.md +88 -0
- package/skills/playwright-cli/references/tracing.md +135 -0
- package/skills/playwright-cli/references/video-recording.md +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) Microsoft Corporation.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# electron-playwright-cli
|
|
2
|
+
|
|
3
|
+
A fork of [playwright-cli](https://github.com/microsoft/playwright-cli) that adds Electron desktop app support. All standard Playwright CLI commands work, plus Electron-specific ones for interacting with the main process and listing windows.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g electron-playwright-cli playwright
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`playwright` is a peer dependency and must be installed alongside.
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
Create `.playwright/cli.config.json` in your project root:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"browser": {
|
|
20
|
+
"launchOptions": {
|
|
21
|
+
"args": ["path/to/your/main.js"]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The `args` array is passed to Electron. Point it at your app's main process entry point.
|
|
28
|
+
|
|
29
|
+
Optional launch options:
|
|
30
|
+
- `executablePath`: path to the Electron binary (defaults to the one in node_modules)
|
|
31
|
+
- `cwd`: working directory for the Electron process
|
|
32
|
+
- `env`: additional environment variables
|
|
33
|
+
- `timeout`: launch timeout in milliseconds
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# take a snapshot of the running app
|
|
39
|
+
electron-playwright-cli snapshot
|
|
40
|
+
|
|
41
|
+
# interact with elements using refs from the snapshot
|
|
42
|
+
electron-playwright-cli click e15
|
|
43
|
+
electron-playwright-cli fill e3 "hello world"
|
|
44
|
+
electron-playwright-cli type "search query"
|
|
45
|
+
electron-playwright-cli press Enter
|
|
46
|
+
|
|
47
|
+
# take a screenshot
|
|
48
|
+
electron-playwright-cli screenshot --filename=app.png
|
|
49
|
+
|
|
50
|
+
# evaluate in the electron main process
|
|
51
|
+
electron-playwright-cli electron_evaluate "electron => electron.app.getName()"
|
|
52
|
+
|
|
53
|
+
# list all electron windows
|
|
54
|
+
electron-playwright-cli electron_windows
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
All standard commands from playwright-cli are available: `goto`, `snapshot`, `click`, `fill`, `type`, `press`, `screenshot`, `tab-list`, `tab-select`, `cookie-list`, `localstorage-get`, `route`, `console`, `network`, `tracing-start`, `tracing-stop`, `run-code`, and more. See the [skill reference](skills/playwright-cli/SKILL.md) for the full command list.
|
|
58
|
+
|
|
59
|
+
## How it works
|
|
60
|
+
|
|
61
|
+
electron-playwright-cli replaces the standard browser launch flow with a custom daemon that composes Playwright's internal `BrowserServerBackend` with an `ElectronContextFactory`. When you run a command, the CLI client spawns (or connects to) a background daemon process that launches your Electron app via `playwright._electron.launch()`, registers Electron-specific tools (like `electron_evaluate` and `electron_windows`) into Playwright's tool registry, and exposes the full Playwright CLI command surface over a Unix socket using newline-delimited JSON.
|
|
62
|
+
|
|
63
|
+
## Upstream
|
|
64
|
+
|
|
65
|
+
This project is a fork of [microsoft/playwright-cli](https://github.com/microsoft/playwright-cli), licensed under Apache-2.0.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
Apache-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "electron-playwright-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Playwright CLI with Electron app support",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"playwright",
|
|
7
|
+
"electron",
|
|
8
|
+
"cli",
|
|
9
|
+
"testing",
|
|
10
|
+
"automation",
|
|
11
|
+
"desktop"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/spacecake-labs/electron-playwright-cli.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/spacecake-labs/electron-playwright-cli",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/spacecake-labs/electron-playwright-cli/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"author": {
|
|
25
|
+
"name": "Spacecake Labs"
|
|
26
|
+
},
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"check": "npm run fmt:check && npm run lint && npm run typecheck",
|
|
30
|
+
"fmt": "oxfmt .",
|
|
31
|
+
"fmt:check": "oxfmt --check .",
|
|
32
|
+
"lint": "oxlint .",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "playwright test",
|
|
35
|
+
"changeset": "changeset",
|
|
36
|
+
"version-packages": "changeset version",
|
|
37
|
+
"release": "changeset publish"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"playwright-cli.js",
|
|
41
|
+
"playwright-electron/",
|
|
42
|
+
"skills/"
|
|
43
|
+
],
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@playwright/test": "1.59.0-alpha-1771104257000",
|
|
46
|
+
"@types/node": "^25.2.1",
|
|
47
|
+
"electron": "^35.0.0",
|
|
48
|
+
"oxfmt": "^0.7.0",
|
|
49
|
+
"oxlint": "^0.16.0",
|
|
50
|
+
"typescript": "^5.7.0",
|
|
51
|
+
"@changesets/cli": "^2.27.0"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"minimist": "^1.2.5"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"playwright": ">=1.52.0"
|
|
58
|
+
},
|
|
59
|
+
"bin": {
|
|
60
|
+
"electron-playwright-cli": "playwright-cli.js"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
// playwright CLI wrapper that adds electron support.
|
|
5
|
+
// re-implements the terminal client with shorter socket paths
|
|
6
|
+
// (macOS unix socket limit is 104 bytes) and spawns our electron
|
|
7
|
+
// daemon which composes playwright internals directly.
|
|
8
|
+
|
|
9
|
+
const crypto = require("crypto");
|
|
10
|
+
const fs = require("fs");
|
|
11
|
+
const net = require("net");
|
|
12
|
+
const os = require("os");
|
|
13
|
+
const path = require("path");
|
|
14
|
+
const cp = require("child_process");
|
|
15
|
+
|
|
16
|
+
// --- socket path computation (uses tmpdir for short paths) ---
|
|
17
|
+
|
|
18
|
+
const installationHash = crypto
|
|
19
|
+
.createHash("sha1")
|
|
20
|
+
.update(require.resolve("playwright/package.json"))
|
|
21
|
+
.digest("hex")
|
|
22
|
+
.substring(0, 16);
|
|
23
|
+
|
|
24
|
+
function daemonSocketDir() {
|
|
25
|
+
const socketsDir = path.join(os.tmpdir(), "playwright-cli");
|
|
26
|
+
return path.join(socketsDir, installationHash);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function daemonSocketPath(sessionName) {
|
|
30
|
+
if (os.platform() === "win32")
|
|
31
|
+
return `\\\\.\\pipe\\${installationHash}-${sessionName}.sock`;
|
|
32
|
+
const socketPath = path.join(daemonSocketDir(), `${sessionName}.sock`);
|
|
33
|
+
if (Buffer.byteLength(socketPath) > 104)
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Socket path too long (${Buffer.byteLength(socketPath)} bytes, max 104). Use a shorter --session name.`,
|
|
36
|
+
);
|
|
37
|
+
return socketPath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// --- socket session (newline-delimited JSON protocol) ---
|
|
41
|
+
|
|
42
|
+
class SocketSession {
|
|
43
|
+
constructor(socket) {
|
|
44
|
+
this._nextId = 1;
|
|
45
|
+
this._callbacks = new Map();
|
|
46
|
+
this._pendingBuffers = [];
|
|
47
|
+
this._socket = socket;
|
|
48
|
+
|
|
49
|
+
socket.on("data", (buf) => this._onData(buf));
|
|
50
|
+
socket.on("close", () => this.dispose());
|
|
51
|
+
socket.on("error", () => {});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async runCliCommand(args) {
|
|
55
|
+
return this._send("runCliCommand", { args });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async _send(method, params = {}) {
|
|
59
|
+
const id = this._nextId++;
|
|
60
|
+
const msg = JSON.stringify({ id, method, params }) + "\n";
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
this._socket.write(msg, (err) => (err ? reject(err) : resolve()));
|
|
63
|
+
});
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
this._callbacks.set(id, { resolve, reject });
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
dispose() {
|
|
70
|
+
for (const cb of this._callbacks.values()) cb.reject(new Error("Disposed"));
|
|
71
|
+
this._callbacks.clear();
|
|
72
|
+
this._socket.destroy();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_onData(buffer) {
|
|
76
|
+
let end = buffer.indexOf("\n");
|
|
77
|
+
if (end === -1) {
|
|
78
|
+
this._pendingBuffers.push(buffer);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
this._pendingBuffers.push(buffer.slice(0, end));
|
|
82
|
+
this._dispatch(Buffer.concat(this._pendingBuffers).toString());
|
|
83
|
+
let start = end + 1;
|
|
84
|
+
end = buffer.indexOf("\n", start);
|
|
85
|
+
while (end !== -1) {
|
|
86
|
+
this._dispatch(buffer.toString(undefined, start, end));
|
|
87
|
+
start = end + 1;
|
|
88
|
+
end = buffer.indexOf("\n", start);
|
|
89
|
+
}
|
|
90
|
+
this._pendingBuffers = [buffer.slice(start)];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_dispatch(message) {
|
|
94
|
+
let obj;
|
|
95
|
+
try {
|
|
96
|
+
obj = JSON.parse(message);
|
|
97
|
+
} catch {
|
|
98
|
+
console.error("Failed to parse daemon message:", message);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (obj.id && this._callbacks.has(obj.id)) {
|
|
102
|
+
const cb = this._callbacks.get(obj.id);
|
|
103
|
+
this._callbacks.delete(obj.id);
|
|
104
|
+
if (obj.error) cb.reject(new Error(obj.error));
|
|
105
|
+
else cb.resolve(obj.result);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// --- daemon management ---
|
|
111
|
+
|
|
112
|
+
async function socketExists(socketPath) {
|
|
113
|
+
try {
|
|
114
|
+
const stat = await fs.promises.stat(socketPath);
|
|
115
|
+
return stat?.isSocket();
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function connectToSocket(socketPath) {
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
const socket = net.createConnection(socketPath, () => resolve(socket));
|
|
124
|
+
socket.on("error", reject);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function connectToDaemon(sessionName) {
|
|
129
|
+
const socketPath = daemonSocketPath(sessionName);
|
|
130
|
+
|
|
131
|
+
// try to connect to existing daemon
|
|
132
|
+
if (await socketExists(socketPath)) {
|
|
133
|
+
try {
|
|
134
|
+
const socket = await connectToSocket(socketPath);
|
|
135
|
+
return new SocketSession(socket);
|
|
136
|
+
} catch {
|
|
137
|
+
if (os.platform() !== "win32")
|
|
138
|
+
await fs.promises.unlink(socketPath).catch(() => {});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ensure socket directory exists
|
|
143
|
+
await fs.promises.mkdir(daemonSocketDir(), { recursive: true });
|
|
144
|
+
|
|
145
|
+
// spawn the electron daemon
|
|
146
|
+
const daemonScript = path.resolve(
|
|
147
|
+
__dirname,
|
|
148
|
+
"playwright-electron",
|
|
149
|
+
"electron-daemon.js",
|
|
150
|
+
);
|
|
151
|
+
const userDataDir = path.resolve(
|
|
152
|
+
daemonSocketDir(),
|
|
153
|
+
`${sessionName}-user-data`,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const args = [
|
|
157
|
+
daemonScript,
|
|
158
|
+
`--daemon=${socketPath}`,
|
|
159
|
+
`--user-data-dir=${userDataDir}`,
|
|
160
|
+
];
|
|
161
|
+
|
|
162
|
+
// auto-inject config file if available
|
|
163
|
+
const configFile =
|
|
164
|
+
process.env.PLAYWRIGHT_MCP_CONFIG ||
|
|
165
|
+
path.resolve(process.cwd(), ".playwright", "cli.config.json");
|
|
166
|
+
if (fs.existsSync(configFile)) args.push(`--config=${configFile}`);
|
|
167
|
+
|
|
168
|
+
const stderrLog = path.join(daemonSocketDir(), `${sessionName}-stderr.log`);
|
|
169
|
+
const stderrFd = fs.openSync(stderrLog, "w");
|
|
170
|
+
const child = cp.spawn(process.execPath, args, {
|
|
171
|
+
detached: true,
|
|
172
|
+
stdio: ["ignore", "ignore", stderrFd],
|
|
173
|
+
cwd: process.cwd(),
|
|
174
|
+
});
|
|
175
|
+
child.unref();
|
|
176
|
+
fs.closeSync(stderrFd);
|
|
177
|
+
|
|
178
|
+
// retry connection
|
|
179
|
+
const maxRetries = 50;
|
|
180
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
181
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
182
|
+
try {
|
|
183
|
+
const socket = await connectToSocket(socketPath);
|
|
184
|
+
return new SocketSession(socket);
|
|
185
|
+
} catch (e) {
|
|
186
|
+
if (e.code !== "ENOENT" && e.code !== "ECONNREFUSED") throw e;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
let detail = "";
|
|
190
|
+
try {
|
|
191
|
+
detail = fs.readFileSync(stderrLog, "utf-8").trim();
|
|
192
|
+
} catch {}
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Failed to connect to daemon at ${socketPath} after ${maxRetries * 100}ms` +
|
|
195
|
+
(detail ? `\nDaemon stderr:\n${detail}` : ""),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// --- main ---
|
|
200
|
+
|
|
201
|
+
async function main() {
|
|
202
|
+
const argv = process.argv.slice(2);
|
|
203
|
+
const args = require("minimist")(argv);
|
|
204
|
+
|
|
205
|
+
if (args.version || args.v) {
|
|
206
|
+
const pkg = require("playwright/package.json");
|
|
207
|
+
console.log(pkg.version);
|
|
208
|
+
process.exit(0);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// load help from playwright's terminal CLI
|
|
212
|
+
const playwrightDir = path.dirname(
|
|
213
|
+
require.resolve("playwright/package.json"),
|
|
214
|
+
);
|
|
215
|
+
let help;
|
|
216
|
+
try {
|
|
217
|
+
help = require(path.join(playwrightDir, "lib/mcp/terminal/help.json"));
|
|
218
|
+
} catch {
|
|
219
|
+
help = {
|
|
220
|
+
commands: {},
|
|
221
|
+
global: "electron-playwright-cli <command> [options]",
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const commandName = args._[0];
|
|
226
|
+
|
|
227
|
+
if (args.help || args.h) {
|
|
228
|
+
if (help.commands[commandName]) {
|
|
229
|
+
console.log(help.commands[commandName]);
|
|
230
|
+
} else {
|
|
231
|
+
console.log(
|
|
232
|
+
"electron-playwright-cli - run playwright mcp commands for electron apps\n",
|
|
233
|
+
);
|
|
234
|
+
console.log(help.global);
|
|
235
|
+
}
|
|
236
|
+
process.exit(0);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!commandName) {
|
|
240
|
+
console.error("No command specified.\n");
|
|
241
|
+
console.log(help.global);
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const sessionName =
|
|
246
|
+
args.session || process.env.PLAYWRIGHT_CLI_SESSION || "default";
|
|
247
|
+
const session = await connectToDaemon(sessionName);
|
|
248
|
+
const result = await session.runCliCommand(args);
|
|
249
|
+
console.log(result);
|
|
250
|
+
session.dispose();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
main().catch((e) => {
|
|
254
|
+
console.error(e.message);
|
|
255
|
+
process.exit(1);
|
|
256
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// electron context factory — launches an electron app and returns
|
|
4
|
+
// its browser context for the playwright CLI daemon.
|
|
5
|
+
|
|
6
|
+
const playwright = require("playwright-core");
|
|
7
|
+
|
|
8
|
+
class ElectronContextFactory {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async createContext(_clientInfo, _abortSignal, _options) {
|
|
14
|
+
const launchOptions = this.config.browser.launchOptions || {};
|
|
15
|
+
const electronApp = await playwright._electron.launch({
|
|
16
|
+
executablePath: launchOptions.executablePath,
|
|
17
|
+
args: launchOptions.args || ["."],
|
|
18
|
+
cwd: launchOptions.cwd || process.cwd(),
|
|
19
|
+
env: launchOptions.env
|
|
20
|
+
? { ...process.env, ...launchOptions.env }
|
|
21
|
+
: undefined,
|
|
22
|
+
timeout: launchOptions.timeout,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const browserContext = electronApp.context();
|
|
27
|
+
// store the electron app reference on the context for tools
|
|
28
|
+
// @ts-ignore — custom property used by electron-tools.js
|
|
29
|
+
browserContext._electronApp = electronApp;
|
|
30
|
+
|
|
31
|
+
// if there are no pages yet, wait for the first window
|
|
32
|
+
if (browserContext.pages().length === 0) {
|
|
33
|
+
await electronApp.firstWindow();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
browserContext,
|
|
38
|
+
close: async () => {
|
|
39
|
+
await electronApp.close().catch(() => {});
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
} catch (e) {
|
|
43
|
+
await electronApp.close().catch(() => {});
|
|
44
|
+
throw e;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = { ElectronContextFactory };
|