browser-commander 0.6.0 → 0.8.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/CHANGELOG.md +35 -0
- package/README.md +32 -0
- package/examples/pdf-generation.js +76 -0
- package/package.json +1 -1
- package/src/README.md +31 -0
- package/src/bindings.js +34 -0
- package/src/browser/launcher.js +27 -2
- package/src/browser/media.js +57 -0
- package/src/browser/pdf.js +31 -0
- package/src/core/dialog-manager.js +158 -0
- package/src/core/engine-adapter.js +97 -0
- package/src/exports.js +6 -0
- package/src/factory.js +37 -0
- package/src/interactions/keyboard.js +91 -0
- package/tests/e2e/playwright.e2e.test.js +84 -0
- package/tests/e2e/puppeteer.e2e.test.js +71 -0
- package/tests/helpers/mocks.js +8 -0
- package/tests/unit/bindings.test.js +72 -0
- package/tests/unit/browser/media.test.js +176 -0
- package/tests/unit/browser/pdf.test.js +80 -0
- package/tests/unit/core/dialog-manager.test.js +310 -0
- package/tests/unit/core/engine-adapter.test.js +21 -0
- package/tests/unit/interactions/keyboard.test.js +316 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import {
|
|
4
|
+
pressKey,
|
|
5
|
+
typeText,
|
|
6
|
+
keyDown,
|
|
7
|
+
keyUp,
|
|
8
|
+
} from '../../../src/interactions/keyboard.js';
|
|
9
|
+
import {
|
|
10
|
+
PlaywrightAdapter,
|
|
11
|
+
PuppeteerAdapter,
|
|
12
|
+
} from '../../../src/core/engine-adapter.js';
|
|
13
|
+
import {
|
|
14
|
+
createMockPlaywrightPage,
|
|
15
|
+
createMockPuppeteerPage,
|
|
16
|
+
} from '../../helpers/mocks.js';
|
|
17
|
+
|
|
18
|
+
describe('keyboard interactions', () => {
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// pressKey
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
describe('pressKey', () => {
|
|
23
|
+
it('should press a key using Playwright adapter', async () => {
|
|
24
|
+
const pressedKeys = [];
|
|
25
|
+
const page = createMockPlaywrightPage();
|
|
26
|
+
page.keyboard.press = async (key) => {
|
|
27
|
+
pressedKeys.push(key);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
await pressKey({ page, engine: 'playwright', key: 'Escape' });
|
|
31
|
+
|
|
32
|
+
assert.deepStrictEqual(pressedKeys, ['Escape']);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should press a key using Puppeteer adapter', async () => {
|
|
36
|
+
const pressedKeys = [];
|
|
37
|
+
const page = createMockPuppeteerPage();
|
|
38
|
+
page.keyboard.press = async (key) => {
|
|
39
|
+
pressedKeys.push(key);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
await pressKey({ page, engine: 'puppeteer', key: 'Enter' });
|
|
43
|
+
|
|
44
|
+
assert.deepStrictEqual(pressedKeys, ['Enter']);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should accept a pre-created adapter', async () => {
|
|
48
|
+
const pressedKeys = [];
|
|
49
|
+
const adapter = {
|
|
50
|
+
keyboardPress: async (key) => {
|
|
51
|
+
pressedKeys.push(key);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
await pressKey({ key: 'Tab', adapter });
|
|
56
|
+
|
|
57
|
+
assert.deepStrictEqual(pressedKeys, ['Tab']);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should throw when key is not provided', async () => {
|
|
61
|
+
const page = createMockPlaywrightPage();
|
|
62
|
+
await assert.rejects(
|
|
63
|
+
() => pressKey({ page, engine: 'playwright' }),
|
|
64
|
+
/key is required/
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// typeText
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
describe('typeText', () => {
|
|
73
|
+
it('should type text using Playwright adapter', async () => {
|
|
74
|
+
const typedTexts = [];
|
|
75
|
+
const page = createMockPlaywrightPage();
|
|
76
|
+
page.keyboard.type = async (text) => {
|
|
77
|
+
typedTexts.push(text);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
await typeText({ page, engine: 'playwright', text: 'Hello World' });
|
|
81
|
+
|
|
82
|
+
assert.deepStrictEqual(typedTexts, ['Hello World']);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should type text using Puppeteer adapter', async () => {
|
|
86
|
+
const typedTexts = [];
|
|
87
|
+
const page = createMockPuppeteerPage();
|
|
88
|
+
page.keyboard.type = async (text) => {
|
|
89
|
+
typedTexts.push(text);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
await typeText({ page, engine: 'puppeteer', text: 'Hello World' });
|
|
93
|
+
|
|
94
|
+
assert.deepStrictEqual(typedTexts, ['Hello World']);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should accept a pre-created adapter', async () => {
|
|
98
|
+
const typedTexts = [];
|
|
99
|
+
const adapter = {
|
|
100
|
+
keyboardType: async (text) => {
|
|
101
|
+
typedTexts.push(text);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
await typeText({ text: 'test input', adapter });
|
|
106
|
+
|
|
107
|
+
assert.deepStrictEqual(typedTexts, ['test input']);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should throw when text is not provided', async () => {
|
|
111
|
+
const page = createMockPlaywrightPage();
|
|
112
|
+
await assert.rejects(
|
|
113
|
+
() => typeText({ page, engine: 'playwright' }),
|
|
114
|
+
/text is required/
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// keyDown
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
describe('keyDown', () => {
|
|
123
|
+
it('should hold a key down using Playwright adapter', async () => {
|
|
124
|
+
const downKeys = [];
|
|
125
|
+
const page = createMockPlaywrightPage();
|
|
126
|
+
page.keyboard.down = async (key) => {
|
|
127
|
+
downKeys.push(key);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
await keyDown({ page, engine: 'playwright', key: 'Control' });
|
|
131
|
+
|
|
132
|
+
assert.deepStrictEqual(downKeys, ['Control']);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should hold a key down using Puppeteer adapter', async () => {
|
|
136
|
+
const downKeys = [];
|
|
137
|
+
const page = createMockPuppeteerPage();
|
|
138
|
+
page.keyboard.down = async (key) => {
|
|
139
|
+
downKeys.push(key);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
await keyDown({ page, engine: 'puppeteer', key: 'Shift' });
|
|
143
|
+
|
|
144
|
+
assert.deepStrictEqual(downKeys, ['Shift']);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should accept a pre-created adapter', async () => {
|
|
148
|
+
const downKeys = [];
|
|
149
|
+
const adapter = {
|
|
150
|
+
keyboardDown: async (key) => {
|
|
151
|
+
downKeys.push(key);
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
await keyDown({ key: 'Alt', adapter });
|
|
156
|
+
|
|
157
|
+
assert.deepStrictEqual(downKeys, ['Alt']);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should throw when key is not provided', async () => {
|
|
161
|
+
const page = createMockPlaywrightPage();
|
|
162
|
+
await assert.rejects(
|
|
163
|
+
() => keyDown({ page, engine: 'playwright' }),
|
|
164
|
+
/key is required/
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// keyUp
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
describe('keyUp', () => {
|
|
173
|
+
it('should release a key using Playwright adapter', async () => {
|
|
174
|
+
const upKeys = [];
|
|
175
|
+
const page = createMockPlaywrightPage();
|
|
176
|
+
page.keyboard.up = async (key) => {
|
|
177
|
+
upKeys.push(key);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
await keyUp({ page, engine: 'playwright', key: 'Control' });
|
|
181
|
+
|
|
182
|
+
assert.deepStrictEqual(upKeys, ['Control']);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should release a key using Puppeteer adapter', async () => {
|
|
186
|
+
const upKeys = [];
|
|
187
|
+
const page = createMockPuppeteerPage();
|
|
188
|
+
page.keyboard.up = async (key) => {
|
|
189
|
+
upKeys.push(key);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
await keyUp({ page, engine: 'puppeteer', key: 'Shift' });
|
|
193
|
+
|
|
194
|
+
assert.deepStrictEqual(upKeys, ['Shift']);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should accept a pre-created adapter', async () => {
|
|
198
|
+
const upKeys = [];
|
|
199
|
+
const adapter = {
|
|
200
|
+
keyboardUp: async (key) => {
|
|
201
|
+
upKeys.push(key);
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
await keyUp({ key: 'Meta', adapter });
|
|
206
|
+
|
|
207
|
+
assert.deepStrictEqual(upKeys, ['Meta']);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should throw when key is not provided', async () => {
|
|
211
|
+
const page = createMockPlaywrightPage();
|
|
212
|
+
await assert.rejects(
|
|
213
|
+
() => keyUp({ page, engine: 'playwright' }),
|
|
214
|
+
/key is required/
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// ---------------------------------------------------------------------------
|
|
220
|
+
// PlaywrightAdapter keyboard methods
|
|
221
|
+
// ---------------------------------------------------------------------------
|
|
222
|
+
describe('PlaywrightAdapter keyboard methods', () => {
|
|
223
|
+
it('should delegate keyboardPress to page.keyboard.press', async () => {
|
|
224
|
+
const pressedKeys = [];
|
|
225
|
+
const page = createMockPlaywrightPage();
|
|
226
|
+
page.keyboard.press = async (key) => pressedKeys.push(key);
|
|
227
|
+
|
|
228
|
+
const adapter = new PlaywrightAdapter(page);
|
|
229
|
+
await adapter.keyboardPress('Escape');
|
|
230
|
+
|
|
231
|
+
assert.deepStrictEqual(pressedKeys, ['Escape']);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('should delegate keyboardType to page.keyboard.type', async () => {
|
|
235
|
+
const typedTexts = [];
|
|
236
|
+
const page = createMockPlaywrightPage();
|
|
237
|
+
page.keyboard.type = async (text) => typedTexts.push(text);
|
|
238
|
+
|
|
239
|
+
const adapter = new PlaywrightAdapter(page);
|
|
240
|
+
await adapter.keyboardType('hello');
|
|
241
|
+
|
|
242
|
+
assert.deepStrictEqual(typedTexts, ['hello']);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should delegate keyboardDown to page.keyboard.down', async () => {
|
|
246
|
+
const downKeys = [];
|
|
247
|
+
const page = createMockPlaywrightPage();
|
|
248
|
+
page.keyboard.down = async (key) => downKeys.push(key);
|
|
249
|
+
|
|
250
|
+
const adapter = new PlaywrightAdapter(page);
|
|
251
|
+
await adapter.keyboardDown('Control');
|
|
252
|
+
|
|
253
|
+
assert.deepStrictEqual(downKeys, ['Control']);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should delegate keyboardUp to page.keyboard.up', async () => {
|
|
257
|
+
const upKeys = [];
|
|
258
|
+
const page = createMockPlaywrightPage();
|
|
259
|
+
page.keyboard.up = async (key) => upKeys.push(key);
|
|
260
|
+
|
|
261
|
+
const adapter = new PlaywrightAdapter(page);
|
|
262
|
+
await adapter.keyboardUp('Control');
|
|
263
|
+
|
|
264
|
+
assert.deepStrictEqual(upKeys, ['Control']);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
// PuppeteerAdapter keyboard methods
|
|
270
|
+
// ---------------------------------------------------------------------------
|
|
271
|
+
describe('PuppeteerAdapter keyboard methods', () => {
|
|
272
|
+
it('should delegate keyboardPress to page.keyboard.press', async () => {
|
|
273
|
+
const pressedKeys = [];
|
|
274
|
+
const page = createMockPuppeteerPage();
|
|
275
|
+
page.keyboard.press = async (key) => pressedKeys.push(key);
|
|
276
|
+
|
|
277
|
+
const adapter = new PuppeteerAdapter(page);
|
|
278
|
+
await adapter.keyboardPress('Enter');
|
|
279
|
+
|
|
280
|
+
assert.deepStrictEqual(pressedKeys, ['Enter']);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('should delegate keyboardType to page.keyboard.type', async () => {
|
|
284
|
+
const typedTexts = [];
|
|
285
|
+
const page = createMockPuppeteerPage();
|
|
286
|
+
page.keyboard.type = async (text) => typedTexts.push(text);
|
|
287
|
+
|
|
288
|
+
const adapter = new PuppeteerAdapter(page);
|
|
289
|
+
await adapter.keyboardType('world');
|
|
290
|
+
|
|
291
|
+
assert.deepStrictEqual(typedTexts, ['world']);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should delegate keyboardDown to page.keyboard.down', async () => {
|
|
295
|
+
const downKeys = [];
|
|
296
|
+
const page = createMockPuppeteerPage();
|
|
297
|
+
page.keyboard.down = async (key) => downKeys.push(key);
|
|
298
|
+
|
|
299
|
+
const adapter = new PuppeteerAdapter(page);
|
|
300
|
+
await adapter.keyboardDown('Shift');
|
|
301
|
+
|
|
302
|
+
assert.deepStrictEqual(downKeys, ['Shift']);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should delegate keyboardUp to page.keyboard.up', async () => {
|
|
306
|
+
const upKeys = [];
|
|
307
|
+
const page = createMockPuppeteerPage();
|
|
308
|
+
page.keyboard.up = async (key) => upKeys.push(key);
|
|
309
|
+
|
|
310
|
+
const adapter = new PuppeteerAdapter(page);
|
|
311
|
+
await adapter.keyboardUp('Shift');
|
|
312
|
+
|
|
313
|
+
assert.deepStrictEqual(upKeys, ['Shift']);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
});
|