This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
mirrored from https://chromium.googlesource.com/catapult
-
Notifications
You must be signed in to change notification settings - Fork 564
/
action_runner.py
896 lines (759 loc) · 37.2 KB
/
action_runner.py
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import division
from __future__ import absolute_import
import logging
import time
import six
import six.moves.urllib.parse # pylint: disable=import-error
from six.moves import input # pylint: disable=redefined-builtin
from telemetry.core import exceptions
from telemetry.internal.actions import page_action
from telemetry.internal.actions.drag import DragAction
from telemetry.internal.actions.javascript_click import ClickElementAction
from telemetry.internal.actions.key_event import KeyPressAction
from telemetry.internal.actions.load_media import LoadMediaAction
from telemetry.internal.actions.mouse_click import MouseClickAction
from telemetry.internal.actions.navigate import NavigateAction
from telemetry.internal.actions.page_action import GESTURE_SOURCE_DEFAULT
from telemetry.internal.actions.page_action import SUPPORTED_GESTURE_SOURCES
from telemetry.internal.actions.pinch import PinchAction
from telemetry.internal.actions.play import PlayAction
from telemetry.internal.actions.repaint_continuously import (
RepaintContinuouslyAction)
from telemetry.internal.actions.repeatable_scroll import RepeatableScrollAction
from telemetry.internal.actions.scroll import ScrollAction
from telemetry.internal.actions.scroll_bounce import ScrollBounceAction
from telemetry.internal.actions.scroll_to_element import ScrollToElementAction
from telemetry.internal.actions.seek import SeekAction
from telemetry.internal.actions.swipe import SwipeAction
from telemetry.internal.actions.tap import TapAction
from telemetry.internal.actions.wait import WaitForElementAction
from telemetry.web_perf import timeline_interaction_record
from py_trace_event import trace_event
import py_utils
# Time to wait in seconds before requesting a memory dump in deterministic
# mode, thus allowing metric values to stabilize a bit.
_MEMORY_DUMP_WAIT_TIME = 3
# Time to wait in seconds after forcing garbage collection to allow its
# effects to propagate. Experimentally determined on an Android One device
# that Java Heap garbage collection can take ~5 seconds to complete.
_GARBAGE_COLLECTION_PROPAGATION_TIME = 6
if six.PY2:
ActionRunnerBase = object
else:
ActionRunnerBase = six.with_metaclass(trace_event.TracedMetaClass, object)
class ActionRunner(ActionRunnerBase):
if six.PY2:
__metaclass__ = trace_event.TracedMetaClass
def __init__(self, tab, skip_waits=False):
self._tab = tab
self._skip_waits = skip_waits
@property
def tab(self):
"""Returns the tab on which actions are performed."""
return self._tab
def _RunAction(self, action):
logging.info("START Page Action: %s", action)
action.WillRunAction(self._tab)
action.RunAction(self._tab)
logging.info("DONE Page Action: %s", action.__class__.__name__)
def CreateInteraction(self, label, repeatable=False):
""" Create an action.Interaction object that issues interaction record.
An interaction record is a labeled time period containing
interaction that developers care about. Each set of metrics
specified in flags will be calculated for this time period.
To mark the start of interaction record, call Begin() method on the returned
object. To mark the finish of interaction record, call End() method on
it. Or better yet, use the with statement to create an
interaction record that covers the actions in the with block.
e.g:
with action_runner.CreateInteraction('Animation-1'):
action_runner.TapElement(...)
action_runner.WaitForJavaScriptCondition(...)
Args:
label: A label for this particular interaction. This can be any
user-defined string, but must not contain '/'.
repeatable: Whether other interactions may use the same logical name
as this interaction. All interactions with the same logical name must
have the same flags.
Returns:
An instance of action_runner.Interaction
"""
flags = []
if repeatable:
flags.append(timeline_interaction_record.REPEATABLE)
return Interaction(self, label, flags)
def CreateGestureInteraction(self, label, repeatable=False):
""" Create an action.Interaction object that issues gesture-based
interaction record.
This is similar to normal interaction record, but it will
auto-narrow the interaction time period to only include the
synthetic gesture event output by Chrome. This is typically use to
reduce noise in gesture-based analysis (e.g., analysis for a
swipe/scroll).
The interaction record label will be prepended with 'Gesture_'.
e.g:
with action_runner.CreateGestureInteraction('Scroll-1'):
action_runner.ScrollPage()
Args:
label: A label for this particular interaction. This can be any
user-defined string, but must not contain '/'.
repeatable: Whether other interactions may use the same logical name
as this interaction. All interactions with the same logical name must
have the same flags.
Returns:
An instance of action_runner.Interaction
"""
return self.CreateInteraction('Gesture_' + label, repeatable)
def WaitForNetworkQuiescence(self, timeout_in_seconds=10):
""" Wait for network quiesence on the page.
Args:
timeout_in_seconds: maximum amount of time (seconds) to wait for network
quiesence before raising exception.
Raises:
py_utils.TimeoutException when the timeout is reached but the page's
network is not quiet.
"""
py_utils.WaitFor(self.tab.HasReachedQuiescence, timeout_in_seconds)
def MeasureMemory(self, deterministic_mode=False):
"""Add a memory measurement to the trace being recorded.
Behaves as a no-op if tracing is not enabled.
Args:
deterministic_mode: A boolean indicating whether to attempt or not to
control the environment (force GCs, clear caches) before making the
measurement in an attempt to obtain more deterministic results.
Returns:
GUID of the generated dump if one was triggered, None otherwise.
"""
if not self.tab.browser.platform.tracing_controller.is_tracing_running:
logging.warning('Tracing is off. No memory dumps are being recorded.')
return None
if deterministic_mode:
self.Wait(_MEMORY_DUMP_WAIT_TIME)
self.ForceGarbageCollection()
dump_id = self.tab.browser.DumpMemory()
if not dump_id:
raise exceptions.StoryActionError('Unable to obtain memory dump')
return dump_id
def PrepareForLeakDetection(self):
"""Prepares for Leak Detection.
Terminate workers, stopping spellcheckers, running GC etc.
"""
self._tab.PrepareForLeakDetection()
def Navigate(self, url, script_to_evaluate_on_commit=None,
timeout_in_seconds=page_action.DEFAULT_TIMEOUT):
"""Navigates to |url|.
If |script_to_evaluate_on_commit| is given, the script source string will be
evaluated when the navigation is committed. This is after the context of
the page exists, but before any script on the page itself has executed.
"""
if six.moves.urllib.parse.urlparse(url).scheme == 'file':
url = self._tab.browser.platform.http_server.UrlOf(url[7:])
self._RunAction(NavigateAction(
url=url,
script_to_evaluate_on_commit=script_to_evaluate_on_commit,
timeout_in_seconds=timeout_in_seconds))
def NavigateBack(self):
""" Navigate back to the previous page."""
self.ExecuteJavaScript('window.history.back()')
def WaitForNavigate(
self, timeout_in_seconds_seconds=page_action.DEFAULT_TIMEOUT):
start_time = time.time()
self._tab.WaitForNavigate(timeout_in_seconds_seconds)
time_left_in_seconds = (start_time + timeout_in_seconds_seconds
- time.time())
time_left_in_seconds = max(0, time_left_in_seconds)
self._tab.WaitForDocumentReadyStateToBeInteractiveOrBetter(
time_left_in_seconds)
def ReloadPage(self):
"""Reloads the page."""
self._tab.ExecuteJavaScript('window.location.reload()')
self._tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
def ExecuteJavaScript(self, *args, **kwargs):
"""Executes a given JavaScript statement. Does not return the result.
Example: runner.ExecuteJavaScript('var foo = {{ value }};', value='hi');
Args:
statement: The statement to execute (provided as a string).
Optional keyword args:
timeout: The number of seconds to wait for the statement to execute.
Additional keyword arguments provide values to be interpolated within
the statement. See telemetry.util.js_template for details.
Raises:
EvaluationException: The statement failed to execute.
"""
if 'timeout' not in kwargs:
kwargs['timeout'] = page_action.DEFAULT_TIMEOUT
return self._tab.ExecuteJavaScript(*args, **kwargs)
def EvaluateJavaScript(self, *args, **kwargs):
"""Returns the result of evaluating a given JavaScript expression.
The evaluation results must be convertible to JSON. If the result
is not needed, use ExecuteJavaScript instead.
Example: runner.ExecuteJavaScript('document.location.href');
Args:
expression: The expression to execute (provided as a string).
Optional keyword args:
timeout: The number of seconds to wait for the expression to evaluate.
Additional keyword arguments provide values to be interpolated within
the expression. See telemetry.util.js_template for details.
Raises:
EvaluationException: The statement expression failed to execute
or the evaluation result can not be JSON-ized.
"""
if 'timeout' not in kwargs:
kwargs['timeout'] = page_action.DEFAULT_TIMEOUT
return self._tab.EvaluateJavaScript(*args, **kwargs)
def WaitForJavaScriptCondition(self, *args, **kwargs):
"""Wait for a JavaScript condition to become true.
Example: runner.WaitForJavaScriptCondition('window.foo == 10');
Args:
condition: The JavaScript condition (provided as string).
Optional keyword args:
timeout: The number in seconds to wait for the condition to become
True (default to 60).
Additional keyword arguments provide values to be interpolated within
the expression. See telemetry.util.js_template for details.
"""
if 'timeout' not in kwargs:
kwargs['timeout'] = page_action.DEFAULT_TIMEOUT
return self._tab.WaitForJavaScriptCondition(*args, **kwargs)
def Wait(self, seconds):
"""Wait for the number of seconds specified.
Args:
seconds: The number of seconds to wait.
"""
if not self._skip_waits:
time.sleep(seconds)
def WaitForElement(self, selector=None, text=None, element_function=None,
timeout_in_seconds=page_action.DEFAULT_TIMEOUT):
"""Wait for an element to appear in the document.
The element may be selected via selector, text, or element_function.
Only one of these arguments must be specified.
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'(function() { return foo.element; })()'.
timeout_in_seconds: The timeout in seconds.
"""
self._RunAction(WaitForElementAction(
selector=selector, text=text, element_function=element_function,
timeout=timeout_in_seconds))
def TapElement(self, selector=None, text=None, element_function=None,
timeout=page_action.DEFAULT_TIMEOUT):
"""Tap an element.
The element may be selected via selector, text, or element_function.
Only one of these arguments must be specified.
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'(function() { return foo.element; })()'.
"""
self._RunAction(TapAction(
selector=selector, text=text, element_function=element_function,
timeout=timeout))
def ClickElement(self, selector=None, text=None, element_function=None):
"""Click an element.
The element may be selected via selector, text, or element_function.
Only one of these arguments must be specified.
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'(function() { return foo.element; })()'.
"""
self._RunAction(ClickElementAction(
selector=selector, text=text, element_function=element_function))
def DragPage(self, left_start_ratio, top_start_ratio, left_end_ratio,
top_end_ratio, speed_in_pixels_per_second=800, use_touch=False,
selector=None, text=None, element_function=None):
"""Perform a drag gesture on the page.
You should specify a start and an end point in ratios of page width and
height (see drag.js for full implementation).
Args:
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
left_end_ratio: The horizontal ending coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_end_ratio: The vertical ending coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
use_touch: Whether dragging should be done with touch input.
"""
self._RunAction(DragAction(
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
left_end_ratio=left_end_ratio, top_end_ratio=top_end_ratio,
speed_in_pixels_per_second=speed_in_pixels_per_second,
use_touch=use_touch, selector=selector, text=text,
element_function=element_function))
def PinchPage(self, left_anchor_ratio=0.5, top_anchor_ratio=0.5,
scale_factor=None, speed_in_pixels_per_second=800):
"""Perform the pinch gesture on the page.
It computes the pinch gesture automatically based on the anchor
coordinate and the scale factor. The scale factor is the ratio of
of the final span and the initial span of the gesture.
Args:
left_anchor_ratio: The horizontal pinch anchor coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_anchor_ratio: The vertical pinch anchor coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
scale_factor: The ratio of the final span to the initial span.
The default scale factor is 3.0 / (current scale factor).
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
"""
self._RunAction(PinchAction(
left_anchor_ratio=left_anchor_ratio, top_anchor_ratio=top_anchor_ratio,
scale_factor=scale_factor,
speed_in_pixels_per_second=speed_in_pixels_per_second))
def ScrollPage(self, left_start_ratio=0.5, top_start_ratio=0.5,
direction='down', distance=None, distance_expr=None,
speed_in_pixels_per_second=800, use_touch=False,
synthetic_gesture_source=GESTURE_SOURCE_DEFAULT):
"""Perform scroll gesture on the page.
You may specify distance or distance_expr, but not both. If
neither is specified, the default scroll distance is variable
depending on direction (see scroll.js for full implementation).
Args:
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
direction: The direction of scroll, either 'left', 'right',
'up', 'down', 'upleft', 'upright', 'downleft', or 'downright'
distance: The distance to scroll (in pixel).
distance_expr: A JavaScript expression (as string) that can be
evaluated to compute scroll distance. Example:
'window.scrollTop' or '(function() { return crazyMath(); })()'.
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
use_touch: Whether scrolling should be done with touch input.
synthetic_gesture_source: the source input device type for the
synthetic gesture: 'DEFAULT', 'TOUCH' or 'MOUSE'.
"""
assert synthetic_gesture_source in SUPPORTED_GESTURE_SOURCES
self._RunAction(ScrollAction(
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance, distance_expr=distance_expr,
speed_in_pixels_per_second=speed_in_pixels_per_second,
use_touch=use_touch, synthetic_gesture_source=synthetic_gesture_source))
def ScrollPageToElement(self, selector=None, element_function=None,
container_selector=None,
container_element_function=None,
speed_in_pixels_per_second=800):
"""Perform scroll gesture on container until an element is in view.
Both the element and the container can be specified by a CSS selector
xor a JavaScript function, provided as a string, which returns an element.
The element is required so exactly one of selector and element_function
must be provided. The container is optional so at most one of
container_selector and container_element_function can be provided.
The container defaults to document.scrollingElement or document.body if
scrollingElement is not set.
Args:
selector: A CSS selector describing the element.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'function() { return foo.element; }'.
container_selector: A CSS selector describing the container element.
container_element_function: A JavaScript function (as a string) that is
used to retrieve the container element.
speed_in_pixels_per_second: Speed to scroll.
"""
self._RunAction(ScrollToElementAction(
selector=selector, element_function=element_function,
container_selector=container_selector,
container_element_function=container_element_function,
speed_in_pixels_per_second=speed_in_pixels_per_second))
def RepeatableBrowserDrivenScroll(self, x_scroll_distance_ratio=0.0,
y_scroll_distance_ratio=0.5,
repeat_count=0,
repeat_delay_ms=250,
timeout=page_action.DEFAULT_TIMEOUT,
prevent_fling=None,
speed=None):
"""Perform a browser driven repeatable scroll gesture.
The scroll gesture is driven from the browser, this is useful because the
main thread often isn't resposive but the browser process usually is, so the
delay between the scroll gestures should be consistent.
Args:
x_scroll_distance_ratio: The horizontal length of the scroll as a fraction
of the screen width.
y_scroll_distance_ratio: The vertical length of the scroll as a fraction
of the screen height.
repeat_count: The number of additional times to repeat the gesture.
repeat_delay_ms: The delay in milliseconds between each scroll gesture.
prevent_fling: Prevents a fling gesture.
speed: Swipe speed in pixels per second.
"""
self._RunAction(RepeatableScrollAction(
x_scroll_distance_ratio=x_scroll_distance_ratio,
y_scroll_distance_ratio=y_scroll_distance_ratio,
repeat_count=repeat_count,
repeat_delay_ms=repeat_delay_ms, timeout=timeout,
prevent_fling=prevent_fling, speed=speed))
def ScrollElement(self, selector=None, text=None, element_function=None,
left_start_ratio=0.5, top_start_ratio=0.5,
direction='down', distance=None, distance_expr=None,
speed_in_pixels_per_second=800, use_touch=False,
synthetic_gesture_source=GESTURE_SOURCE_DEFAULT):
"""Perform scroll gesture on the element.
The element may be selected via selector, text, or element_function.
Only one of these arguments must be specified.
You may specify distance or distance_expr, but not both. If
neither is specified, the default scroll distance is variable
depending on direction (see scroll.js for full implementation).
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'function() { return foo.element; }'.
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
the element.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
the element.
direction: The direction of scroll, either 'left', 'right',
'up', 'down', 'upleft', 'upright', 'downleft', or 'downright'
distance: The distance to scroll (in pixel).
distance_expr: A JavaScript expression (as string) that can be
evaluated to compute scroll distance. Example:
'window.scrollTop' or '(function() { return crazyMath(); })()'.
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
use_touch: Whether scrolling should be done with touch input.
synthetic_gesture_source: the source input device type for the
synthetic gesture: 'DEFAULT', 'TOUCH' or 'MOUSE'.
"""
assert synthetic_gesture_source in SUPPORTED_GESTURE_SOURCES
self._RunAction(ScrollAction(
selector=selector, text=text, element_function=element_function,
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance, distance_expr=distance_expr,
speed_in_pixels_per_second=speed_in_pixels_per_second,
use_touch=use_touch, synthetic_gesture_source=synthetic_gesture_source))
def ScrollBouncePage(self, left_start_ratio=0.5, top_start_ratio=0.5,
direction='down', distance=100,
overscroll=10, repeat_count=10,
speed_in_pixels_per_second=400):
"""Perform scroll bounce gesture on the page.
This gesture scrolls the page by the number of pixels specified in
distance, in the given direction, followed by a scroll by
(distance + overscroll) pixels in the opposite direction.
The above gesture is repeated repeat_count times.
Args:
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
direction: The direction of scroll, either 'left', 'right',
'up', 'down', 'upleft', 'upright', 'downleft', or 'downright'
distance: The distance to scroll (in pixel).
overscroll: The number of additional pixels to scroll back, in
addition to the givendistance.
repeat_count: How often we want to repeat the full gesture.
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
"""
self._RunAction(ScrollBounceAction(
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance,
overscroll=overscroll, repeat_count=repeat_count,
speed_in_pixels_per_second=speed_in_pixels_per_second))
def ScrollBounceElement(
self, selector=None, text=None, element_function=None,
left_start_ratio=0.5, top_start_ratio=0.5,
direction='down', distance=100,
overscroll=10, repeat_count=10,
speed_in_pixels_per_second=400):
"""Perform scroll bounce gesture on the element.
This gesture scrolls on the element by the number of pixels specified in
distance, in the given direction, followed by a scroll by
(distance + overscroll) pixels in the opposite direction.
The above gesture is repeated repeat_count times.
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'function() { return foo.element; }'.
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
direction: The direction of scroll, either 'left', 'right',
'up', 'down', 'upleft', 'upright', 'downleft', or 'downright'
distance: The distance to scroll (in pixel).
overscroll: The number of additional pixels to scroll back, in
addition to the given distance.
repeat_count: How often we want to repeat the full gesture.
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
"""
self._RunAction(ScrollBounceAction(
selector=selector, text=text, element_function=element_function,
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance,
overscroll=overscroll, repeat_count=repeat_count,
speed_in_pixels_per_second=speed_in_pixels_per_second))
def MouseClick(self, selector=None, timeout=page_action.DEFAULT_TIMEOUT):
"""Mouse click the given element.
Args:
selector: A CSS selector describing the element.
"""
self._RunAction(MouseClickAction(selector=selector, timeout=timeout))
def SwipePage(self, left_start_ratio=0.5, top_start_ratio=0.5,
direction='left', distance=100, speed_in_pixels_per_second=800,
timeout=page_action.DEFAULT_TIMEOUT):
"""Perform swipe gesture on the page.
Args:
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
document.body.
direction: The direction of swipe, either 'left', 'right',
'up', or 'down'
distance: The distance to swipe (in pixel).
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
"""
self._RunAction(SwipeAction(
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance,
speed_in_pixels_per_second=speed_in_pixels_per_second,
timeout=timeout))
def SwipeElement(self, selector=None, text=None, element_function=None,
left_start_ratio=0.5, top_start_ratio=0.5,
direction='left', distance=100,
speed_in_pixels_per_second=800,
timeout=page_action.DEFAULT_TIMEOUT):
"""Perform swipe gesture on the element.
The element may be selected via selector, text, or element_function.
Only one of these arguments must be specified.
Args:
selector: A CSS selector describing the element.
text: The element must contains this exact text.
element_function: A JavaScript function (as string) that is used
to retrieve the element. For example:
'function() { return foo.element; }'.
left_start_ratio: The horizontal starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
the element.
top_start_ratio: The vertical starting coordinate of the
gesture, as a ratio of the visible bounding rectangle for
the element.
direction: The direction of swipe, either 'left', 'right',
'up', or 'down'
distance: The distance to swipe (in pixel).
speed_in_pixels_per_second: The speed of the gesture (in pixels/s).
"""
self._RunAction(SwipeAction(
selector=selector, text=text, element_function=element_function,
left_start_ratio=left_start_ratio, top_start_ratio=top_start_ratio,
direction=direction, distance=distance,
speed_in_pixels_per_second=speed_in_pixels_per_second,
timeout=timeout))
def PressKey(self, key, repeat_count=1, repeat_delay_ms=100,
timeout=page_action.DEFAULT_TIMEOUT):
"""Perform a key press.
Args:
key: DOM value of the pressed key (e.g. 'PageDown', see
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key).
repeat_count: How many times the key should be pressed.
repeat_delay_ms: Delay after each keypress (including the last one) in
milliseconds.
"""
for _ in range(repeat_count):
self._RunAction(KeyPressAction(key, timeout=timeout))
#2To3-division: this line is unchanged as result is expected floats.
self.Wait(repeat_delay_ms / 1000.0)
def EnterText(self, text, character_delay_ms=100,
timeout=page_action.DEFAULT_TIMEOUT):
"""Enter text by performing key presses.
Args:
text: The text to enter.
character_delay_ms: Delay after each keypress (including the last one) in
milliseconds.
"""
for c in text:
self.PressKey(c, repeat_delay_ms=character_delay_ms, timeout=timeout)
def LoadMedia(self, selector=None, event_timeout_in_seconds=0,
event_to_await='canplaythrough'):
"""Invokes load() on media elements and awaits an event.
Args:
selector: A CSS selector describing the element. If none is
specified, play the first media element on the page. If the
selector matches more than 1 media element, all of them will
be played.
event_timeout_in_seconds: Maximum waiting time for the event to be fired.
0 means do not wait.
event_to_await: Which event to await. For example: 'canplaythrough' or
'loadedmetadata'.
Raises:
TimeoutException: If the maximum waiting time is exceeded.
"""
self._RunAction(LoadMediaAction(
selector=selector, timeout_in_seconds=event_timeout_in_seconds,
event_to_await=event_to_await))
def PlayMedia(self, selector=None,
playing_event_timeout_in_seconds=0,
ended_event_timeout_in_seconds=0):
"""Invokes the "play" action on media elements (such as video).
Args:
selector: A CSS selector describing the element. If none is
specified, play the first media element on the page. If the
selector matches more than 1 media element, all of them will
be played.
playing_event_timeout_in_seconds: Maximum waiting time for the "playing"
event (dispatched when the media begins to play) to be fired.
0 means do not wait.
ended_event_timeout_in_seconds: Maximum waiting time for the "ended"
event (dispatched when playback completes) to be fired.
0 means do not wait.
Raises:
TimeoutException: If the maximum waiting time is exceeded.
"""
self._RunAction(PlayAction(
selector=selector,
playing_event_timeout_in_seconds=playing_event_timeout_in_seconds,
ended_event_timeout_in_seconds=ended_event_timeout_in_seconds))
def SeekMedia(self, seconds, selector=None, timeout_in_seconds=0,
log_time=True, label=''):
"""Performs a seek action on media elements (such as video).
Args:
seconds: The media time to seek to.
selector: A CSS selector describing the element. If none is
specified, seek the first media element on the page. If the
selector matches more than 1 media element, all of them will
be seeked.
timeout_in_seconds: Maximum waiting time for the "seeked" event
(dispatched when the seeked operation completes) to be
fired. 0 means do not wait.
log_time: Whether to log the seek time for the perf
measurement. Useful when performing multiple seek.
label: A suffix string to name the seek perf measurement.
Raises:
TimeoutException: If the maximum waiting time is exceeded.
"""
self._RunAction(SeekAction(
seconds=seconds, selector=selector,
timeout_in_seconds=timeout_in_seconds,
log_time=log_time, label=label))
def ForceGarbageCollection(self):
"""Forces garbage collection on all relevant systems.
This includes:
- Java heap for browser and child subprocesses (on Android).
- JavaScript on the current renderer.
- System caches (on supported platforms).
"""
# 1) Perform V8 and Blink garbage collection. This may free java wrappers.
self._tab.CollectGarbage()
# 2) Perform Java garbage collection
if self._tab.browser.supports_java_heap_garbage_collection:
self._tab.browser.ForceJavaHeapGarbageCollection()
# 3) Flush system caches. This affects GPU memory.
if self._tab.browser.platform.SupportFlushEntireSystemCache():
self._tab.browser.platform.FlushEntireSystemCache()
# 4) Wait until the effect of Java GC and system cache flushing propagates.
self.Wait(_GARBAGE_COLLECTION_PROPAGATION_TIME)
# 5) Re-do V8 and Blink garbage collection to free garbage allocated
# while waiting.
self._tab.CollectGarbage()
# 6) Finally, finish with V8 and Blink garbage collection because some
# objects require V8 GC => Blink GC => V8 GC roundtrip.
self._tab.CollectGarbage()
def SimulateMemoryPressureNotification(self, pressure_level):
"""Simulate memory pressure notification.
Args:
pressure_level: 'moderate' or 'critical'.
"""
self._tab.browser.SimulateMemoryPressureNotification(pressure_level)
def EnterOverviewMode(self):
if not self._tab.browser.supports_overview_mode:
raise exceptions.StoryActionError('Overview mode is not supported')
self._tab.browser.EnterOverviewMode()
def ExitOverviewMode(self):
if not self._tab.browser.supports_overview_mode:
raise exceptions.StoryActionError('Overview mode is not supported')
self._tab.browser.ExitOverviewMode()
def PauseInteractive(self):
"""Pause the page execution and wait for terminal interaction.
This is typically used for debugging. You can use this to pause
the page execution and inspect the browser state before
continuing.
"""
input("Interacting... Press Enter to continue.")
def RepaintContinuously(self, seconds):
"""Continuously repaints the visible content.
It does this by requesting animation frames until the given number
of seconds have elapsed AND at least three RAFs have been
fired. Times out after max(60, self.seconds), if less than three
RAFs were fired."""
self._RunAction(RepaintContinuouslyAction(
seconds=0 if self._skip_waits else seconds))
def StartMobileDeviceEmulation(
self, width=360, height=640, dsr=2, timeout=60):
"""Emulates a mobile device.
This method is intended for benchmarks used to gather non-performance
metrics only. Mobile emulation is not guaranteed to have the same
performance characteristics as real devices.
Example device parameters:
https://gist.github.com/devinmancuso/0c94410cb14c83ddad6f
Args:
width: Screen width.
height: Screen height.
dsr: Screen device scale factor.
"""
self._tab.StartMobileDeviceEmulation(width, height, dsr, timeout)
def StopMobileDeviceEmulation(self, timeout=60):
"""Stops emulation of a mobile device."""
self._tab.StopMobileDeviceEmulation(timeout)
class Interaction(object):
def __init__(self, action_runner, label, flags):
assert action_runner
assert label
assert isinstance(flags, list)
self._action_runner = action_runner
self._label = label
self._flags = flags
self._started = False
def __enter__(self):
self.Begin()
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_value is None:
self.End()
else:
logging.warning(
'Exception was raised in the with statement block, the end of '
'interaction record is not marked.')
def Begin(self):
assert not self._started
self._started = True
self._action_runner.ExecuteJavaScript(
'console.time({{ marker }});',
marker=timeline_interaction_record.GetJavaScriptMarker(
self._label, self._flags))
def End(self):
assert self._started
self._started = False
self._action_runner.ExecuteJavaScript(
'console.timeEnd({{ marker }});',
marker=timeline_interaction_record.GetJavaScriptMarker(
self._label, self._flags))