146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved. 27dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch# Use of this source code is governed by a BSD-style license that can be 37dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch# found in the LICENSE file. 47dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch"""A Telemetry page_action that performs the "play" action on media elements. 67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 7116680a4aac90f2aa7413d9095a592090648e557Ben MurdochMedia elements can be specified by a selector argument. If no selector is 87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochdefined then then the action attempts to play the first video element or audio 97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochelement on the page. A selector can also be 'all' to play all media elements. 107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 11116680a4aac90f2aa7413d9095a592090648e557Ben MurdochOther arguments to use are: playing_event_timeout_in_seconds and 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochended_event_timeout_in_seconds, which forces the action to wait until 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdochplaying and ended events get fired respectively. 147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch""" 157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochfrom telemetry.core import exceptions 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)from telemetry.page.actions import media_action 187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochfrom telemetry.page.actions import page_action 197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class PlayAction(media_action.MediaAction): 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch def __init__(self, selector=None, 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch playing_event_timeout_in_seconds=0, 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ended_event_timeout_in_seconds=0): 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch super(PlayAction, self).__init__() 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self._selector = selector if selector else '' 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self._playing_event_timeout_in_seconds = playing_event_timeout_in_seconds 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self._ended_event_timeout_in_seconds = ended_event_timeout_in_seconds 297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) def WillRunAction(self, tab): 317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch """Load the media metrics JS code prior to running the action.""" 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) super(PlayAction, self).WillRunAction(tab) 33a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch self.LoadJS(tab, 'play.js') 347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) def RunAction(self, tab): 367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch try: 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch tab.ExecuteJavaScript('window.__playMedia("%s");' % self._selector) 387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch # Check if we need to wait for 'playing' event to fire. 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if self._playing_event_timeout_in_seconds > 0: 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self.WaitForEvent(tab, self._selector, 'playing', 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self._playing_event_timeout_in_seconds) 427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch # Check if we need to wait for 'ended' event to fire. 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if self._ended_event_timeout_in_seconds > 0: 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self.WaitForEvent(tab, self._selector, 'ended', 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch self._ended_event_timeout_in_seconds) 467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch except exceptions.EvaluateException: 477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch raise page_action.PageActionFailed('Cannot play media element(s) with ' 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 'selector = %s.' % self._selector) 49