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
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# Running Custom Playwright Code
|
|
2
|
+
|
|
3
|
+
Use `run-code` to execute arbitrary Playwright code for advanced scenarios not covered by CLI commands.
|
|
4
|
+
|
|
5
|
+
## Syntax
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
electron-playwright-cli run-code "async page => {
|
|
9
|
+
// Your Playwright code here
|
|
10
|
+
// Access page.context() for browser context operations
|
|
11
|
+
}"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Geolocation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# Grant geolocation permission and set location
|
|
18
|
+
electron-playwright-cli run-code "async page => {
|
|
19
|
+
await page.context().grantPermissions(['geolocation']);
|
|
20
|
+
await page.context().setGeolocation({ latitude: 37.7749, longitude: -122.4194 });
|
|
21
|
+
}"
|
|
22
|
+
|
|
23
|
+
# Set location to London
|
|
24
|
+
electron-playwright-cli run-code "async page => {
|
|
25
|
+
await page.context().grantPermissions(['geolocation']);
|
|
26
|
+
await page.context().setGeolocation({ latitude: 51.5074, longitude: -0.1278 });
|
|
27
|
+
}"
|
|
28
|
+
|
|
29
|
+
# Clear geolocation override
|
|
30
|
+
electron-playwright-cli run-code "async page => {
|
|
31
|
+
await page.context().clearPermissions();
|
|
32
|
+
}"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Permissions
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Grant multiple permissions
|
|
39
|
+
electron-playwright-cli run-code "async page => {
|
|
40
|
+
await page.context().grantPermissions([
|
|
41
|
+
'geolocation',
|
|
42
|
+
'notifications',
|
|
43
|
+
'camera',
|
|
44
|
+
'microphone'
|
|
45
|
+
]);
|
|
46
|
+
}"
|
|
47
|
+
|
|
48
|
+
# Grant permissions for specific origin
|
|
49
|
+
electron-playwright-cli run-code "async page => {
|
|
50
|
+
await page.context().grantPermissions(['clipboard-read'], {
|
|
51
|
+
origin: 'file://'
|
|
52
|
+
});
|
|
53
|
+
}"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Media Emulation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Emulate dark color scheme
|
|
60
|
+
electron-playwright-cli run-code "async page => {
|
|
61
|
+
await page.emulateMedia({ colorScheme: 'dark' });
|
|
62
|
+
}"
|
|
63
|
+
|
|
64
|
+
# Emulate light color scheme
|
|
65
|
+
electron-playwright-cli run-code "async page => {
|
|
66
|
+
await page.emulateMedia({ colorScheme: 'light' });
|
|
67
|
+
}"
|
|
68
|
+
|
|
69
|
+
# Emulate reduced motion
|
|
70
|
+
electron-playwright-cli run-code "async page => {
|
|
71
|
+
await page.emulateMedia({ reducedMotion: 'reduce' });
|
|
72
|
+
}"
|
|
73
|
+
|
|
74
|
+
# Emulate print media
|
|
75
|
+
electron-playwright-cli run-code "async page => {
|
|
76
|
+
await page.emulateMedia({ media: 'print' });
|
|
77
|
+
}"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Wait Strategies
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Wait for network idle
|
|
84
|
+
electron-playwright-cli run-code "async page => {
|
|
85
|
+
await page.waitForLoadState('networkidle');
|
|
86
|
+
}"
|
|
87
|
+
|
|
88
|
+
# Wait for specific element
|
|
89
|
+
electron-playwright-cli run-code "async page => {
|
|
90
|
+
await page.waitForSelector('.loading', { state: 'hidden' });
|
|
91
|
+
}"
|
|
92
|
+
|
|
93
|
+
# Wait for function to return true
|
|
94
|
+
electron-playwright-cli run-code "async page => {
|
|
95
|
+
await page.waitForFunction(() => window.appReady === true);
|
|
96
|
+
}"
|
|
97
|
+
|
|
98
|
+
# Wait with timeout
|
|
99
|
+
electron-playwright-cli run-code "async page => {
|
|
100
|
+
await page.waitForSelector('.result', { timeout: 10000 });
|
|
101
|
+
}"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Frames and Iframes
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# Work with iframe
|
|
108
|
+
electron-playwright-cli run-code "async page => {
|
|
109
|
+
const frame = page.locator('iframe#my-iframe').contentFrame();
|
|
110
|
+
await frame.locator('button').click();
|
|
111
|
+
}"
|
|
112
|
+
|
|
113
|
+
# Get all frames
|
|
114
|
+
electron-playwright-cli run-code "async page => {
|
|
115
|
+
const frames = page.frames();
|
|
116
|
+
return frames.map(f => f.url());
|
|
117
|
+
}"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## File Downloads
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# Handle file download
|
|
124
|
+
electron-playwright-cli run-code "async page => {
|
|
125
|
+
const [download] = await Promise.all([
|
|
126
|
+
page.waitForEvent('download'),
|
|
127
|
+
page.click('a.download-link')
|
|
128
|
+
]);
|
|
129
|
+
await download.saveAs('./downloaded-file.pdf');
|
|
130
|
+
return download.suggestedFilename();
|
|
131
|
+
}"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Clipboard
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# Read clipboard (requires permission)
|
|
138
|
+
electron-playwright-cli run-code "async page => {
|
|
139
|
+
await page.context().grantPermissions(['clipboard-read']);
|
|
140
|
+
return await page.evaluate(() => navigator.clipboard.readText());
|
|
141
|
+
}"
|
|
142
|
+
|
|
143
|
+
# Write to clipboard
|
|
144
|
+
electron-playwright-cli run-code "async page => {
|
|
145
|
+
await page.evaluate(text => navigator.clipboard.writeText(text), 'Hello clipboard!');
|
|
146
|
+
}"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Page Information
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# Get page title
|
|
153
|
+
electron-playwright-cli run-code "async page => {
|
|
154
|
+
return await page.title();
|
|
155
|
+
}"
|
|
156
|
+
|
|
157
|
+
# Get current URL
|
|
158
|
+
electron-playwright-cli run-code "async page => {
|
|
159
|
+
return page.url();
|
|
160
|
+
}"
|
|
161
|
+
|
|
162
|
+
# Get page content
|
|
163
|
+
electron-playwright-cli run-code "async page => {
|
|
164
|
+
return await page.content();
|
|
165
|
+
}"
|
|
166
|
+
|
|
167
|
+
# Get viewport size
|
|
168
|
+
electron-playwright-cli run-code "async page => {
|
|
169
|
+
return page.viewportSize();
|
|
170
|
+
}"
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## JavaScript Execution
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
# Execute JavaScript and return result
|
|
177
|
+
electron-playwright-cli run-code "async page => {
|
|
178
|
+
return await page.evaluate(() => {
|
|
179
|
+
return {
|
|
180
|
+
userAgent: navigator.userAgent,
|
|
181
|
+
language: navigator.language,
|
|
182
|
+
cookiesEnabled: navigator.cookieEnabled
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
}"
|
|
186
|
+
|
|
187
|
+
# Pass arguments to evaluate
|
|
188
|
+
electron-playwright-cli run-code "async page => {
|
|
189
|
+
const multiplier = 5;
|
|
190
|
+
return await page.evaluate(m => document.querySelectorAll('li').length * m, multiplier);
|
|
191
|
+
}"
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Error Handling
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
# Try-catch in run-code
|
|
198
|
+
electron-playwright-cli run-code "async page => {
|
|
199
|
+
try {
|
|
200
|
+
await page.click('.maybe-missing', { timeout: 1000 });
|
|
201
|
+
return 'clicked';
|
|
202
|
+
} catch (e) {
|
|
203
|
+
return 'element not found';
|
|
204
|
+
}
|
|
205
|
+
}"
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Complex Workflows
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
# Save app state
|
|
212
|
+
electron-playwright-cli run-code "async page => {
|
|
213
|
+
await page.fill('input[name=email]', 'user@example.com');
|
|
214
|
+
await page.fill('input[name=password]', 'secret');
|
|
215
|
+
await page.click('button[type=submit]');
|
|
216
|
+
await page.waitForSelector('.dashboard');
|
|
217
|
+
await page.context().storageState({ path: 'auth.json' });
|
|
218
|
+
return 'Login successful';
|
|
219
|
+
}"
|
|
220
|
+
|
|
221
|
+
# Extract data from a list
|
|
222
|
+
electron-playwright-cli run-code "async page => {
|
|
223
|
+
const items = await page.locator('.item').allTextContents();
|
|
224
|
+
return items;
|
|
225
|
+
}"
|
|
226
|
+
```
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Session Management
|
|
2
|
+
|
|
3
|
+
Run multiple isolated Electron app sessions concurrently with state persistence.
|
|
4
|
+
|
|
5
|
+
## Named Sessions
|
|
6
|
+
|
|
7
|
+
Use `-s` flag to isolate Electron app instances:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# Session 1: Main app interaction
|
|
11
|
+
electron-playwright-cli -s=main snapshot
|
|
12
|
+
|
|
13
|
+
# Session 2: Separate instance (independent cookies, storage)
|
|
14
|
+
electron-playwright-cli -s=secondary snapshot
|
|
15
|
+
|
|
16
|
+
# Commands are isolated by session
|
|
17
|
+
electron-playwright-cli -s=main fill e1 "user@example.com"
|
|
18
|
+
electron-playwright-cli -s=secondary click e3
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Session Isolation Properties
|
|
22
|
+
|
|
23
|
+
Each session has independent:
|
|
24
|
+
- Cookies
|
|
25
|
+
- LocalStorage / SessionStorage
|
|
26
|
+
- IndexedDB
|
|
27
|
+
- Cache
|
|
28
|
+
- Open windows
|
|
29
|
+
|
|
30
|
+
## Session Commands
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# List all sessions
|
|
34
|
+
electron-playwright-cli list
|
|
35
|
+
|
|
36
|
+
# Stop a session (close the Electron app)
|
|
37
|
+
electron-playwright-cli close # stop the default session
|
|
38
|
+
electron-playwright-cli -s=mysession close # stop a named session
|
|
39
|
+
|
|
40
|
+
# Stop all sessions
|
|
41
|
+
electron-playwright-cli close-all
|
|
42
|
+
|
|
43
|
+
# Forcefully kill all daemon processes (for stale/zombie processes)
|
|
44
|
+
electron-playwright-cli kill-all
|
|
45
|
+
|
|
46
|
+
# Delete session user data (profile directory)
|
|
47
|
+
electron-playwright-cli delete-data # delete default session data
|
|
48
|
+
electron-playwright-cli -s=mysession delete-data # delete named session data
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Environment Variable
|
|
52
|
+
|
|
53
|
+
Set a default session name via environment variable:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
export PLAYWRIGHT_CLI_SESSION="mysession"
|
|
57
|
+
electron-playwright-cli snapshot # Uses "mysession" automatically
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Common Patterns
|
|
61
|
+
|
|
62
|
+
### Multiple App Instances
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Launch two instances of your Electron app
|
|
66
|
+
electron-playwright-cli -s=instance1 snapshot
|
|
67
|
+
electron-playwright-cli -s=instance2 snapshot
|
|
68
|
+
|
|
69
|
+
# Interact with each independently
|
|
70
|
+
electron-playwright-cli -s=instance1 fill e1 "user-a@example.com"
|
|
71
|
+
electron-playwright-cli -s=instance2 fill e1 "user-b@example.com"
|
|
72
|
+
|
|
73
|
+
# Compare screenshots
|
|
74
|
+
electron-playwright-cli -s=instance1 screenshot --filename=instance1.png
|
|
75
|
+
electron-playwright-cli -s=instance2 screenshot --filename=instance2.png
|
|
76
|
+
|
|
77
|
+
# Cleanup
|
|
78
|
+
electron-playwright-cli close-all
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Testing Different App States
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Test with different configurations or data
|
|
85
|
+
electron-playwright-cli -s=fresh snapshot
|
|
86
|
+
electron-playwright-cli -s=with-data snapshot
|
|
87
|
+
|
|
88
|
+
# Take screenshots for comparison
|
|
89
|
+
electron-playwright-cli -s=fresh screenshot
|
|
90
|
+
electron-playwright-cli -s=with-data screenshot
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Default Session
|
|
94
|
+
|
|
95
|
+
When `-s` is omitted, commands use the default session:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
# These use the same default session
|
|
99
|
+
electron-playwright-cli snapshot
|
|
100
|
+
electron-playwright-cli click e3
|
|
101
|
+
electron-playwright-cli close # Stops the default session
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Session Configuration
|
|
105
|
+
|
|
106
|
+
Configure a session with a specific config file:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# Use a custom config file
|
|
110
|
+
electron-playwright-cli --config=.playwright/my-cli.json snapshot
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Best Practices
|
|
114
|
+
|
|
115
|
+
### 1. Name Sessions Semantically
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# GOOD: Clear purpose
|
|
119
|
+
electron-playwright-cli -s=login-test snapshot
|
|
120
|
+
electron-playwright-cli -s=settings-test snapshot
|
|
121
|
+
|
|
122
|
+
# AVOID: Generic names
|
|
123
|
+
electron-playwright-cli -s=s1 snapshot
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 2. Always Clean Up
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
# Stop sessions when done
|
|
130
|
+
electron-playwright-cli -s=login-test close
|
|
131
|
+
electron-playwright-cli -s=settings-test close
|
|
132
|
+
|
|
133
|
+
# Or stop all at once
|
|
134
|
+
electron-playwright-cli close-all
|
|
135
|
+
|
|
136
|
+
# If sessions become unresponsive or zombie processes remain
|
|
137
|
+
electron-playwright-cli kill-all
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 3. Delete Stale Session Data
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Remove old session data to free disk space
|
|
144
|
+
electron-playwright-cli -s=oldsession delete-data
|
|
145
|
+
```
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# Storage Management
|
|
2
|
+
|
|
3
|
+
Manage cookies, localStorage, sessionStorage, and storage state in your Electron app.
|
|
4
|
+
|
|
5
|
+
## Storage State
|
|
6
|
+
|
|
7
|
+
Save and restore complete app state including cookies and storage.
|
|
8
|
+
|
|
9
|
+
### Save Storage State
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Save to auto-generated filename (storage-state-{timestamp}.json)
|
|
13
|
+
electron-playwright-cli state-save
|
|
14
|
+
|
|
15
|
+
# Save to specific filename
|
|
16
|
+
electron-playwright-cli state-save my-state.json
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Restore Storage State
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Load storage state from file
|
|
23
|
+
electron-playwright-cli state-load my-state.json
|
|
24
|
+
|
|
25
|
+
# Reload the app to apply
|
|
26
|
+
electron-playwright-cli reload
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Storage State File Format
|
|
30
|
+
|
|
31
|
+
The saved file contains:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"cookies": [
|
|
36
|
+
{
|
|
37
|
+
"name": "session_id",
|
|
38
|
+
"value": "abc123",
|
|
39
|
+
"domain": "example.com",
|
|
40
|
+
"path": "/",
|
|
41
|
+
"expires": 1735689600,
|
|
42
|
+
"httpOnly": true,
|
|
43
|
+
"secure": true,
|
|
44
|
+
"sameSite": "Lax"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"origins": [
|
|
48
|
+
{
|
|
49
|
+
"origin": "https://example.com",
|
|
50
|
+
"localStorage": [
|
|
51
|
+
{ "name": "theme", "value": "dark" },
|
|
52
|
+
{ "name": "user_id", "value": "12345" }
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Cookies
|
|
60
|
+
|
|
61
|
+
### List All Cookies
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
electron-playwright-cli cookie-list
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Filter Cookies by Domain
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
electron-playwright-cli cookie-list --domain=example.com
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Filter Cookies by Path
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
electron-playwright-cli cookie-list --path=/api
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Get Specific Cookie
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
electron-playwright-cli cookie-get session_id
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Set a Cookie
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Basic cookie
|
|
89
|
+
electron-playwright-cli cookie-set session abc123
|
|
90
|
+
|
|
91
|
+
# Cookie with options
|
|
92
|
+
electron-playwright-cli cookie-set session abc123 --domain=example.com --path=/ --httpOnly --secure --sameSite=Lax
|
|
93
|
+
|
|
94
|
+
# Cookie with expiration (Unix timestamp)
|
|
95
|
+
electron-playwright-cli cookie-set remember_me token123 --expires=1735689600
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Delete a Cookie
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
electron-playwright-cli cookie-delete session_id
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Clear All Cookies
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
electron-playwright-cli cookie-clear
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Advanced: Multiple Cookies or Custom Options
|
|
111
|
+
|
|
112
|
+
For complex scenarios like adding multiple cookies at once, use `run-code`:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
electron-playwright-cli run-code "async page => {
|
|
116
|
+
await page.context().addCookies([
|
|
117
|
+
{ name: 'session_id', value: 'sess_abc123', domain: 'example.com', path: '/', httpOnly: true },
|
|
118
|
+
{ name: 'preferences', value: JSON.stringify({ theme: 'dark' }), domain: 'example.com', path: '/' }
|
|
119
|
+
]);
|
|
120
|
+
}"
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Local Storage
|
|
124
|
+
|
|
125
|
+
### List All localStorage Items
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
electron-playwright-cli localstorage-list
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Get Single Value
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
electron-playwright-cli localstorage-get token
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Set Value
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
electron-playwright-cli localstorage-set theme dark
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Set JSON Value
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
electron-playwright-cli localstorage-set user_settings '{"theme":"dark","language":"en"}'
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Delete Single Item
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
electron-playwright-cli localstorage-delete token
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Clear All localStorage
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
electron-playwright-cli localstorage-clear
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Advanced: Multiple Operations
|
|
162
|
+
|
|
163
|
+
For complex scenarios like setting multiple values at once, use `run-code`:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
electron-playwright-cli run-code "async page => {
|
|
167
|
+
await page.evaluate(() => {
|
|
168
|
+
localStorage.setItem('token', 'jwt_abc123');
|
|
169
|
+
localStorage.setItem('user_id', '12345');
|
|
170
|
+
localStorage.setItem('expires_at', Date.now() + 3600000);
|
|
171
|
+
});
|
|
172
|
+
}"
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Session Storage
|
|
176
|
+
|
|
177
|
+
### List All sessionStorage Items
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
electron-playwright-cli sessionstorage-list
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Get Single Value
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
electron-playwright-cli sessionstorage-get form_data
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Set Value
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
electron-playwright-cli sessionstorage-set step 3
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Delete Single Item
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
electron-playwright-cli sessionstorage-delete step
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Clear sessionStorage
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
electron-playwright-cli sessionstorage-clear
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## IndexedDB
|
|
208
|
+
|
|
209
|
+
### List Databases
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
electron-playwright-cli run-code "async page => {
|
|
213
|
+
return await page.evaluate(async () => {
|
|
214
|
+
const databases = await indexedDB.databases();
|
|
215
|
+
return databases;
|
|
216
|
+
});
|
|
217
|
+
}"
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Delete Database
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
electron-playwright-cli run-code "async page => {
|
|
224
|
+
await page.evaluate(() => {
|
|
225
|
+
indexedDB.deleteDatabase('myDatabase');
|
|
226
|
+
});
|
|
227
|
+
}"
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Common Patterns
|
|
231
|
+
|
|
232
|
+
### Save and Restore App State
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Step 1: Set up the app state
|
|
236
|
+
electron-playwright-cli snapshot
|
|
237
|
+
electron-playwright-cli fill e1 "user@example.com"
|
|
238
|
+
electron-playwright-cli fill e2 "password123"
|
|
239
|
+
electron-playwright-cli click e3
|
|
240
|
+
|
|
241
|
+
# Save the state
|
|
242
|
+
electron-playwright-cli state-save app-state.json
|
|
243
|
+
|
|
244
|
+
# Step 2: Later, restore state and skip setup
|
|
245
|
+
electron-playwright-cli state-load app-state.json
|
|
246
|
+
electron-playwright-cli reload
|
|
247
|
+
# App state is restored!
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Inspect and Modify Storage
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
# Check current storage
|
|
254
|
+
electron-playwright-cli cookie-list
|
|
255
|
+
electron-playwright-cli localstorage-list
|
|
256
|
+
|
|
257
|
+
# Modify values
|
|
258
|
+
electron-playwright-cli localstorage-set theme dark
|
|
259
|
+
electron-playwright-cli cookie-set debug true
|
|
260
|
+
|
|
261
|
+
# Reload to see effect
|
|
262
|
+
electron-playwright-cli reload
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Security Notes
|
|
266
|
+
|
|
267
|
+
- Never commit storage state files containing auth tokens
|
|
268
|
+
- Add `*.auth-state.json` to `.gitignore`
|
|
269
|
+
- Delete state files after automation completes
|
|
270
|
+
- Use environment variables for sensitive data
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Test Generation
|
|
2
|
+
|
|
3
|
+
Generate Playwright test code automatically as you interact with your Electron app.
|
|
4
|
+
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
Every action you perform with `electron-playwright-cli` generates corresponding Playwright TypeScript code.
|
|
8
|
+
This code appears in the output and can be copied directly into your test files.
|
|
9
|
+
|
|
10
|
+
## Example Workflow
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# Take a snapshot to see elements
|
|
14
|
+
electron-playwright-cli snapshot
|
|
15
|
+
# Output shows: e1 [textbox "Email"], e2 [textbox "Password"], e3 [button "Sign In"]
|
|
16
|
+
|
|
17
|
+
# Fill form fields - generates code automatically
|
|
18
|
+
electron-playwright-cli fill e1 "user@example.com"
|
|
19
|
+
# Ran Playwright code:
|
|
20
|
+
# await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
|
|
21
|
+
|
|
22
|
+
electron-playwright-cli fill e2 "password123"
|
|
23
|
+
# Ran Playwright code:
|
|
24
|
+
# await page.getByRole('textbox', { name: 'Password' }).fill('password123');
|
|
25
|
+
|
|
26
|
+
electron-playwright-cli click e3
|
|
27
|
+
# Ran Playwright code:
|
|
28
|
+
# await page.getByRole('button', { name: 'Sign In' }).click();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Building a Test File
|
|
32
|
+
|
|
33
|
+
Collect the generated code into a Playwright test for your Electron app:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { test, expect, _electron as electron } from '@playwright/test';
|
|
37
|
+
|
|
38
|
+
test('login flow', async () => {
|
|
39
|
+
const electronApp = await electron.launch({ args: ['path/to/main.js'] });
|
|
40
|
+
const page = await electronApp.firstWindow();
|
|
41
|
+
|
|
42
|
+
// Generated code from electron-playwright-cli session:
|
|
43
|
+
await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
|
|
44
|
+
await page.getByRole('textbox', { name: 'Password' }).fill('password123');
|
|
45
|
+
await page.getByRole('button', { name: 'Sign In' }).click();
|
|
46
|
+
|
|
47
|
+
// Add assertions
|
|
48
|
+
await expect(page.getByText('Dashboard')).toBeVisible();
|
|
49
|
+
|
|
50
|
+
await electronApp.close();
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Best Practices
|
|
55
|
+
|
|
56
|
+
### 1. Use Semantic Locators
|
|
57
|
+
|
|
58
|
+
The generated code uses role-based locators when possible, which are more resilient:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Generated (good - semantic)
|
|
62
|
+
await page.getByRole('button', { name: 'Submit' }).click();
|
|
63
|
+
|
|
64
|
+
// Avoid (fragile - CSS selectors)
|
|
65
|
+
await page.locator('#submit-btn').click();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 2. Explore Before Recording
|
|
69
|
+
|
|
70
|
+
Take snapshots to understand the app structure before recording actions:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
electron-playwright-cli snapshot
|
|
74
|
+
# Review the element structure
|
|
75
|
+
electron-playwright-cli click e5
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 3. Add Assertions Manually
|
|
79
|
+
|
|
80
|
+
Generated code captures actions but not assertions. Add expectations in your test:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Generated action
|
|
84
|
+
await page.getByRole('button', { name: 'Submit' }).click();
|
|
85
|
+
|
|
86
|
+
// Manual assertion
|
|
87
|
+
await expect(page.getByText('Success')).toBeVisible();
|
|
88
|
+
```
|