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 performs the "play" action on media elements. 6 7Media elements can be specified by a selector attribute. If no selector is 8defined then then the action attempts to play the first video element or audio 9element on the page. A selector can also be 'all' to play all media elements. 10 11Other attributes to use are: wait_for_playing and wait_for_ended, which forces 12the action to wait until playing and ended events get fired respectively. 13""" 14 15from telemetry.core import exceptions 16from telemetry.page.actions import media_action 17from telemetry.page.actions import page_action 18 19 20class PlayAction(media_action.MediaAction): 21 def __init__(self, attributes=None): 22 super(PlayAction, self).__init__(attributes) 23 24 def WillRunAction(self, tab): 25 """Load the media metrics JS code prior to running the action.""" 26 super(PlayAction, self).WillRunAction(tab) 27 self.LoadJS(tab, 'play.js') 28 29 def RunAction(self, tab): 30 try: 31 selector = self.selector if hasattr(self, 'selector') else '' 32 tab.ExecuteJavaScript('window.__playMedia("%s");' % selector) 33 timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60 34 # Check if we need to wait for 'playing' event to fire. 35 if hasattr(self, 'wait_for_playing') and self.wait_for_playing: 36 self.WaitForEvent(tab, selector, 'playing', timeout) 37 # Check if we need to wait for 'ended' event to fire. 38 if hasattr(self, 'wait_for_ended') and self.wait_for_ended: 39 self.WaitForEvent(tab, selector, 'ended', timeout) 40 except exceptions.EvaluateException: 41 raise page_action.PageActionFailed('Cannot play media element(s) with ' 42 'selector = %s.' % selector) 43