OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """A Telemetry page_action that loops media playback. |
| 6 |
| 7 Action attributes are: |
| 8 - loop_count: The number of times to loop media. |
| 9 - selector: If no selector is defined then the action attempts to loop the first |
| 10 media element on the page. If 'all' then loop all media elements. |
| 11 - wait_timeout: Timeout to wait for media to loop. Default is |
| 12 60 sec x loop_count. |
| 13 - wait_for_loop: If true, forces the action to wait for last loop to end, |
| 14 otherwise it starts the loops and exit. Default true. |
| 15 """ |
| 16 |
| 17 from telemetry.core import exceptions |
| 18 from telemetry.page.actions import page_action |
| 19 import telemetry.page.actions.media_action as media_action |
| 20 |
| 21 |
| 22 class LoopAction(media_action.MediaAction): |
| 23 def WillRunAction(self, page, tab): |
| 24 """Load the media metrics JS code prior to running the action.""" |
| 25 super(LoopAction, self).WillRunAction(page, tab) |
| 26 self.LoadJS(tab, 'loop.js') |
| 27 |
| 28 def RunAction(self, page, tab, previous_action): |
| 29 try: |
| 30 assert hasattr(self, 'loop_count') and self.loop_count > 0 |
| 31 selector = self.selector if hasattr(self, 'selector') else '' |
| 32 tab.ExecuteJavaScript('window.__loopMedia("%s", %i);' % |
| 33 (selector, self.loop_count)) |
| 34 timeout = (self.wait_timeout if hasattr(self, 'wait_timeout') |
| 35 else 60 * self.loop_count) |
| 36 # Check if there is no need to wait for all loops to end |
| 37 if hasattr(self, 'wait_for_loop') and not self.wait_for_loop: |
| 38 return |
| 39 self.WaitForEvent(tab, selector, 'loop', timeout) |
| 40 except exceptions.EvaluateException: |
| 41 raise page_action.PageActionFailed('Cannot loop media element(s) with ' |
| 42 'selector = %s.' % selector) |
OLD | NEW |