"""A Telemetry page_action that performs the "play" action on media elements.
-Media elements can be specified by a selector attribute. If no selector is
+Media elements can be specified by a selector argument. If no selector is
defined then then the action attempts to play the first video element or audio
element on the page. A selector can also be 'all' to play all media elements.
-Other attributes to use are: wait_for_playing and wait_for_ended, which forces
-the action to wait until playing and ended events get fired respectively.
+Other arguments to use are: playing_event_timeout_in_seconds and
+ended_event_timeout_in_seconds, which forces the action to wait until
+playing and ended events get fired respectively.
"""
from telemetry.core import exceptions
class PlayAction(media_action.MediaAction):
- def __init__(self, attributes=None):
- super(PlayAction, self).__init__(attributes)
+ def __init__(self, selector=None,
+ playing_event_timeout_in_seconds=0,
+ ended_event_timeout_in_seconds=0):
+ super(PlayAction, self).__init__()
+ self._selector = selector if selector else ''
+ self._playing_event_timeout_in_seconds = playing_event_timeout_in_seconds
+ self._ended_event_timeout_in_seconds = ended_event_timeout_in_seconds
def WillRunAction(self, tab):
"""Load the media metrics JS code prior to running the action."""
def RunAction(self, tab):
try:
- selector = self.selector if hasattr(self, 'selector') else ''
- tab.ExecuteJavaScript('window.__playMedia("%s");' % selector)
- timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60
+ tab.ExecuteJavaScript('window.__playMedia("%s");' % self._selector)
# Check if we need to wait for 'playing' event to fire.
- if hasattr(self, 'wait_for_playing') and self.wait_for_playing:
- self.WaitForEvent(tab, selector, 'playing', timeout)
+ if self._playing_event_timeout_in_seconds > 0:
+ self.WaitForEvent(tab, self._selector, 'playing',
+ self._playing_event_timeout_in_seconds)
# Check if we need to wait for 'ended' event to fire.
- if hasattr(self, 'wait_for_ended') and self.wait_for_ended:
- self.WaitForEvent(tab, selector, 'ended', timeout)
+ if self._ended_event_timeout_in_seconds > 0:
+ self.WaitForEvent(tab, self._selector, 'ended',
+ self._ended_event_timeout_in_seconds)
except exceptions.EvaluateException:
raise page_action.PageActionFailed('Cannot play media element(s) with '
- 'selector = %s.' % selector)
+ 'selector = %s.' % self._selector)