presidium 2.1.1 → 3.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.
Files changed (101) hide show
  1. package/Docker.js +3 -4
  2. package/GoogleChromeDevTools.js +902 -0
  3. package/index.js +1 -1
  4. package/internal/Archive.js +113 -0
  5. package/internal/Archive.test.js +71 -0
  6. package/internal/GoogleChromeForTesting.js +218 -0
  7. package/internal/GoogleChromeForTesting.test.js +50 -0
  8. package/internal/parsePath.js +2 -2
  9. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69977DD3-9B6F.pma +0 -0
  10. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69977DD4-9B86.pma +0 -0
  11. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69978250-A191.pma +0 -0
  12. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69978251-A1AD.pma +0 -0
  13. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69978277-A1FA.pma +0 -0
  14. package/internal/tmp/chrome/BrowserMetrics/BrowserMetrics-69978278-A209.pma +0 -0
  15. package/internal/tmp/chrome/ChromeFeatureState +1 -0
  16. package/internal/tmp/chrome/Default/Affiliation Database +0 -0
  17. package/internal/tmp/chrome/Default/Affiliation Database-journal +0 -0
  18. package/internal/tmp/chrome/Default/ClientCertificates/LOCK +0 -0
  19. package/internal/tmp/chrome/Default/ClientCertificates/LOG +0 -0
  20. package/internal/tmp/chrome/Default/ClientCertificates/LOG.old +0 -0
  21. package/internal/tmp/chrome/Default/Extension Rules/000003.log +0 -0
  22. package/internal/tmp/chrome/Default/Extension Rules/CURRENT +1 -0
  23. package/internal/tmp/chrome/Default/Extension Rules/LOCK +0 -0
  24. package/internal/tmp/chrome/Default/Extension Rules/LOG +3 -0
  25. package/internal/tmp/chrome/Default/Extension Rules/LOG.old +3 -0
  26. package/internal/tmp/chrome/Default/Extension Rules/MANIFEST-000001 +0 -0
  27. package/internal/tmp/chrome/Default/Extension Scripts/000003.log +0 -0
  28. package/internal/tmp/chrome/Default/Extension Scripts/CURRENT +1 -0
  29. package/internal/tmp/chrome/Default/Extension Scripts/LOCK +0 -0
  30. package/internal/tmp/chrome/Default/Extension Scripts/LOG +3 -0
  31. package/internal/tmp/chrome/Default/Extension Scripts/LOG.old +3 -0
  32. package/internal/tmp/chrome/Default/Extension Scripts/MANIFEST-000001 +0 -0
  33. package/internal/tmp/chrome/Default/Favicons +0 -0
  34. package/internal/tmp/chrome/Default/Favicons-journal +0 -0
  35. package/internal/tmp/chrome/Default/History +0 -0
  36. package/internal/tmp/chrome/Default/History-journal +0 -0
  37. package/internal/tmp/chrome/Default/LOCK +0 -0
  38. package/internal/tmp/chrome/Default/LOG +0 -0
  39. package/internal/tmp/chrome/Default/LOG.old +0 -0
  40. package/internal/tmp/chrome/Default/Local Storage/leveldb/000003.log +0 -0
  41. package/internal/tmp/chrome/Default/Local Storage/leveldb/CURRENT +1 -0
  42. package/internal/tmp/chrome/Default/Local Storage/leveldb/LOCK +0 -0
  43. package/internal/tmp/chrome/Default/Local Storage/leveldb/LOG +2 -0
  44. package/internal/tmp/chrome/Default/Local Storage/leveldb/MANIFEST-000001 +0 -0
  45. package/internal/tmp/chrome/Default/PersistentOriginTrials/LOCK +0 -0
  46. package/internal/tmp/chrome/Default/PersistentOriginTrials/LOG +0 -0
  47. package/internal/tmp/chrome/Default/PersistentOriginTrials/LOG.old +0 -0
  48. package/internal/tmp/chrome/Default/README +1 -0
  49. package/internal/tmp/chrome/Default/Segmentation Platform/SegmentInfoDB/LOCK +0 -0
  50. package/internal/tmp/chrome/Default/Segmentation Platform/SegmentInfoDB/LOG +0 -0
  51. package/internal/tmp/chrome/Default/Segmentation Platform/SegmentInfoDB/LOG.old +0 -0
  52. package/internal/tmp/chrome/Default/Segmentation Platform/SignalDB/LOCK +0 -0
  53. package/internal/tmp/chrome/Default/Segmentation Platform/SignalDB/LOG +0 -0
  54. package/internal/tmp/chrome/Default/Segmentation Platform/SignalDB/LOG.old +0 -0
  55. package/internal/tmp/chrome/Default/Segmentation Platform/SignalStorageConfigDB/LOCK +0 -0
  56. package/internal/tmp/chrome/Default/Segmentation Platform/SignalStorageConfigDB/LOG +0 -0
  57. package/internal/tmp/chrome/Default/Segmentation Platform/SignalStorageConfigDB/LOG.old +0 -0
  58. package/internal/tmp/chrome/Default/ServerCertificate +0 -0
  59. package/internal/tmp/chrome/Default/ServerCertificate-journal +0 -0
  60. package/internal/tmp/chrome/Default/Site Characteristics Database/000003.log +0 -0
  61. package/internal/tmp/chrome/Default/Site Characteristics Database/CURRENT +1 -0
  62. package/internal/tmp/chrome/Default/Site Characteristics Database/LOCK +0 -0
  63. package/internal/tmp/chrome/Default/Site Characteristics Database/LOG +3 -0
  64. package/internal/tmp/chrome/Default/Site Characteristics Database/LOG.old +3 -0
  65. package/internal/tmp/chrome/Default/Site Characteristics Database/MANIFEST-000001 +0 -0
  66. package/internal/tmp/chrome/Default/Sync Data/LevelDB/000003.log +0 -0
  67. package/internal/tmp/chrome/Default/Sync Data/LevelDB/CURRENT +1 -0
  68. package/internal/tmp/chrome/Default/Sync Data/LevelDB/LOCK +0 -0
  69. package/internal/tmp/chrome/Default/Sync Data/LevelDB/LOG +3 -0
  70. package/internal/tmp/chrome/Default/Sync Data/LevelDB/LOG.old +3 -0
  71. package/internal/tmp/chrome/Default/Sync Data/LevelDB/MANIFEST-000001 +0 -0
  72. package/internal/tmp/chrome/Default/chrome_cart_db/LOCK +0 -0
  73. package/internal/tmp/chrome/Default/chrome_cart_db/LOG +0 -0
  74. package/internal/tmp/chrome/Default/chrome_cart_db/LOG.old +0 -0
  75. package/internal/tmp/chrome/Default/commerce_subscription_db/LOCK +0 -0
  76. package/internal/tmp/chrome/Default/commerce_subscription_db/LOG +0 -0
  77. package/internal/tmp/chrome/Default/commerce_subscription_db/LOG.old +0 -0
  78. package/internal/tmp/chrome/Default/discount_infos_db/LOCK +0 -0
  79. package/internal/tmp/chrome/Default/discount_infos_db/LOG +0 -0
  80. package/internal/tmp/chrome/Default/discount_infos_db/LOG.old +0 -0
  81. package/internal/tmp/chrome/Default/discounts_db/LOCK +0 -0
  82. package/internal/tmp/chrome/Default/discounts_db/LOG +0 -0
  83. package/internal/tmp/chrome/Default/discounts_db/LOG.old +0 -0
  84. package/internal/tmp/chrome/Default/parcel_tracking_db/LOCK +0 -0
  85. package/internal/tmp/chrome/Default/parcel_tracking_db/LOG +0 -0
  86. package/internal/tmp/chrome/Default/parcel_tracking_db/LOG.old +0 -0
  87. package/internal/tmp/chrome/Default/power_bookmarks/PowerBookmarks.db +0 -0
  88. package/internal/tmp/chrome/Default/power_bookmarks/PowerBookmarks.db-journal +0 -0
  89. package/internal/tmp/chrome/First Run +0 -0
  90. package/internal/tmp/chrome/Last Version +1 -0
  91. package/internal/tmp/chrome/Local State +1 -0
  92. package/internal/tmp/chrome/ShaderCache/data_0 +0 -0
  93. package/internal/tmp/chrome/ShaderCache/data_1 +0 -0
  94. package/internal/tmp/chrome/ShaderCache/data_2 +0 -0
  95. package/internal/tmp/chrome/ShaderCache/data_3 +0 -0
  96. package/internal/tmp/chrome/ShaderCache/index +0 -0
  97. package/internal/tmp/chrome/Variations +1 -0
  98. package/internal/tmp/chrome/segmentation_platform/ukm_db +0 -0
  99. package/internal/tmp/chrome/segmentation_platform/ukm_db-wal +0 -0
  100. package/package.json +3 -2
  101. package/Archive.js +0 -120
@@ -0,0 +1,902 @@
1
+ const EventEmitter = require('events')
2
+ const GoogleChromeForTesting = require('./internal/GoogleChromeForTesting')
3
+ const WebSocket = require('./WebSocket')
4
+
5
+ let id = 0
6
+ function getId() {
7
+ return id += 1
8
+ }
9
+
10
+ /**
11
+ * @name sendRequestJSON
12
+ *
13
+ * @docs
14
+ * ```coffeescript [specscript]
15
+ * sendRequestJSON(payload string) -> data Promise<Object>
16
+ * ```
17
+ */
18
+ async function sendRequestJSON(payload) {
19
+ let resolve
20
+ const promise = new Promise(_resolve => {
21
+ resolve = _resolve
22
+ })
23
+
24
+ const handler = function (message) {
25
+ const data = JSON.parse(message.toString('utf8'))
26
+ if (data.id == id) {
27
+ resolve(data)
28
+ }
29
+ }
30
+
31
+ this.websocket.on('message', handler)
32
+ this.websocket.send(payload)
33
+
34
+ const data = await promise
35
+ this.websocket.removeListener('message', handler)
36
+
37
+ return data.result
38
+ }
39
+
40
+ /**
41
+ * @name _Method
42
+ *
43
+ * @docs
44
+ * ```coffeescript [specscript]
45
+ * _Method(method string, {
46
+ * sessionId: string,
47
+ * ...params Object
48
+ * }) -> data Promise<Object>
49
+ * ```
50
+ */
51
+ async function _Method(method, { sessionId, ...params }) {
52
+ const id = getId()
53
+
54
+ const payload = JSON.stringify({
55
+ sessionId: sessionId ?? this.sessionId,
56
+ id,
57
+ method,
58
+ params,
59
+ })
60
+
61
+ const data = await sendRequestJSON.call(this, payload)
62
+
63
+ if (data.error) {
64
+ const error = new Error(data.error.message)
65
+ error.code = data.error.code
66
+ throw error
67
+ }
68
+
69
+ return data
70
+ }
71
+
72
+ class GoogleChromeDevToolsTarget {
73
+ constructor(websocket) {
74
+ this.websocket = websocket
75
+ }
76
+
77
+ /**
78
+ * @name Target.getTargets
79
+ *
80
+ * @docs
81
+ * ```coffeescript [specscript]
82
+ * module CDPTarget 'https://chromedevtools.github.io/devtools-protocol/tot/Target/'
83
+ *
84
+ * Target.getTargets() -> data Promise<{
85
+ * targetInfos: Array<CDPTarget.TargetInfo>,
86
+ * }>
87
+ * ```
88
+ *
89
+ * Retrieves a list of available targets.
90
+ *
91
+ * Arguments:
92
+ * * (none)
93
+ *
94
+ * Return:
95
+ * * `data`
96
+ * * `targetInfos` - [`Array<CDPTarget.TargetInfo>`](https://chromedevtools.github.io/devtools-protocol/tot/Target/#type-TargetInfo) - an array of information about the available targets.
97
+ *
98
+ * ```javascript
99
+ * const data = await googleChromeDevTools.Target.getTargets()
100
+ * ```
101
+ */
102
+ getTargets(options = {}) {
103
+ return _Method.call(this, 'Target.getTargets', options)
104
+ }
105
+
106
+ /**
107
+ * @name Target.attachToTarget
108
+ *
109
+ * @docs
110
+ * ```coffeescript [specscript]
111
+ * module CDPTarget 'https://chromedevtools.github.io/devtools-protocol/tot/Target/'
112
+ *
113
+ * Target.attachToTarget(options {
114
+ * targetId: CDPTarget.TargetID,
115
+ * }) -> data Promise<{
116
+ * sessionId: string,
117
+ * }>
118
+ * ```
119
+ *
120
+ * Attaches to a target and creates a new session.
121
+ *
122
+ * Arguments:
123
+ * * `options`
124
+ * * `target` - the target ID.
125
+ *
126
+ * Return:
127
+ * * `data`
128
+ * * `sessionId` - the ID of the new session.
129
+ *
130
+ * ```javascript
131
+ * const data = await googleChromeDevTools.Target.attachToTarget()
132
+ * ```
133
+ */
134
+ attachToTarget(options = {}) {
135
+ return _Method.call(this, 'Target.attachToTarget', options)
136
+ }
137
+ }
138
+
139
+ class GoogleChromeDevToolsPage {
140
+ constructor(websocket) {
141
+ this.websocket = websocket
142
+ }
143
+
144
+ /**
145
+ * @name Page.navigate
146
+ *
147
+ * @docs
148
+ * ```coffeescript [specscript]
149
+ * module CDPPage 'https://chromedevtools.github.io/devtools-protocol/tot/Page/'
150
+ *
151
+ * Page.navigate(options {
152
+ * sessionId: string,
153
+ * url: string,
154
+ * }) -> data Promise<{
155
+ * frameId: CDPPage.FrameID,
156
+ * }>
157
+ * ```
158
+ *
159
+ * Navigates the current page to a url.
160
+ *
161
+ * Arguments:
162
+ * * `options`
163
+ * * `sessionId` - the session ID.
164
+ * * `url` - the url to navigate to.
165
+ *
166
+ * Return:
167
+ * * `data`
168
+ * * `frameId` - [`Page.FrameId`](https://chromedevtools.github.io/devtools-protocol/tot/Page/#type-FrameId) - the ID of the document context (top-level page or `<iframe>`).
169
+ *
170
+ * ```javascript
171
+ * await googleChromeDevTools.Page.navigate({
172
+ * url: 'http://localhost:3000/',
173
+ * })
174
+ * ```
175
+ */
176
+ navigate(options = {}) {
177
+ return _Method.call(this, 'Page.navigate', options)
178
+ }
179
+ }
180
+
181
+ /**
182
+ * @name Event: DOM.attributeModified
183
+ */
184
+
185
+ /**
186
+ * @name Event: DOM.attributeRemoved
187
+ */
188
+
189
+ /**
190
+ * @name Event: DOM.characterDataModified
191
+ */
192
+
193
+ /**
194
+ * @name Event: DOM.childNodeCountUpdated
195
+ */
196
+
197
+ /**
198
+ * @name Event: DOM.childNodeInserted
199
+ */
200
+
201
+ /**
202
+ * @name Event: DOM.childNodeRemoved
203
+ */
204
+
205
+ /**
206
+ * @name Event: DOM.documentUpdated
207
+ */
208
+
209
+ /**
210
+ * @name Event: DOM.setChildNodes
211
+ */
212
+
213
+ class GoogleChromeDevToolsDOM {
214
+ constructor(websocket) {
215
+ this.websocket = websocket
216
+ }
217
+
218
+ /**
219
+ * @name DOM.enable
220
+ *
221
+ * @docs
222
+ * ```coffeescript [specscript]
223
+ * DOM.enable(options {
224
+ * sessionId: string,
225
+ * }) -> data Promise<{}>
226
+ * ```
227
+ *
228
+ * Enables DOM events for the current target.
229
+ *
230
+ * Arguments:
231
+ * * `options`
232
+ * * `sessionId` - the session ID.
233
+ *
234
+ * Return:
235
+ * * `data` - promise of an empty object.
236
+ *
237
+ * ```javascript
238
+ * await googleChromeDevTools.DOM.enable()
239
+ * ```
240
+ */
241
+ enable(options = {}) {
242
+ return _Method.call(this, 'DOM.enable', options)
243
+ }
244
+
245
+ /**
246
+ * @name DOM.getDocument
247
+ *
248
+ * @docs
249
+ * ```coffeescript [specscript]
250
+ * module CDPDOM 'https://chromedevtools.github.io/devtools-protocol/tot/DOM/'
251
+ *
252
+ * DOM.getDocument(options {
253
+ * sessionId: string,
254
+ * depth: number,
255
+ * }) -> data Promise<{
256
+ * root: CDPDOM.Node,
257
+ * }>
258
+ * ```
259
+ *
260
+ * Returns the root DOM node and subtree. Enables DOM domain events for the current target.
261
+ *
262
+ * Arguments:
263
+ * * `options`
264
+ * * `sessionId` - the session ID.
265
+ * * `depth` - the maximum depth of the subtree. Defaults to `1`. Use `-1` for the entire subtree.
266
+ *
267
+ * Return:
268
+ * * `data`
269
+ * * `root` - the root DOM node and subtree.
270
+ *
271
+ * ```javascript
272
+ * const data = await googleChromeDevTools.DOM.getDocument({ depth: 10 })
273
+ * ```
274
+ */
275
+ getDocument(options = {}) {
276
+ return _Method.call(this, 'DOM.getDocument', options)
277
+ }
278
+
279
+ /**
280
+ * @name DOM.querySelector
281
+ *
282
+ * @docs
283
+ * ```coffeescript [specscript]
284
+ * DOM.querySelector(options {
285
+ * sessionId: string,
286
+ * nodeId: string,
287
+ * selector: string,
288
+ * }) -> data Promise<{
289
+ * nodeId: string,
290
+ * }>
291
+ * ```
292
+ *
293
+ * Executes a query selector on a node.
294
+ *
295
+ * Arguments:
296
+ * * `options`
297
+ * * `sessionId` - the session ID.
298
+ * * `nodeId` - the ID of the node to query.
299
+ * * `selector` - the query selector to execute.
300
+ *
301
+ * Return:
302
+ * * `data`
303
+ * * `nodeId` - the first node ID that matches the query selector.
304
+ *
305
+ * ```javascript
306
+ * const data = await googleChromeDevTools.DOM.querySelector({
307
+ * nodeId: 1,
308
+ * selector: 'form > button[type="submit"]',
309
+ * })
310
+ * ```
311
+ *
312
+ * References:
313
+ * * [Selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Selectors#selectors)
314
+ */
315
+ querySelector(options = {}) {
316
+ return _Method.call(this, 'DOM.querySelector', options)
317
+ }
318
+
319
+ /**
320
+ * @name DOM.querySelectorAll
321
+ *
322
+ * @docs
323
+ * ```coffeescript [specscript]
324
+ * DOM.querySelectorAll(options {
325
+ * sessionId: string,
326
+ * nodeId: string,
327
+ * selector: string,
328
+ * }) -> data Promise<{
329
+ * nodeIds: Array<string>,
330
+ * }>
331
+ * ```
332
+ *
333
+ * Executes a query selector on a node, returning all node IDs that match the selector.
334
+ *
335
+ * Arguments:
336
+ * * `options`
337
+ * * `sessionId` - the session ID.
338
+ * * `nodeId` - the ID of the node to query.
339
+ * * `selector` - the query selector to execute.
340
+ *
341
+ * Return:
342
+ * * `data`
343
+ * * `nodeIds` - an array of the node IDs that match the query selector.
344
+ *
345
+ * ```javascript
346
+ * const data = await googleChromeDevTools.DOM.querySelectorAll({
347
+ * nodeId: 1,
348
+ * selector: 'nav > a',
349
+ * })
350
+ * ```
351
+ *
352
+ * References:
353
+ * * [Selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Selectors#selectors)
354
+ */
355
+ querySelectorAll(options = {}) {
356
+ return _Method.call(this, 'DOM.querySelectorAll', options)
357
+ }
358
+
359
+ /**
360
+ * @name DOM.getBoxModel
361
+ *
362
+ * @docs
363
+ * ```coffeescript [specscript]
364
+ * module CDPDOM 'https://chromedevtools.github.io/devtools-protocol/tot/DOM/'
365
+ *
366
+ * DOM.getBoxModel(options {
367
+ * sessionId: string,
368
+ * nodeId: string,
369
+ * }) -> data Promise<{
370
+ * model: CDPDOM.BoxModel,
371
+ * }>
372
+ * ```
373
+ *
374
+ * Returns the box model of a node.
375
+ *
376
+ * Arguments:
377
+ * * `options`
378
+ * * `sessionId` - the session ID.
379
+ * * `nodeId` - the ID of the node to query.
380
+ *
381
+ * Return:
382
+ * * `data`
383
+ * * `model` - [`CDPDOM.BoxModel`](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#type-BoxModel) - the box model for the node.
384
+ *
385
+ * ```javascript
386
+ * const data = await googleChromeDevTools.DOM.getBoxModel({ nodeId: 32 })
387
+ * ```
388
+ *
389
+ * References:
390
+ * * [Box Model](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Box_model)
391
+ */
392
+ getBoxModel(options = {}) {
393
+ return _Method.call(this, 'DOM.getBoxModel', options)
394
+ }
395
+
396
+ /**
397
+ * @name DOM.describeNode
398
+ *
399
+ * @docs
400
+ * ```coffeescript [specscript]
401
+ * module CDPDOM 'https://chromedevtools.github.io/devtools-protocol/tot/DOM/'
402
+ *
403
+ * DOM.describeNode(options {
404
+ * sessionId: string,
405
+ * nodeId: string,
406
+ * }) -> data Promise<{
407
+ * node: CDPDOM.Node,
408
+ * }>
409
+ * ```
410
+ *
411
+ * Describes a node.
412
+ *
413
+ * Arguments:
414
+ * * `options`
415
+ * * `sessionId` - the session ID.
416
+ * * `nodeId` - the ID of the node to describe.
417
+ *
418
+ * Return:
419
+ * * `data`
420
+ * * `node` - [`CDPDOM.Node`](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#type-Node) - the node description.
421
+ *
422
+ * ```javascript
423
+ * const data = await googleChromeDevTools.DOM.describeNode({ nodeId: 1 })
424
+ * ```
425
+ */
426
+ describeNode(options = {}) {
427
+ return _Method.call(this, 'DOM.describeNode', options)
428
+ }
429
+
430
+ /**
431
+ * @name DOM.setAttributeValue
432
+ *
433
+ * @docs
434
+ * ```coffeescript [specscript]
435
+ * module CDPDOM 'https://chromedevtools.github.io/devtools-protocol/tot/DOM/'
436
+ *
437
+ * DOM.setAttributeValue(options {
438
+ * sessionId: string,
439
+ * nodeId: string,
440
+ * name: string,
441
+ * value: string,
442
+ * }) -> data Promise<{}>
443
+ * ```
444
+ *
445
+ * Sets the value of an attribute for a node.
446
+ *
447
+ * Arguments:
448
+ * * `options`
449
+ * * `sessionId` - the session ID.
450
+ * * `nodeId` - the ID of the node.
451
+ * * `name` - the name of the attribute to set.
452
+ * * `value` - the value to set for the attribute.
453
+ *
454
+ * Return:
455
+ * * `data` - promise of an empty object.
456
+ *
457
+ * ```javascript
458
+ * await googleChromeDevTools.DOM.setAttributeValue({
459
+ * nodeId: 19,
460
+ * name: 'value',
461
+ * value: 'Example',
462
+ * })
463
+ * ```
464
+ */
465
+ setAttributeValue(options = {}) {
466
+ return _Method.call(this, 'DOM.setAttributeValue', options)
467
+ }
468
+
469
+ /**
470
+ * @name DOM.focus
471
+ *
472
+ * @docs
473
+ * ```coffeescript [specscript]
474
+ * module CDPDOM 'https://chromedevtools.github.io/devtools-protocol/tot/DOM/'
475
+ *
476
+ * DOM.focus(options {
477
+ * sessionId: string,
478
+ * nodeId: string,
479
+ * }) -> data Promise<{}>
480
+ * ```
481
+ *
482
+ * Focuses the element or node.
483
+ *
484
+ * Arguments:
485
+ * * `options`
486
+ * * `sessionId` - the session ID.
487
+ * * `nodeId` - the ID of the node.
488
+ *
489
+ * Return:
490
+ * * `data` - promise of an empty object.
491
+ *
492
+ * ```javascript
493
+ * await googleChromeDevTools.DOM.focus({ nodeId: 21 })
494
+ * ```
495
+ */
496
+ focus(options = {}) {
497
+ return _Method.call(this, 'DOM.focus', options)
498
+ }
499
+
500
+ }
501
+
502
+ class GoogleChromeDevToolsInput {
503
+ constructor(websocket) {
504
+ this.websocket = websocket
505
+ }
506
+
507
+ /**
508
+ * @name Input.dispatchMouseEvent
509
+ *
510
+ * @docs
511
+ * ```coffeescript [specscript]
512
+ * Input.dispatchMouseEvent(options {
513
+ * sessionId: string,
514
+ * type: 'mousePressed'|'mouseReleased'|'mouseMoved'|'mouseWheel',
515
+ * button: 'none'|'left'|'middle'|'right'|'back'|'forward',
516
+ * clickCount: number,
517
+ * x: number,
518
+ * y: number,
519
+ * }) -> data Promise<{}>
520
+ * ```
521
+ *
522
+ * Dispatches a mouse event to the page.
523
+ *
524
+ * Arguments:
525
+ * * `options`
526
+ * * `sessionId` - the session ID.
527
+ * * `type` - the type of mouse event.
528
+ * * `button` - the mouse button.
529
+ * * `clickCount` - the number of times to click the mouse button.
530
+ * * `x` - the x-coordinate of the event relative to the main frame's viewport.
531
+ * * `y` - the y-coordinate of the event relative to the main frame's viewport.
532
+ *
533
+ * Return:
534
+ * * `data` - promise of an empty object.
535
+ *
536
+ * ```javascript
537
+ * await googleChromeDevTools.Input.dispatchMouseEvent({
538
+ * type: 'mousePressed',
539
+ * button: 'left',
540
+ * clickCount: 1,
541
+ * x: 500,
542
+ * y: 500,
543
+ * })
544
+ * ```
545
+ */
546
+ dispatchMouseEvent(options = {}) {
547
+ return _Method.call(this, 'Input.dispatchMouseEvent', options)
548
+ }
549
+
550
+ /**
551
+ * @name Input.dispatchKeyEvent
552
+ *
553
+ * @docs
554
+ * ```coffeescript [specscript]
555
+ * Input.dispatchKeyEvent(options {
556
+ * sessionId: string,
557
+ * type: 'keyDown'|'keyUp'|'rawKeyDown'|'char',
558
+ * text: string,
559
+ * }) -> data Promise<{}>
560
+ * ```
561
+ *
562
+ * Dispatches a key event to the page.
563
+ *
564
+ * Arguments:
565
+ * * `options`
566
+ * * `sessionId` - the session ID.
567
+ * * `type` - the type of key event.
568
+ * * `text` - the text generated by processing a virtual key code with a keyboard layout.
569
+ *
570
+ * Return:
571
+ * * `data` - promise of an empty object.
572
+ *
573
+ * ```javascript
574
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyDown', text: 'T' })
575
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyUp' })
576
+ * ```
577
+ */
578
+ dispatchKeyEvent(options = {}) {
579
+ return _Method.call(this, 'Input.dispatchKeyEvent', options)
580
+ }
581
+
582
+ }
583
+
584
+ class GoogleChromeDevToolsStorage {
585
+ constructor(websocket) {
586
+ this.websocket = websocket
587
+ }
588
+
589
+ /**
590
+ * @name Storage.clearCookies
591
+ *
592
+ * @docs
593
+ * ```coffeescript [specscript]
594
+ * Storage.clearCookies(options {
595
+ * sessionId: string,
596
+ * }) -> data Promise<{}>
597
+ * ```
598
+ *
599
+ * Clears the cookies of the target.
600
+ *
601
+ * Arguments:
602
+ * * `options`
603
+ * * `sessionId` - the session ID.
604
+ *
605
+ * Return:
606
+ * * `data` - promise of an empty object.
607
+ *
608
+ * ```javascript
609
+ * await googleChromeDevTools.Storage.clearCookies()
610
+ * ```
611
+ */
612
+ clearCookies(options = {}) {
613
+ return _Method.call(this, 'Storage.clearCookies', options)
614
+ }
615
+
616
+ /**
617
+ * @name Storage.getCookies
618
+ *
619
+ * @docs
620
+ * ```coffeescript [specscript]
621
+ * module CDPNetwork 'https://chromedevtools.github.io/devtools-protocol/tot/Network/'
622
+ *
623
+ * Storage.getCookies(options {
624
+ * sessionId: string,
625
+ * }) -> data Promise<{
626
+ * cookies: Array<CDPNetwork.Cookie>,
627
+ * }>
628
+ * ```
629
+ *
630
+ * Gets the cookies of the target.
631
+ *
632
+ * Arguments:
633
+ * * `options`
634
+ * * `sessionId` - the session ID.
635
+ *
636
+ * Return:
637
+ * * `data`
638
+ * * `cookies` - [`CDPNetwork.Cookie`](https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-Cookie) - An array of cookie objects.
639
+ *
640
+ * ```javascript
641
+ * const data = await googleChromeDevTools.Storage.getCookies()
642
+ * ```
643
+ */
644
+ getCookies(options = {}) {
645
+ return _Method.call(this, 'Storage.getCookies', options)
646
+ }
647
+
648
+ /**
649
+ * @name Storage.setCookies
650
+ *
651
+ * @docs
652
+ * ```coffeescript [specscript]
653
+ * module CDPNetwork 'https://chromedevtools.github.io/devtools-protocol/tot/Network/'
654
+ *
655
+ * Storage.setCookies(options {
656
+ * sessionId: string,
657
+ * cookies: Array<CDPNetwork.CookieParam>,
658
+ * }) -> data Promise<{}>
659
+ * ```
660
+ *
661
+ * Sets the cookies of the target.
662
+ *
663
+ * Arguments:
664
+ * * `options`
665
+ * * `sessionId` - the session ID.
666
+ * * `cookies` - [`Array<CDPNetwork.CookieParam>`](https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-CookieParam) - the cookies to be set.
667
+ *
668
+ * Return:
669
+ * * `data` - promise of an empty object.
670
+ *
671
+ * ```javascript
672
+ * await googleChromeDevTools.Storage.setCookies({
673
+ * cookies: [
674
+ * {
675
+ * name: 'cookie1',
676
+ * value: 'example1',
677
+ * domain: 'localhost:3000',
678
+ * path: '/',
679
+ * },
680
+ * {
681
+ * name: 'cookie2',
682
+ * value: 'example2',
683
+ * domain: 'localhost:3000',
684
+ * path: '/',
685
+ * }
686
+ * ],
687
+ * })
688
+ * ```
689
+ */
690
+ setCookies(options = {}) {
691
+ return _Method.call(this, 'Storage.setCookies', options)
692
+ }
693
+ }
694
+
695
+ class GoogleChromeDevToolsRuntime {
696
+ constructor(websocket) {
697
+ this.websocket = websocket
698
+ }
699
+
700
+ /**
701
+ * @name Runtime.evaluate
702
+ *
703
+ * @docs
704
+ * ```coffeescript [specscript]
705
+ * module CDPRuntime 'https://chromedevtools.github.io/devtools-protocol/tot/Runtime/'
706
+ *
707
+ * Runtime.evaluate(options {
708
+ * sessionId: string,
709
+ * expression: string,
710
+ * }) -> data Promise<{
711
+ * result: CDPRuntime.RemoteObject,
712
+ * exceptionDetails: CDPRuntime.ExceptionDetails,
713
+ * }>
714
+ * ```
715
+ *
716
+ * Evaluates an expression on the global object.
717
+ *
718
+ * Arguments:
719
+ * * `options`
720
+ * * `sessionId` - the session ID.
721
+ * * `expression` - the expression to evaluate.
722
+ *
723
+ * Return:
724
+ * * `data`
725
+ * * `result` - [`CDPRuntime.RemoteObject`](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject) - the evaluation result.
726
+ * * `exceptionDetails` - [`CDPRuntime.ExceptionDetails`](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExceptionDetails) - detailed information about any exceptions or errors that were thrown during evaluation.
727
+ *
728
+ * ```javascript
729
+ * const expression = 'document.querySelector(\'button[type="submit"]\').click();'
730
+ * await googleChromeDevTools.Runtime.evaluate({ expression })
731
+ * ```
732
+ */
733
+ evaluate(options = {}) {
734
+ return _Method.call(this, 'Runtime.evaluate', options)
735
+ }
736
+
737
+ }
738
+
739
+ /**
740
+ * @name GoogleChromeDevTools
741
+ *
742
+ * @constructor
743
+ *
744
+ * @docs
745
+ * ```coffeescript [specscript]
746
+ * new GoogleChromeDevTools(url string) -> googleChromeDevTools GoogleChromeDevTools
747
+ * ```
748
+ *
749
+ * Presidium GoogleChromeDevTools client for test automation.
750
+ *
751
+ * ```javascript
752
+ * const googleChromeDevTools = new GoogleChromeDevTools()
753
+ * await googleChromeDevTools.init()
754
+ *
755
+ * const target = await googleChromeDevTools.Target.getTargets()
756
+ * .then(data => data.result.targetInfos[0])
757
+ *
758
+ * const sessionId = await googleChromeDevTools.Target.attachToTarget({
759
+ * targetId: target.targetId,
760
+ * flatten: true,
761
+ * }).then(data => data.result.sessionId)
762
+ *
763
+ * googleChromeDevTools.setSessionId(sessionId)
764
+ *
765
+ * await googleChromeDevTools.Page.navigate({
766
+ * url: 'http://localhost:7357/',
767
+ * })
768
+ *
769
+ * const documentNodeId = await googleChromeDevTools.DOM.getDocument()
770
+ * .then(data => data.result.root.nodeId)
771
+ *
772
+ * const inputNodeId = await googleChromeDevTools.DOM.querySelector({
773
+ * nodeId: documentNodeId,
774
+ * selector: 'input',
775
+ * }).then(data => data.result.nodeId)
776
+ *
777
+ * await googleChromeDevTools.DOM.focus({ nodeId: inputNodeId })
778
+ *
779
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyDown', text: 'T' })
780
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyUp', text: 'T' })
781
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyDown', text: 'e' })
782
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyUp', text: 'e' })
783
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyDown', text: 's' })
784
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyUp', text: 's`' })
785
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyDown', text: 't' })
786
+ * await googleChromeDevTools.Input.dispatchKeyEvent({ type: 'keyUp', text: 't' })
787
+ * ```
788
+ *
789
+ * The Chrome DevTools Protocol has various APIs to interact with the different parts of the browser. These parts are separated into different domains. The Presidium GoogleChromeDevTools client covers the `Target`, `Page`, `DOM`, `Input`, `Storage`, and `Runtime` domains. Pages, serviceworkers, and extensions are called "Targets" and can be fetched and tracked using the `Target` domain.
790
+ *
791
+ * Every Chrome DevTools Protocol client needs to first attach to the target using the `Target.attachToTarget` command. The command will establish a protocol session with the given target and return a `sessionId`. The returned `sessionId` should be included in every message to the DevTools server.
792
+ *
793
+ * ```javascript
794
+ * const googleChromeDevTools = new GoogleChromeDevTools()
795
+ * await googleChromeDevTools.init()
796
+ *
797
+ * // get targets
798
+ * const targetsData = await googleChromeDevTools.Target.getTargets()
799
+ * const pageTarget = targetsData.result.targetInfos.find(info => info.type == 'page')
800
+ *
801
+ * // attach to target
802
+ * const attachToTargetData = await googleChromeDevTools.Target.attachToTarget({
803
+ * targetId: this.pageTarget.targetId,
804
+ * flatten: true,
805
+ * })
806
+ * const sessionId = attachToTargetData.result.sessionId
807
+ *
808
+ * // navigate to the home page
809
+ * const data = await googleChromeDevTools.Page.navigate({
810
+ * sessionId: this.sessionId,
811
+ * url: `http://localhost:3000/`,
812
+ * })
813
+ * ```
814
+ *
815
+ * References:
816
+ * * [Getting Started with the Chrome Devtools Protocol](https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md)
817
+ * * [Chrome Devtools Protocol](https://chromedevtools.github.io/devtools-protocol/)
818
+ */
819
+ class GoogleChromeDevTools extends EventEmitter {
820
+ constructor(options = {}) {
821
+ super()
822
+
823
+ this.chromeVersion = options.chromeVersion ?? 'stable'
824
+ }
825
+
826
+ /**
827
+ * @name init
828
+ *
829
+ * @docs
830
+ * ```coffeescript [specscript]
831
+ * init() -> Promise<>
832
+ * ```
833
+ */
834
+ async init() {
835
+ const googleChromeForTesting = new GoogleChromeForTesting({
836
+ chromeVersion: this.chromeVersion,
837
+ userDataDir: `${__dirname}/tmp/chrome`,
838
+ useMockKeychain: true,
839
+ })
840
+ await googleChromeForTesting.init()
841
+ this.googleChromeForTesting = googleChromeForTesting
842
+
843
+ this.websocket = new WebSocket(googleChromeForTesting.devtoolsUrl, {
844
+ offerPerMessageDeflate: false,
845
+ })
846
+ this.websocket.on('error', error => {
847
+ throw error
848
+ })
849
+
850
+ this.websocket.on('message', message => {
851
+ const data = JSON.parse(message.toString('utf8'))
852
+ if (data.method) {
853
+ console.log('Event:', data.method, JSON.stringify(data.params))
854
+ this.emit(data.method, data.params)
855
+ }
856
+ })
857
+
858
+ this.Target = new GoogleChromeDevToolsTarget(this.websocket)
859
+ this.Page = new GoogleChromeDevToolsPage(this.websocket)
860
+ this.DOM = new GoogleChromeDevToolsDOM(this.websocket)
861
+ this.Input = new GoogleChromeDevToolsInput(this.websocket)
862
+ this.Storage = new GoogleChromeDevToolsStorage(this.websocket)
863
+ this.Runtime = new GoogleChromeDevToolsRuntime(this.websocket)
864
+
865
+ await new Promise(resolve => {
866
+ this.websocket.on('open', resolve)
867
+ })
868
+ }
869
+
870
+ /**
871
+ * @name setSessionId
872
+ *
873
+ * @docs
874
+ * ```coffeescript [specscript]
875
+ * setSessionId(sessionId string) -> undefined
876
+ * ```
877
+ *
878
+ * Sets the session ID for the GoogleChromeDevTools client.
879
+ *
880
+ * Arguments:
881
+ * * `sessionId` - the ID of the session.
882
+ *
883
+ * Return:
884
+ * * `undefined`
885
+ *
886
+ * ```javascript
887
+ * const data = await googleChromeDevTools.Target.attachToTarget()
888
+ * googleChromeDevTools.setSessionId(data.sessionId)
889
+ * ```
890
+ */
891
+ setSessionId(sessionId) {
892
+ this.sessionId = sessionId
893
+ this.Page.sessionId = sessionId
894
+ this.DOM.sessionId = sessionId
895
+ this.Input.sessionId = sessionId
896
+ this.Storage.sessionId = sessionId
897
+ this.Runtime.sessionId = sessionId
898
+ }
899
+
900
+ }
901
+
902
+ module.exports = GoogleChromeDevTools