1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# DO NOT EDIT THIS FILE!
#
# This file is generated from the CDP specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# CDP domain: Input
from __future__ import annotations
from .util import event_class, T_JSON_DICT
from dataclasses import dataclass
import enum
import typing
 
@dataclass
class TouchPoint:
    #: X coordinate of the event relative to the main frame's viewport in CSS pixels.
    x: float
 
    #: Y coordinate of the event relative to the main frame's viewport in CSS pixels. 0 refers to
    #: the top of the viewport and Y increases as it proceeds towards the bottom of the viewport.
    y: float
 
    #: X radius of the touch area (default: 1.0).
    radius_x: typing.Optional[float] = None
 
    #: Y radius of the touch area (default: 1.0).
    radius_y: typing.Optional[float] = None
 
    #: Rotation angle (default: 0.0).
    rotation_angle: typing.Optional[float] = None
 
    #: Force (default: 1.0).
    force: typing.Optional[float] = None
 
    #: Identifier used to track touch sources between events, must be unique within an event.
    id_: typing.Optional[float] = None
 
    def to_json(self):
        json = dict()
        json['x'] = self.x
        json['y'] = self.y
        if self.radius_x is not None:
            json['radiusX'] = self.radius_x
        if self.radius_y is not None:
            json['radiusY'] = self.radius_y
        if self.rotation_angle is not None:
            json['rotationAngle'] = self.rotation_angle
        if self.force is not None:
            json['force'] = self.force
        if self.id_ is not None:
            json['id'] = self.id_
        return json
 
    @classmethod
    def from_json(cls, json):
        return cls(
            x=float(json['x']),
            y=float(json['y']),
            radius_x=float(json['radiusX']) if 'radiusX' in json else None,
            radius_y=float(json['radiusY']) if 'radiusY' in json else None,
            rotation_angle=float(json['rotationAngle']) if 'rotationAngle' in json else None,
            force=float(json['force']) if 'force' in json else None,
            id_=float(json['id']) if 'id' in json else None,
        )
 
 
class GestureSourceType(enum.Enum):
    DEFAULT = "default"
    TOUCH = "touch"
    MOUSE = "mouse"
 
    def to_json(self):
        return self.value
 
    @classmethod
    def from_json(cls, json):
        return cls(json)
 
 
class MouseButton(enum.Enum):
    NONE = "none"
    LEFT = "left"
    MIDDLE = "middle"
    RIGHT = "right"
    BACK = "back"
    FORWARD = "forward"
 
    def to_json(self):
        return self.value
 
    @classmethod
    def from_json(cls, json):
        return cls(json)
 
 
class TimeSinceEpoch(float):
    '''
    UTC time in seconds, counted from January 1, 1970.
    '''
    def to_json(self) -> float:
        return self
 
    @classmethod
    def from_json(cls, json: float) -> TimeSinceEpoch:
        return cls(json)
 
    def __repr__(self):
        return 'TimeSinceEpoch({})'.format(super().__repr__())
 
 
def dispatch_key_event(
        type_: str,
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        text: typing.Optional[str] = None,
        unmodified_text: typing.Optional[str] = None,
        key_identifier: typing.Optional[str] = None,
        code: typing.Optional[str] = None,
        key: typing.Optional[str] = None,
        windows_virtual_key_code: typing.Optional[int] = None,
        native_virtual_key_code: typing.Optional[int] = None,
        auto_repeat: typing.Optional[bool] = None,
        is_keypad: typing.Optional[bool] = None,
        is_system_key: typing.Optional[bool] = None,
        location: typing.Optional[int] = None,
        commands: typing.Optional[typing.List[str]] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a key event to the page.
 
    :param type_: Type of the key event.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    :param text: *(Optional)* Text as generated by processing a virtual key code with a keyboard layout. Not needed for for ```keyUp```` and ````rawKeyDown```` events (default: "")
    :param unmodified_text: *(Optional)* Text that would have been generated by the keyboard if no modifiers were pressed (except for shift). Useful for shortcut (accelerator) key handling (default: "").
    :param key_identifier: *(Optional)* Unique key identifier (e.g., 'U+0041') (default: "").
    :param code: *(Optional)* Unique DOM defined string value for each physical key (e.g., 'KeyA') (default: "").
    :param key: *(Optional)* Unique DOM defined string value describing the meaning of the key in the context of active modifiers, keyboard layout, etc (e.g., 'AltGr') (default: "").
    :param windows_virtual_key_code: *(Optional)* Windows virtual key code (default: 0).
    :param native_virtual_key_code: *(Optional)* Native virtual key code (default: 0).
    :param auto_repeat: *(Optional)* Whether the event was generated from auto repeat (default: false).
    :param is_keypad: *(Optional)* Whether the event was generated from the keypad (default: false).
    :param is_system_key: *(Optional)* Whether the event was a system key event (default: false).
    :param location: *(Optional)* Whether the event was from the left or right side of the keyboard. 1=Left, 2=Right (default: 0).
    :param commands: **(EXPERIMENTAL)** *(Optional)* Editing commands to send with the key event (e.g., 'selectAll') (default: []). These are related to but not equal the command names used in ````document.execCommand``` and NSStandardKeyBindingResponding. See https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/editing/commands/editor_command_names.h for valid command names.
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if text is not None:
        params['text'] = text
    if unmodified_text is not None:
        params['unmodifiedText'] = unmodified_text
    if key_identifier is not None:
        params['keyIdentifier'] = key_identifier
    if code is not None:
        params['code'] = code
    if key is not None:
        params['key'] = key
    if windows_virtual_key_code is not None:
        params['windowsVirtualKeyCode'] = windows_virtual_key_code
    if native_virtual_key_code is not None:
        params['nativeVirtualKeyCode'] = native_virtual_key_code
    if auto_repeat is not None:
        params['autoRepeat'] = auto_repeat
    if is_keypad is not None:
        params['isKeypad'] = is_keypad
    if is_system_key is not None:
        params['isSystemKey'] = is_system_key
    if location is not None:
        params['location'] = location
    if commands is not None:
        params['commands'] = [i for i in commands]
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchKeyEvent',
        'params': params,
    }
    json = yield cmd_dict
 
 
def insert_text(
        text: str
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    This method emulates inserting text that doesn't come from a key press,
    for example an emoji keyboard or an IME.
 
    **EXPERIMENTAL**
 
    :param text: The text to insert.
    '''
    params: T_JSON_DICT = dict()
    params['text'] = text
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.insertText',
        'params': params,
    }
    json = yield cmd_dict
 
 
def dispatch_mouse_event(
        type_: str,
        x: float,
        y: float,
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        button: typing.Optional[MouseButton] = None,
        buttons: typing.Optional[int] = None,
        click_count: typing.Optional[int] = None,
        delta_x: typing.Optional[float] = None,
        delta_y: typing.Optional[float] = None,
        pointer_type: typing.Optional[str] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a mouse event to the page.
 
    :param type_: Type of the mouse event.
    :param x: X coordinate of the event relative to the main frame's viewport in CSS pixels.
    :param y: Y coordinate of the event relative to the main frame's viewport in CSS pixels. 0 refers to the top of the viewport and Y increases as it proceeds towards the bottom of the viewport.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    :param button: *(Optional)* Mouse button (default: "none").
    :param buttons: *(Optional)* A number indicating which buttons are pressed on the mouse when a mouse event is triggered. Left=1, Right=2, Middle=4, Back=8, Forward=16, None=0.
    :param click_count: *(Optional)* Number of times the mouse button was clicked (default: 0).
    :param delta_x: *(Optional)* X delta in CSS pixels for mouse wheel event (default: 0).
    :param delta_y: *(Optional)* Y delta in CSS pixels for mouse wheel event (default: 0).
    :param pointer_type: *(Optional)* Pointer type (default: "mouse").
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['x'] = x
    params['y'] = y
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if button is not None:
        params['button'] = button.to_json()
    if buttons is not None:
        params['buttons'] = buttons
    if click_count is not None:
        params['clickCount'] = click_count
    if delta_x is not None:
        params['deltaX'] = delta_x
    if delta_y is not None:
        params['deltaY'] = delta_y
    if pointer_type is not None:
        params['pointerType'] = pointer_type
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchMouseEvent',
        'params': params,
    }
    json = yield cmd_dict
 
 
def dispatch_touch_event(
        type_: str,
        touch_points: typing.List[TouchPoint],
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a touch event to the page.
 
    :param type_: Type of the touch event. TouchEnd and TouchCancel must not contain any touch points, while TouchStart and TouchMove must contains at least one.
    :param touch_points: Active touch points on the touch device. One event per any changed point (compared to previous touch event in a sequence) is generated, emulating pressing/moving/releasing points one by one.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['touchPoints'] = [i.to_json() for i in touch_points]
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchTouchEvent',
        'params': params,
    }
    json = yield cmd_dict
 
 
def emulate_touch_from_mouse_event(
        type_: str,
        x: int,
        y: int,
        button: MouseButton,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        delta_x: typing.Optional[float] = None,
        delta_y: typing.Optional[float] = None,
        modifiers: typing.Optional[int] = None,
        click_count: typing.Optional[int] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Emulates touch event from the mouse event parameters.
 
    **EXPERIMENTAL**
 
    :param type_: Type of the mouse event.
    :param x: X coordinate of the mouse pointer in DIP.
    :param y: Y coordinate of the mouse pointer in DIP.
    :param button: Mouse button. Only "none", "left", "right" are supported.
    :param timestamp: *(Optional)* Time at which the event occurred (default: current time).
    :param delta_x: *(Optional)* X delta in DIP for mouse wheel event (default: 0).
    :param delta_y: *(Optional)* Y delta in DIP for mouse wheel event (default: 0).
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param click_count: *(Optional)* Number of times the mouse button was clicked (default: 0).
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['x'] = x
    params['y'] = y
    params['button'] = button.to_json()
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if delta_x is not None:
        params['deltaX'] = delta_x
    if delta_y is not None:
        params['deltaY'] = delta_y
    if modifiers is not None:
        params['modifiers'] = modifiers
    if click_count is not None:
        params['clickCount'] = click_count
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.emulateTouchFromMouseEvent',
        'params': params,
    }
    json = yield cmd_dict
 
 
def set_ignore_input_events(
        ignore: bool
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Ignores input events (useful while auditing page).
 
    :param ignore: Ignores input events processing when set to true.
    '''
    params: T_JSON_DICT = dict()
    params['ignore'] = ignore
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.setIgnoreInputEvents',
        'params': params,
    }
    json = yield cmd_dict
 
 
def synthesize_pinch_gesture(
        x: float,
        y: float,
        scale_factor: float,
        relative_speed: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a pinch gesture over a time period by issuing appropriate touch events.
 
    **EXPERIMENTAL**
 
    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param scale_factor: Relative scale factor after zooming (>1.0 zooms in, <1.0 zooms out).
    :param relative_speed: *(Optional)* Relative pointer speed in pixels per second (default: 800).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    params['scaleFactor'] = scale_factor
    if relative_speed is not None:
        params['relativeSpeed'] = relative_speed
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizePinchGesture',
        'params': params,
    }
    json = yield cmd_dict
 
 
def synthesize_scroll_gesture(
        x: float,
        y: float,
        x_distance: typing.Optional[float] = None,
        y_distance: typing.Optional[float] = None,
        x_overscroll: typing.Optional[float] = None,
        y_overscroll: typing.Optional[float] = None,
        prevent_fling: typing.Optional[bool] = None,
        speed: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None,
        repeat_count: typing.Optional[int] = None,
        repeat_delay_ms: typing.Optional[int] = None,
        interaction_marker_name: typing.Optional[str] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a scroll gesture over a time period by issuing appropriate touch events.
 
    **EXPERIMENTAL**
 
    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param x_distance: *(Optional)* The distance to scroll along the X axis (positive to scroll left).
    :param y_distance: *(Optional)* The distance to scroll along the Y axis (positive to scroll up).
    :param x_overscroll: *(Optional)* The number of additional pixels to scroll back along the X axis, in addition to the given distance.
    :param y_overscroll: *(Optional)* The number of additional pixels to scroll back along the Y axis, in addition to the given distance.
    :param prevent_fling: *(Optional)* Prevent fling (default: true).
    :param speed: *(Optional)* Swipe speed in pixels per second (default: 800).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    :param repeat_count: *(Optional)* The number of times to repeat the gesture (default: 0).
    :param repeat_delay_ms: *(Optional)* The number of milliseconds delay between each repeat. (default: 250).
    :param interaction_marker_name: *(Optional)* The name of the interaction markers to generate, if not empty (default: "").
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    if x_distance is not None:
        params['xDistance'] = x_distance
    if y_distance is not None:
        params['yDistance'] = y_distance
    if x_overscroll is not None:
        params['xOverscroll'] = x_overscroll
    if y_overscroll is not None:
        params['yOverscroll'] = y_overscroll
    if prevent_fling is not None:
        params['preventFling'] = prevent_fling
    if speed is not None:
        params['speed'] = speed
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    if repeat_count is not None:
        params['repeatCount'] = repeat_count
    if repeat_delay_ms is not None:
        params['repeatDelayMs'] = repeat_delay_ms
    if interaction_marker_name is not None:
        params['interactionMarkerName'] = interaction_marker_name
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizeScrollGesture',
        'params': params,
    }
    json = yield cmd_dict
 
 
def synthesize_tap_gesture(
        x: float,
        y: float,
        duration: typing.Optional[int] = None,
        tap_count: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a tap gesture over a time period by issuing appropriate touch events.
 
    **EXPERIMENTAL**
 
    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param duration: *(Optional)* Duration between touchdown and touchup events in ms (default: 50).
    :param tap_count: *(Optional)* Number of times to perform the tap (e.g. 2 for double tap, default: 1).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    if duration is not None:
        params['duration'] = duration
    if tap_count is not None:
        params['tapCount'] = tap_count
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizeTapGesture',
        'params': params,
    }
    json = yield cmd_dict