OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """A Telemetry page_action that performs the "play" action on media elements. | 5 """A Telemetry page_action that performs the "play" action on media elements. |
6 | 6 |
7 Media elements can be specified by a selector attribute. If no selector is | 7 Media elements can be specified by a selector argument. If no selector is |
8 defined then then the action attempts to play the first video element or audio | 8 defined then then the action attempts to play the first video element or audio |
9 element on the page. A selector can also be 'all' to play all media elements. | 9 element on the page. A selector can also be 'all' to play all media elements. |
10 | 10 |
11 Other attributes to use are: wait_for_playing and wait_for_ended, which forces | 11 Other arguments to use are: playing_event_timeout_in_seconds and |
12 the action to wait until playing and ended events get fired respectively. | 12 ended_event_timeout_in_seconds, which forces the action to wait until |
| 13 playing and ended events get fired respectively. |
13 """ | 14 """ |
14 | 15 |
15 from telemetry.core import exceptions | 16 from telemetry.core import exceptions |
16 from telemetry.page.actions import media_action | 17 from telemetry.page.actions import media_action |
17 from telemetry.page.actions import page_action | 18 from telemetry.page.actions import page_action |
18 | 19 |
19 | 20 |
20 class PlayAction(media_action.MediaAction): | 21 class PlayAction(media_action.MediaAction): |
21 def __init__(self, attributes=None): | 22 def __init__(self, selector=None, |
22 super(PlayAction, self).__init__(attributes) | 23 playing_event_timeout_in_seconds=0, |
| 24 ended_event_timeout_in_seconds=0): |
| 25 super(PlayAction, self).__init__() |
| 26 self._selector = selector if selector else '' |
| 27 self._playing_event_timeout_in_seconds = playing_event_timeout_in_seconds |
| 28 self._ended_event_timeout_in_seconds = ended_event_timeout_in_seconds |
23 | 29 |
24 def WillRunAction(self, tab): | 30 def WillRunAction(self, tab): |
25 """Load the media metrics JS code prior to running the action.""" | 31 """Load the media metrics JS code prior to running the action.""" |
26 super(PlayAction, self).WillRunAction(tab) | 32 super(PlayAction, self).WillRunAction(tab) |
27 self.LoadJS(tab, 'play.js') | 33 self.LoadJS(tab, 'play.js') |
28 | 34 |
29 def RunAction(self, tab): | 35 def RunAction(self, tab): |
30 try: | 36 try: |
31 selector = self.selector if hasattr(self, 'selector') else '' | 37 tab.ExecuteJavaScript('window.__playMedia("%s");' % self._selector) |
32 tab.ExecuteJavaScript('window.__playMedia("%s");' % selector) | |
33 timeout_in_seconds = (self.wait_timeout_in_seconds | |
34 if hasattr(self, 'wait_timeout_in_seconds') else 60) | |
35 # Check if we need to wait for 'playing' event to fire. | 38 # Check if we need to wait for 'playing' event to fire. |
36 if hasattr(self, 'wait_for_playing') and self.wait_for_playing: | 39 if self._playing_event_timeout_in_seconds > 0: |
37 self.WaitForEvent(tab, selector, 'playing', timeout_in_seconds) | 40 self.WaitForEvent(tab, self._selector, 'playing', |
| 41 self._playing_event_timeout_in_seconds) |
38 # Check if we need to wait for 'ended' event to fire. | 42 # Check if we need to wait for 'ended' event to fire. |
39 if hasattr(self, 'wait_for_ended') and self.wait_for_ended: | 43 if self._ended_event_timeout_in_seconds > 0: |
40 self.WaitForEvent(tab, selector, 'ended', timeout_in_seconds) | 44 self.WaitForEvent(tab, self._selector, 'ended', |
| 45 self._ended_event_timeout_in_seconds) |
41 except exceptions.EvaluateException: | 46 except exceptions.EvaluateException: |
42 raise page_action.PageActionFailed('Cannot play media element(s) with ' | 47 raise page_action.PageActionFailed('Cannot play media element(s) with ' |
43 'selector = %s.' % selector) | 48 'selector = %s.' % self._selector) |
OLD | NEW |