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 performs the "play" action on media elements. | |
6 | |
7 Media elements can be specified by a selector attribute. If no selector is | |
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. | |
10 | |
11 Other attributes to use are: wait_for_playing and wait_for_ended, which forces | |
12 the action to wait untill playing and ended events get fired respectively. | |
13 """ | |
14 | |
15 import os | |
16 | |
17 from telemetry.core import exceptions | |
18 from telemetry.core import util | |
19 from telemetry.page.actions import page_action | |
20 | |
21 | |
22 class PlayAction(page_action.PageAction): | |
23 def __init__(self, attributes=None): | |
24 super(PlayAction, self).__init__(attributes) | |
25 | |
26 def WillRunAction(self, page, tab): | |
27 """Load the media metrics JS code prior to running the action.""" | |
28 deps_found = tab.EvaluateJavaScript('window.__mediaRecorders != undefined') | |
29 if not deps_found: | |
scherkus (not reviewing)
2013/06/14 20:50:45
extra space after not
shadi
2013/06/17 19:13:59
Done.
Nice catch, gpylint did not catch it.
| |
30 with open( | |
31 # TODO(shadi): Find or define a global path to src/tools in Telemetry. | |
32 os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', | |
33 'perf', 'perf_tools', 'media_metrics.js')) as f: | |
34 js = f.read() | |
35 tab.ExecuteJavaScript(js) | |
36 | |
37 def RunAction(self, page, tab, previous_action): | |
38 try: | |
39 selector = self.selector if hasattr(self, 'selector') else '' | |
40 tab.ExecuteJavaScript('window.__playMedia(\'%s\');' % selector) | |
41 # Check if we need to wait for 'playing' event to fire. | |
42 if hasattr(self, 'wait_for_playing') and self.wait_for_playing: | |
43 self.WaitForEvent(tab, selector, 'playing') | |
44 # Check if we need to wait for 'ended' event to fire. | |
45 if hasattr(self, 'wait_for_ended') and self.wait_for_ended: | |
46 self.WaitForEvent(tab, selector, 'ended') | |
47 except exceptions.EvaluateException: | |
48 raise page_action.PageActionFailed('Cannot play media element(s) with ' | |
49 'selector = %s.' % selector) | |
50 | |
51 def WaitForEvent(self, tab, selector, event_name): | |
52 """Halts play action untill the selector's event is fired.""" | |
scherkus (not reviewing)
2013/06/14 20:50:45
s/untill/until/
shadi
2013/06/17 19:13:59
Done.
| |
53 # TODO(shadi): add action attribute to control timeout for videos that take | |
54 # longer than 60 secs to end. | |
55 util.WaitFor(lambda: self.HasEventCompleted(tab, selector, event_name), | |
56 timeout=60, poll_interval=0.5) | |
57 | |
58 def HasEventCompleted(self, tab, selector, event_name): | |
59 return tab.EvaluateJavaScript( | |
60 'window.__hasEventCompleted(\'%s\', \'%s\');' % (selector, event_name)) | |
OLD | NEW |