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 18from telemetry.internal.actions import utils 19 20 21class LoopAction(media_action.MediaAction): 22 def __init__(self, loop_count, selector=None, timeout_in_seconds=None): 23 super(LoopAction, self).__init__() 24 self._loop_count = loop_count 25 self._selector = selector if selector else '' 26 self._timeout_in_seconds = ( 27 timeout_in_seconds if timeout_in_seconds else 60 * loop_count) 28 29 def WillRunAction(self, tab): 30 """Load the media metrics JS code prior to running the action.""" 31 super(LoopAction, self).WillRunAction(tab) 32 utils.InjectJavaScript(tab, 'loop.js') 33 34 def RunAction(self, tab): 35 try: 36 tab.ExecuteJavaScript( 37 'window.__loopMedia({{ selector }}, {{ loop_count }});', 38 selector=self._selector, loop_count=self._loop_count) 39 if self._timeout_in_seconds > 0: 40 self.WaitForEvent(tab, self._selector, 'loop', self._timeout_in_seconds) 41 except exceptions.EvaluateException: 42 raise page_action.PageActionFailed('Cannot loop media element(s) with ' 43 'selector = %s.' % self._selector) 44