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
3 # found in the LICENSE file.
5 """Common media action functions."""
10 from telemetry.core import util
11 from telemetry.page.actions import page_action
14 class MediaAction(page_action.PageAction):
15 def WillRunAction(self, tab):
16 """Loads the common media action JS code prior to running the action."""
17 self.LoadJS(tab, 'media_action.js')
19 def RunAction(self, tab):
20 super(MediaAction, self).RunAction(tab)
22 def LoadJS(self, tab, js_file_name):
23 """Loads and executes a JS file in the tab."""
24 with open(os.path.join(os.path.dirname(__file__), js_file_name)) as f:
26 tab.ExecuteJavaScript(js)
28 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds):
29 """Halts media action until the selector's event is fired.
32 tab: The tab to check for event on.
33 selector: Media element selector.
34 event_name: Name of the event to check if fired or not.
35 timeout_in_seconds: Timeout to check for event, throws an exception if
39 self.HasEventCompletedOrError(tab, selector, event_name),
40 timeout=timeout_in_seconds)
42 def HasEventCompletedOrError(self, tab, selector, event_name):
43 if tab.EvaluateJavaScript(
44 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)):
46 error = tab.EvaluateJavaScript('window.__error')
48 logging.error('Detected media error while waiting for %s: %s', event_name,