Chromium Code Reviews| Index: tools/telemetry/telemetry/page/actions/seek.py |
| diff --git a/tools/telemetry/telemetry/page/actions/seek.py b/tools/telemetry/telemetry/page/actions/seek.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83fee3e40a23320bd74d053c98dd894e03ebf902 |
| --- /dev/null |
| +++ b/tools/telemetry/telemetry/page/actions/seek.py |
| @@ -0,0 +1,60 @@ |
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""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. |
| +- 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 |
| +""" |
| + |
| +import os |
| + |
| +from telemetry.core import exceptions |
| +from telemetry.core import util |
| +from telemetry.page.actions import page_action |
| + |
| + |
| +class SeekAction(page_action.PageAction): |
| + def __init__(self, attributes=None): |
| + super(SeekAction, self).__init__(attributes) |
| + |
| + def WillRunAction(self, page, tab): |
| + """Load the media metrics JS code prior to running the action.""" |
| + with open(os.path.join(os.path.dirname(__file__), 'seek.js')) as f: |
| + js = f.read() |
| + tab.ExecuteJavaScript(js) |
| + |
| + def RunAction(self, page, tab, previous_action): |
| + try: |
| + if not hasattr(self, 'seek_time'): |
|
dtu
2013/07/20 01:20:56
assert this instead of raising PageActionFailed.
shadi
2013/07/22 19:18:31
Done.
|
| + raise page_action.PageActionFailed('Seek time is missing in the action') |
| + selector = self.selector if hasattr(self, 'selector') else '' |
| + log_seek = self.log_seek if hasattr(self, 'log_seek') else True |
| + tab.ExecuteJavaScript('window.__seekMedia(\'%s\', \'%s\', \'%s\');' % |
|
dtu
2013/07/20 01:20:56
" in place of \'
shadi
2013/07/22 19:18:31
Done.
|
| + (selector, self.seek_time, log_seek)) |
| + 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) |
| + except exceptions.EvaluateException: |
| + raise page_action.PageActionFailed('Cannot seek media element(s) with ' |
| + 'selector = %s.' % selector) |
| + |
| + def WaitForEvent(self, tab, selector, event_name, timeout): |
| + """Halts seek action until the selector's event is fired.""" |
| + util.WaitFor(lambda: self.HasEventCompleted(tab, selector, event_name), |
| + timeout=timeout, poll_interval=0.5) |
| + |
| + def HasEventCompleted(self, tab, selector, event_name): |
| + return tab.EvaluateJavaScript( |
| + 'window.__hasEventCompleted(\'%s\', \'%s\');' % (selector, event_name)) |