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"""A Telemetry page_action that loops media playback. 6 7Action parameters are: 8- loop_count: The number of times to loop media. 9- selector: If no selector is defined then the action attempts to loop the first 10 media element on the page. If 'all' then loop all media elements. 11- timeout_in_seconds: Timeout to wait for media to loop. Default is 12 60 sec x loop_count. 0 means do not wait. 13""" 14 15from telemetry.core import exceptions 16from telemetry.internal.actions import media_action 17from telemetry.internal.actions import page_action 18 19 20class LoopAction(media_action.MediaAction): 21 def __init__(self, loop_count, selector=None, timeout_in_seconds=None): 22 super(LoopAction, self).__init__() 23 self._loop_count = loop_count 24 self._selector = selector if selector else '' 25 self._timeout_in_seconds = ( 26 timeout_in_seconds if timeout_in_seconds else 60 * loop_count) 27 28 def WillRunAction(self, tab): 29 """Load the media metrics JS code prior to running the action.""" 30 super(LoopAction, self).WillRunAction(tab) 31 self.LoadJS(tab, 'loop.js') 32 33 def RunAction(self, tab): 34 try: 35 tab.ExecuteJavaScript('window.__loopMedia("%s", %i);' % 36 (self._selector, self._loop_count)) 37 if self._timeout_in_seconds > 0: 38 self.WaitForEvent(tab, self._selector, 'loop', self._timeout_in_seconds) 39 except exceptions.EvaluateException: 40 raise page_action.PageActionFailed('Cannot loop media element(s) with ' 41 'selector = %s.' % self._selector) 42