Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 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 "seek" action on media elements. | |
| 6 | |
| 7 Action attributes are: | |
| 8 - seek_time: The media time to seek to. Test fails if not provided. | |
| 9 - selector: If no selector is defined then the action attempts to seek the first | |
| 10 media element on the page. If 'all' then seek all media elements. | |
| 11 - log_seek_time: If true the seek time is recorded, otherwise media measurement | |
| 12 will not be aware of the seek action. Used to perform multiple | |
| 13 seeks. Default true. | |
| 14 - wait_for_seeked: If true forces the action to wait for seeked event to fire. | |
| 15 Default false. | |
| 16 - wait_timeout: Timeout to wait for seeked event. Only valid with | |
| 17 wait_for_seeked=true | |
| 18 """ | |
| 19 | |
| 20 import os | |
| 21 | |
| 22 from telemetry.core import exceptions | |
| 23 from telemetry.core import util | |
| 24 from telemetry.page.actions import page_action | |
| 25 | |
| 26 | |
| 27 class SeekAction(page_action.PageAction): | |
| 28 def __init__(self, attributes=None): | |
| 29 super(SeekAction, self).__init__(attributes) | |
| 30 | |
| 31 def WillRunAction(self, page, tab): | |
| 32 """Load the media metrics JS code prior to running the action.""" | |
| 33 with open(os.path.join(os.path.dirname(__file__), 'seek.js')) as f: | |
| 34 js = f.read() | |
| 35 tab.ExecuteJavaScript(js) | |
| 36 | |
| 37 def RunAction(self, page, tab, previous_action): | |
| 38 try: | |
| 39 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.
| |
| 40 raise page_action.PageActionFailed('Seek time is missing in the action') | |
| 41 selector = self.selector if hasattr(self, 'selector') else '' | |
| 42 log_seek = self.log_seek if hasattr(self, 'log_seek') else True | |
| 43 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.
| |
| 44 (selector, self.seek_time, log_seek)) | |
| 45 timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60 | |
| 46 # Check if we need to wait for 'seeked' event to fire. | |
| 47 if hasattr(self, 'wait_for_seeked') and self.wait_for_seeked: | |
| 48 self.WaitForEvent(tab, selector, 'seeked', timeout) | |
| 49 except exceptions.EvaluateException: | |
| 50 raise page_action.PageActionFailed('Cannot seek media element(s) with ' | |
| 51 'selector = %s.' % selector) | |
| 52 | |
| 53 def WaitForEvent(self, tab, selector, event_name, timeout): | |
| 54 """Halts seek action until the selector's event is fired.""" | |
| 55 util.WaitFor(lambda: self.HasEventCompleted(tab, selector, event_name), | |
| 56 timeout=timeout, poll_interval=0.5) | |
| 57 | |
| 58 def HasEventCompleted(self, tab, selector, event_name): | |
| 59 return tab.EvaluateJavaScript( | |
| 60 'window.__hasEventCompleted(\'%s\', \'%s\');' % (selector, event_name)) | |
| OLD | NEW |