Index: tools/telemetry/telemetry/page/actions/play.py |
diff --git a/tools/telemetry/telemetry/page/actions/play.py b/tools/telemetry/telemetry/page/actions/play.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0234db200a42e311800f426c6f20dda66ae6e415 |
--- /dev/null |
+++ b/tools/telemetry/telemetry/page/actions/play.py |
@@ -0,0 +1,60 @@ |
+# Copyright (c) 2013 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. |
+ |
+"""A Telemetry page_action that performs the "play" action on media elements. |
+ |
+Media elements can be specified by a selector attribute. If no selector is |
+defined then then the action attempts to play the first video element or audio |
+element on the page. A selector can also be 'all' to play all media elements. |
+ |
+Other attributes to use are: wait_for_playing and wait_for_ended, which forces |
+the action to wait untill playing and ended events get fired respectively. |
+""" |
+ |
+import os |
+ |
+from telemetry.core import exceptions |
+from telemetry.core import util |
+from telemetry.page.actions import page_action |
+ |
+ |
+class PlayAction(page_action.PageAction): |
+ def __init__(self, attributes=None): |
+ super(PlayAction, self).__init__(attributes) |
+ |
+ def WillRunAction(self, page, tab): |
+ """Load the media metrics JS code prior to running the action.""" |
+ deps_found = tab.EvaluateJavaScript('window.__mediaRecorders != undefined') |
+ if not deps_found: |
+ with open( |
nduca
2013/06/17 23:01:33
you should have play.js here --- you cant share co
shadi
2013/06/18 00:30:01
But it is the same code that we want to run. What
|
+ # TODO(shadi): Find or define a global path to src/tools in Telemetry. |
+ os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', |
+ 'perf', 'perf_tools', 'media_metrics.js')) as f: |
+ js = f.read() |
+ tab.ExecuteJavaScript(js) |
+ |
+ def RunAction(self, page, tab, previous_action): |
+ try: |
+ selector = self.selector if hasattr(self, 'selector') else '' |
+ tab.ExecuteJavaScript('window.__playMedia(\'%s\');' % selector) |
+ # Check if we need to wait for 'playing' event to fire. |
+ if hasattr(self, 'wait_for_playing') and self.wait_for_playing: |
+ self.WaitForEvent(tab, selector, 'playing') |
+ # Check if we need to wait for 'ended' event to fire. |
+ if hasattr(self, 'wait_for_ended') and self.wait_for_ended: |
+ self.WaitForEvent(tab, selector, 'ended') |
+ except exceptions.EvaluateException: |
+ raise page_action.PageActionFailed('Cannot play media element(s) with ' |
+ 'selector = %s.' % selector) |
+ |
+ def WaitForEvent(self, tab, selector, event_name): |
+ """Halts play action until the selector's event is fired.""" |
+ # TODO(shadi): add action attribute to control timeout for videos that take |
+ # longer than 60 secs to end. |
+ util.WaitFor(lambda: self.HasEventCompleted(tab, selector, event_name), |
+ timeout=60, poll_interval=0.5) |
+ |
+ def HasEventCompleted(self, tab, selector, event_name): |
+ return tab.EvaluateJavaScript( |
+ 'window.__hasEventCompleted(\'%s\', \'%s\');' % (selector, event_name)) |