1# Copyright 2015 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 5from telemetry.core import exceptions 6from telemetry.internal.actions import media_action 7from telemetry.internal.actions import page_action 8 9 10class LoadMediaAction(media_action.MediaAction): 11 """ For calling load() on media elements and waiting for an event to fire. 12 """ 13 14 def __init__(self, selector=None, timeout_in_seconds=0, 15 event_to_await='canplaythrough'): 16 super(LoadMediaAction, self).__init__() 17 self._selector = selector or '' 18 self._timeout_in_seconds = timeout_in_seconds 19 self._event_to_await = event_to_await 20 21 def WillRunAction(self, tab): 22 """Load the JS code prior to running the action.""" 23 super(LoadMediaAction, self).WillRunAction(tab) 24 self.LoadJS(tab, 'load_media.js') 25 26 def RunAction(self, tab): 27 try: 28 tab.ExecuteJavaScript('window.__loadMediaAndAwait("%s", "%s");' 29 % (self._selector, self._event_to_await)) 30 if self._timeout_in_seconds > 0: 31 self.WaitForEvent(tab, self._selector, self._event_to_await, 32 self._timeout_in_seconds) 33 except exceptions.EvaluateException: 34 raise page_action.PageActionFailed('Failed waiting for event "%s" on ' 35 'elements with selector = %s.' % 36 (self._event_to_await, self._selector)) 37