"""A Telemetry page_action that performs the "seek" action on media elements.
-Action attributes are:
-- seek_time: The media time to seek to. Test fails if not provided.
+Action parameters are:
+- seconds: The media time to seek to. Test fails if not provided.
- selector: If no selector is defined then the action attempts to seek the first
media element on the page. If 'all' then seek all media elements.
-- log_seek_time: If true the seek time is recorded, otherwise media measurement
- will not be aware of the seek action. Used to perform multiple
- seeks. Default true.
-- wait_for_seeked: If true forces the action to wait for seeked event to fire.
- Default false.
-- wait_timeout: Timeout to wait for seeked event. Only valid with
- wait_for_seeked=true
-- seek_label: A suffix string to name the seek perf measurement.
+- timeout_in_seconds: Maximum waiting time for the "seeked" event
+ (dispatched when the seeked operation completes)
+ to be fired. 0 means do not wait.
+- log_time: If true the seek time is recorded, otherwise media
+ measurement will not be aware of the seek action. Used to
+ perform multiple seeks. Default true.
+- label: A suffix string to name the seek perf measurement.
"""
from telemetry.core import exceptions
class SeekAction(media_action.MediaAction):
+ def __init__(self, seconds, selector=None, timeout_in_seconds=0,
+ log_time=True, label=''):
+ super(SeekAction, self).__init__()
+ self._seconds = seconds
+ self._selector = selector if selector else ''
+ self._timeout_in_seconds = timeout_in_seconds
+ self._log_time = log_time
+ self._label = label
+
def WillRunAction(self, tab):
"""Load the media metrics JS code prior to running the action."""
super(SeekAction, self).WillRunAction(tab)
def RunAction(self, tab):
try:
- assert hasattr(self, 'seek_time')
- selector = self.selector if hasattr(self, 'selector') else ''
- log_seek = self.log_seek == True if hasattr(self, 'log_seek') else True
- seek_label = self.seek_label if hasattr(self, 'seek_label') else ''
- tab.ExecuteJavaScript('window.__seekMedia("%s", "%s", %i, "%s");' %
- (selector, self.seek_time, log_seek, seek_label))
- timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60
- # Check if we need to wait for 'seeked' event to fire.
- if hasattr(self, 'wait_for_seeked') and self.wait_for_seeked:
- self.WaitForEvent(tab, selector, 'seeked', timeout)
+ tab.ExecuteJavaScript(
+ 'window.__seekMedia("%s", "%s", %i, "%s");' %
+ (self._selector, self._seconds, self._log_time, self._label))
+ if self._timeout_in_seconds > 0:
+ self.WaitForEvent(tab, self._selector, 'seeked',
+ self._timeout_in_seconds)
except exceptions.EvaluateException:
raise page_action.PageActionFailed('Cannot seek media element(s) with '
- 'selector = %s.' % selector)
+ 'selector = %s.' % self._selector)