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. 4 5"""Common media action functions.""" 6 7import logging 8 9from telemetry.internal.actions import page_action 10from telemetry.internal.actions import utils 11 12import py_utils 13 14 15class MediaAction(page_action.PageAction): 16 def WillRunAction(self, tab): 17 """Loads the common media action JS code prior to running the action.""" 18 utils.InjectJavaScript(tab, 'media_action.js') 19 20 def RunAction(self, tab): 21 super(MediaAction, self).RunAction(tab) 22 23 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds): 24 """Halts media action until the selector's event is fired. 25 26 Args: 27 tab: The tab to check for event on. 28 selector: Media element selector. 29 event_name: Name of the event to check if fired or not. 30 timeout_in_seconds: Timeout to check for event, throws an exception if 31 not fired. 32 """ 33 py_utils.WaitFor( 34 lambda: self.HasEventCompletedOrError(tab, selector, event_name), 35 timeout=timeout_in_seconds) 36 37 def HasEventCompletedOrError(self, tab, selector, event_name): 38 if tab.EvaluateJavaScript( 39 'window.__hasEventCompleted({{ selector }}, {{ event_name }});', 40 selector=selector, event_name=event_name): 41 return True 42 error = tab.EvaluateJavaScript('window.__error') 43 if error: 44 logging.error('Detected media error while waiting for %s: %s', event_name, 45 error) 46 return True 47 return False 48