Home | History | Annotate | Download | only in actions
      1 # Copyright (c) 2012 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 class PageActionNotSupported(Exception):
      6   pass
      7 
      8 class PageActionFailed(Exception):
      9   pass
     10 
     11 class PageAction(object):
     12   """Represents an action that a user might try to perform to a page."""
     13   _next_timeline_marker_id = 0
     14 
     15   def __init__(self, attributes=None):
     16     if attributes:
     17       for k, v in attributes.iteritems():
     18         setattr(self, k, v)
     19     self._timeline_marker_base_name = None
     20     self._timeline_marker_id = None
     21 
     22   def CustomizeBrowserOptions(self, options):
     23     """Override to add action-specific options to the BrowserOptions
     24     object."""
     25     pass
     26 
     27   def WillRunAction(self, page, tab):
     28     """Override to do action-specific setup before
     29     Test.WillRunAction is called."""
     30     pass
     31 
     32   def RunAction(self, page, tab, previous_action):
     33     raise NotImplementedError()
     34 
     35   def RunsPreviousAction(self):
     36     """Some actions require some initialization to be performed before the
     37     previous action. For example, wait for href change needs to record the old
     38     href before the previous action changes it. Therefore, we allow actions to
     39     run the previous action. An action that does this should override this to
     40     return True in order to prevent the previous action from being run twice."""
     41     return False
     42 
     43   def CleanUp(self, page, tab):
     44     pass
     45 
     46   def CanBeBound(self):
     47     """If this class implements BindMeasurementJavaScript, override CanBeBound
     48     to return True so that a test knows it can bind measurements."""
     49     return False
     50 
     51   def BindMeasurementJavaScript(
     52       self, tab, start_js, stop_js):  # pylint: disable=W0613
     53     """Let this action determine when measurements should start and stop.
     54 
     55     A measurement can call this method to provide the action
     56     with JavaScript code that starts and stops measurements. The action
     57     determines when to execute the provided JavaScript code, for more accurate
     58     timings.
     59 
     60     Args:
     61       tab: The tab to do everything on.
     62       start_js: JavaScript code that starts measurements.
     63       stop_js: JavaScript code that stops measurements.
     64     """
     65     raise Exception('This action cannot be bound.')
     66 
     67   @staticmethod
     68   def ResetNextTimelineMarkerId():
     69     PageAction._next_timeline_marker_id = 0
     70 
     71   def _SetTimelineMarkerBaseName(self, name):
     72     self._timeline_marker_base_name = name
     73     self._timeline_marker_id = PageAction._next_timeline_marker_id
     74     PageAction._next_timeline_marker_id += 1
     75 
     76   def GetTimelineMarkerName(self):
     77     if self._timeline_marker_base_name:
     78       return \
     79         '%s_%d' % (self._timeline_marker_base_name, self._timeline_marker_id)
     80     else:
     81       return None
     82